Привет, ребята У меня есть вопрос, что, как выполнить задачу в фоновом режиме в JavaFXЗапуск фоновой задачи в JavaFX
В настоящее время ситуация такова, что я создал функции копирования в JavaFX, она работает абсолютно нормально, но если у нас больше файлов, то он переходит в не отвечающий режим до тех пор, пока процесс не завершится, журналы также не будут напечатаны в моем текстовом поле. Каждый файл копируется в уважаемой папке, но проблема в том, что он висит до завершения процесса,
И еще один вопрос, как запустить эту программу навсегда, означает, что когда новый файл поступает в исходный каталог, он автоматически переходит в целевой каталог.
Вот мой код
try
{
sourceFile = new File(sourcePath).listFiles();
syslog.appendText("\nTotal Files in the Directory : " + sourceFile.length);
for(int i = 0; i<sourceFile.length;i++)
{
if(sourceFile[i].isFile())
{
String file = sourceFile[i].getName();
String extension = Files.getFileExtension(file);
if(!new File(destinationPath+"/"+extension.toUpperCase()).exists())
{
if(new File(destinationPath+"/"+extension.toUpperCase()).mkdir())
{
syslog.appendText("\nDirectory Created : " + destinationPath+"/"+extension.toUpperCase());
try
{
if(!new File(destinationPath+"/"+extension.toUpperCase()+"/"+file).exists())
{
syslog.appendText("\nFile "+file+" is processing to copy to "+destinationPath+"/"+extension.toUpperCase());
copyFile(sourceFile[i],new File(destinationPath+"/"+extension.toUpperCase()+"/"+file));
syslog.appendText("\nFile "+file+" is successfully copied to "+destinationPath+"/"+extension.toUpperCase());
if(sourceFile[i].delete())
syslog.appendText("\nFile "+file+" is successfully deleted from "+sourcePath);
else
syslog.appendText("\nError in deleting File "+file+" from "+sourcePath);
}
}
catch(Exception e)
{
e.printStackTrace();
syslog.appendText("\nSome Error Occurred while copying the File : "+sourceFile[i]);
}
}
}
else
{
try
{
if(!new File(destinationPath+"/"+extension.toUpperCase()+"/"+file).exists())
{
syslog.appendText("\nFile "+file+" is processing to copy to "+destinationPath+"/"+extension.toUpperCase());
copyFile(sourceFile[i],new File(destinationPath+"/"+extension.toUpperCase()+"/"+file));
syslog.appendText("\nFile "+file+" is successfully copied to "+destinationPath+"/"+extension.toUpperCase());
if(sourceFile[i].delete())
syslog.appendText("\nFile "+file+" is successfully deleted from "+sourcePath);
else
syslog.appendText("\nError in deleting File "+file+" from "+sourcePath);
}
}
catch(Exception e)
{
e.printStackTrace();
syslog.appendText("\nSome Error Occurred while copying the File : "+sourceFile[i]);
}
}
}
}
syslog.appendText("\nFinished..........");
}
catch (Exception e)
{
e.printStackTrace();
}
И это копия Функция
private static void copyFile(File source, File destination)
throws IOException {
FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
inputChannel = new FileInputStream(source).getChannel();
outputChannel = new FileOutputStream(destination).getChannel();
outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
} finally {
inputChannel.close();
outputChannel.close();
}
}
Не забудьте позвонить [ 'exService.shutdown()'] (https://docs.oracle.com/javase/8/docs/api /java/util/concurrent/ExecutorService.html#shutdown--) на каком-то этапе (обычно это приложения '' stop() ') (https://docs.oracle.com/javase/8/javafx/api/javafx/ application/Application.html # stop--)), чтобы завершить работу службы-исполнителя, когда она больше не нужна и позволит вашей программе изящно выйти. Либо это, либо создать ExecutorService с новой фабрикой потоков, предоставляющей потоки демона. – jewelsea