根据 Java 中的文件名删除数据库 table
Delete database table based on the file names in Java
我有下面的 class IImporter
代码,到目前为止工作正常。我每天收到许多 zip 文件,其中包含名称为 'EQUFULLFILE'
和 'NONEQUFULLFILE'
的目录,我试图从该目录读取文件并在数据库 table 'EquData'
或'NonEquData'
相应地。在处理之前,我只是想从这个数据库中删除所有数据 table。
但是我在我的代码中发现了问题。有时当我在目录 'EQUFULLFILE' or 'NONEQUFULLFILE'
中没有收到任何文件时,它只是从数据库 table.
中删除数据
我只需要稍微修改我的代码以适应逻辑,这样当我在目录中没有收到任何文件时 'EQUFULLFILE'
那么它不应该从数据库中删除任何数据 table 'EquData'
.当我没有在 'NONEQUFULLFILE'
目录中收到任何文件时,它不应从数据库 table 'NonEquData'
.
中删除任何数据
有什么建议吗?
@Service
public class IImporter {
private final static Logger log = LoggerFactory.getLogger(IImporter.class);
private final static String EQU_FILE_TAG = "EQUFULLFILE";
private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";
private boolean isEquity;
@Autowired
private IFullreader IFullreader;
@Autowired
private ZipWalker zipWalker;
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void importDir(Path indir) throws IOException {
log.info("Delete all table DATA");
//here the logic should be changed and based on file name table should be deleted
sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();
log.info("Process directory" + indir.toString());
Files.walk(indir, 1, FOLLOW_LINKS)
.filter(Files::isRegularFile)
.filter(f -> f.toString().endsWith(".zip"))
.sorted()
.forEach(f -> zipWalker.processZipFile(f, this::importFile));
}
private void importFile(Path path) {
this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
if (isEquity) {
//code for reading data from file EQUFULLFILE
}
else {
//code for reading data from file NONEQUFULLFILE
}
}
}
}
您已 EQU_FILE_TAG 定义了两次。我怀疑这是一个打字错误。
这个怎么样?
@Service
public class IImporter {
private final static Logger log = LoggerFactory.getLogger(IImporter.class);
private final static String EQU_FILE_TAG = "EQUFULLFILE";
private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";
private boolean isEquity;
private boolean equFileFirstTime = true;
private boolean nonequFileFirstTime = true;
@Autowired
private IFullreader IFullreader;
@Autowired
private ZipWalker zipWalker;
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void importDir(Path indir) throws IOException {
log.info("Process directory" + indir.toString());
Files.walk(indir, 1, FOLLOW_LINKS)
.filter(Files::isRegularFile)
.filter(f -> f.toString().endsWith(".zip"))
.sorted()
.forEach(f -> zipWalker.processZipFile(f, this::importFile));
}
private void importFile(Path path) {
this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
if (isEquity) {
if (equFileFirstTime) {
log.info("Delete EQUFULLFILE table DATA");
sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
equFileFirstTime = false;
}
//code for reading data from file EQUFULLFILE
}
else {
if (nonequFileFirstTime) {
log.info("Delete NONEQUFULLFILE table DATA");
sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();
nonequFileFirstTime = false;
}
//code for reading data from file NONEQUFULLFILE
}
}
}
我有下面的 class IImporter
代码,到目前为止工作正常。我每天收到许多 zip 文件,其中包含名称为 'EQUFULLFILE'
和 'NONEQUFULLFILE'
的目录,我试图从该目录读取文件并在数据库 table 'EquData'
或'NonEquData'
相应地。在处理之前,我只是想从这个数据库中删除所有数据 table。
但是我在我的代码中发现了问题。有时当我在目录 'EQUFULLFILE' or 'NONEQUFULLFILE'
中没有收到任何文件时,它只是从数据库 table.
我只需要稍微修改我的代码以适应逻辑,这样当我在目录中没有收到任何文件时 'EQUFULLFILE'
那么它不应该从数据库中删除任何数据 table 'EquData'
.当我没有在 'NONEQUFULLFILE'
目录中收到任何文件时,它不应从数据库 table 'NonEquData'
.
有什么建议吗?
@Service
public class IImporter {
private final static Logger log = LoggerFactory.getLogger(IImporter.class);
private final static String EQU_FILE_TAG = "EQUFULLFILE";
private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";
private boolean isEquity;
@Autowired
private IFullreader IFullreader;
@Autowired
private ZipWalker zipWalker;
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void importDir(Path indir) throws IOException {
log.info("Delete all table DATA");
//here the logic should be changed and based on file name table should be deleted
sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();
log.info("Process directory" + indir.toString());
Files.walk(indir, 1, FOLLOW_LINKS)
.filter(Files::isRegularFile)
.filter(f -> f.toString().endsWith(".zip"))
.sorted()
.forEach(f -> zipWalker.processZipFile(f, this::importFile));
}
private void importFile(Path path) {
this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
if (isEquity) {
//code for reading data from file EQUFULLFILE
}
else {
//code for reading data from file NONEQUFULLFILE
}
}
}
}
您已 EQU_FILE_TAG 定义了两次。我怀疑这是一个打字错误。
这个怎么样?
@Service
public class IImporter {
private final static Logger log = LoggerFactory.getLogger(IImporter.class);
private final static String EQU_FILE_TAG = "EQUFULLFILE";
private final static String NONEQU_FILE_TAG = "NONEQUFULLFILE";
private boolean isEquity;
private boolean equFileFirstTime = true;
private boolean nonequFileFirstTime = true;
@Autowired
private IFullreader IFullreader;
@Autowired
private ZipWalker zipWalker;
@Autowired
private SessionFactory sessionFactory;
@Transactional
public void importDir(Path indir) throws IOException {
log.info("Process directory" + indir.toString());
Files.walk(indir, 1, FOLLOW_LINKS)
.filter(Files::isRegularFile)
.filter(f -> f.toString().endsWith(".zip"))
.sorted()
.forEach(f -> zipWalker.processZipFile(f, this::importFile));
}
private void importFile(Path path) {
this.isEquity = path.getFileName().toString().contains(EQU_FILE_TAG);
if (isEquity) {
if (equFileFirstTime) {
log.info("Delete EQUFULLFILE table DATA");
sessionFactory.getCurrentSession().createQuery("delete from EquData").executeUpdate();
equFileFirstTime = false;
}
//code for reading data from file EQUFULLFILE
}
else {
if (nonequFileFirstTime) {
log.info("Delete NONEQUFULLFILE table DATA");
sessionFactory.getCurrentSession().createQuery("delete from NonEquData").executeUpdate();
nonequFileFirstTime = false;
}
//code for reading data from file NONEQUFULLFILE
}
}
}