Cannot move file because NoSuchFileException - java

I use this code to list files into directory and move it based on a found value.
#Override
public void execute(JobExecutionContext context) {
File directoryPath = new File("C:\\csv\\nov");
// Create a new subfolder called "processed" into source directory
try {
Path processedFolderPath = Path.of(directoryPath.getAbsolutePath() + "/processed");
if (!Files.exists(processedFolderPath) || !Files.isDirectory(processedFolderPath)) {
Files.createDirectory(processedFolderPath);
}
Path invalidFilesFolderPath = Path.of(directoryPath.getAbsolutePath() + "/invalid_files");
if (!Files.exists(invalidFilesFolderPath) || !Files.isDirectory(invalidFilesFolderPath)) {
Files.createDirectory(invalidFilesFolderPath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
FilenameFilter textFileFilter = (dir, name) -> {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".csv")) {
return true;
} else {
return false;
}
};
// List of all the csv files
File filesList[] = directoryPath.listFiles(textFileFilter);
System.out.println("List of the text files in the specified directory:");
for(File file : filesList) {
try {
try (var br = new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)){
List<CsvLine> beans = new CsvToBeanBuilder(br)
.withType(CsvLine.class)
.withSeparator('\t')
.withSkipLines(3)
.build()
.parse();
for (CsvLine item : beans)
{
Path originalPath = file.toPath();
if(item.getValue().equals(2)
|| item.getValue().equals(43)
|| item.getValue().equals(32))
{
// Move here file into new subdirectory when file is invalid
Path copied = Paths.get(file.getParent() + "/invalid_files");
try {
// Use resolve method to keep the "processed" as folder
br.close();
Files.move(originalPath, copied.resolve(originalPath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}}
} catch (Exception e){
e.printStackTrace();
}
Path originalPath = file.toPath();
System.out.println(String.format("\nProcessed file : %s, moving the file to subfolder /processed\n",
originalPath));
}
// Move here file into new subdirectory when file processing is finished
Path copied = Paths.get(file.getParent() + "/processed");
try {
// Use resolve method to keep the "processed" as folder
br.close();
Files.move(originalPath, copied.resolve(originalPath.getFileName()), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
The problem is that I try to move a file. I get error:
java.lang.RuntimeException: java.nio.file.NoSuchFileException: C:\csv\nov\12_21_39.csv
at com.wordscore.engine.processor.DataValidationCheckJob.execute(DataValidationCheckJob.java:94)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.nio.file.NoSuchFileException: C:\csv\nov\12_21_39.csv
How I can edit the code in way that it can be moved properly?

It looks like you have omitted break; after both Files.move(originalPath, ...); calls.
Without a break to exit the for (CsvLine item : beans) loop, processing continues over the next iteration. The second iteration will attempt to perform Files.move(originalPath, ...) again - and fails because the file has already been moved.
Therefore you get the NoSuchFileException which you have re-thrown as RuntimeException in your try.. Files.move .. catch block.
The handling would be cleaner if you dealt with move in one place and such that it avoids the untidy br.close(), leaving file handling to the auto-close try() block. Something like this:
Path invalidDir = Paths.get(file.getParent() + "/invalid_files");
Path processedDir = Paths.get(file.getParent() + "/processed");
for(File file : filesList) {
Path originalPath = file.toPath();
Path moveTo = processedDir.resolve(originalPath.getFileName());
try (var br = new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)){
List<CsvLine> beans = new CsvToBeanBuilder(br)
.withType(CsvLine.class)
.withSeparator('\t')
.withSkipLines(3)
.build()
.parse();
for (CsvLine item : beans) {
if(item.getValue().equals(2)
|| item.getValue().equals(43)
|| item.getValue().equals(32)) {
// file is invalid, skip it
moveTo = invalidDir.resolve(originalPath.getFileName());
// LOG MSG HERE
break;
}
}
}
Files.move(originalPath, moveTo, StandardCopyOption.REPLACE_EXISTING);
System.out.format("%nProcessed file : %s, moving the file to subfolder %s%n", originalPath, moveTo);
}

Related

Move file after reading content [duplicate]

I want to create a Quartz job which reads .csv files and moves them when file is processed. I tried this:
#Override
public void execute(JobExecutionContext context) {
File directoryPath = new File("C:\\csv\\nov");
// Create a new subfolder called "processed" into source directory
try {
Files.createDirectory(Path.of(directoryPath.getAbsolutePath() + "/processed"));
} catch (IOException e) {
throw new RuntimeException(e);
}
FilenameFilter textFileFilter = (dir, name) -> {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".csv")) {
return true;
} else {
return false;
}
};
// List of all the csv files
File filesList[] = directoryPath.listFiles(textFileFilter);
System.out.println("List of the text files in the specified directory:");
Optional<File> csvFile = Arrays.stream(filesList).findFirst();
File file = csvFile.get();
for(File file : filesList) {
try {
List<CsvLine> beans = new CsvToBeanBuilder(new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16))
.....
.build()
.parse();
for(CsvLine item: beans){
....... sql queries
Optional<ProcessedWords> isFound = processedWordsService.findByKeyword(item.getKeyword());
......................................
}
} catch (Exception e){
e.printStackTrace();
}
// Move here file into new subdirectory when file processing is finished
Path copied = Paths.get(file.getAbsolutePath() + "/processed");
Path originalPath = file.toPath();
try {
Files.move(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
Folder processed is created when the job is started but I get exception:
2022-11-17 23:12:51.470 ERROR 16512 --- [cessor_Worker-4] org.quartz.core.JobRunShell : Job DEFAULT.keywordPostJobDetail threw an unhandled Exception:
java.lang.RuntimeException: java.nio.file.FileSystemException: C:\csv\nov\11_42_33.csv -> C:\csv\nov\processed\11_42_33.csv: The process cannot access the file because it is being used by another process
at com.wordscore.engine.processor.ImportCsvFilePostJob.execute(ImportCsvFilePostJob.java:127) ~[main/:na]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) ~[quartz-2.3.2.jar:na]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) ~[quartz-2.3.2.jar:na]
Caused by: java.nio.file.FileSystemException: C:\csv\nov\11_42_33.csv -> C:\csv\nov\processed\11_42_33.csv: The process cannot access the file because it is being used by another process
at java.base/sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:92) ~[na:na]
at java.base/sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:103) ~[na:na]
at java.base/sun.nio.fs.WindowsFileCopy.move(WindowsFileCopy.java:403) ~[na:na]
at java.base/sun.nio.fs.WindowsFileSystemProvider.move(WindowsFileSystemProvider.java:293) ~[na:na]
at java.base/java.nio.file.Files.move(Files.java:1432) ~[na:na]
at com.wordscore.engine.processor.ImportCsvFilePostJob.execute(ImportCsvFilePostJob.java:125) ~[main/:na]
... 2 common frames omitted
Do you know how I can release the file and move it into a sub directory?
EDIT: Update code with try-catch
#Override
public void execute(JobExecutionContext context) {
File directoryPath = new File("C:\\csv\\nov");
// Create a new subfolder called "processed" into source directory
try {
Path path = Path.of(directoryPath.getAbsolutePath() + "/processed");
if (!Files.exists(path) || !Files.isDirectory(path)) {
Files.createDirectory(path);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
FilenameFilter textFileFilter = (dir, name) -> {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".csv")) {
return true;
} else {
return false;
}
};
// List of all the csv files
File filesList[] = directoryPath.listFiles(textFileFilter);
System.out.println("List of the text files in the specified directory:");
Optional<File> csvFile = Arrays.stream(filesList).findFirst();
File file = csvFile.get();
for(File file : filesList) {
try {
try (var br = new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)){
List<CsvLine> beans = new CsvToBeanBuilder(br)
......
.build()
.parse();
for (CsvLine item : beans) {
.....
if (isFound.isPresent()) {
.........
}}
} catch (Exception e){
e.printStackTrace();
}
// Move here file into new subdirectory when file processing is finished
Path copied = Paths.get(file.getAbsolutePath() + "/processed");
Path originalPath = file.toPath();
try {
Files.move(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Quartz config:
#Configuration
public class SchedulerConfig {
private static final Logger LOG = LoggerFactory.getLogger(SchedulerConfig.class);
private ApplicationContext applicationContext;
#Autowired
public SchedulerConfig(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
#Bean
public JobFactory jobFactory() {
AutowiringSpringBeanJobFactory jobFactory = new AutowiringSpringBeanJobFactory();
jobFactory.setApplicationContext(applicationContext);
return jobFactory;
}
#Bean
public SchedulerFactoryBean schedulerFactoryBean(Trigger simpleJobTrigger) throws IOException {
SchedulerFactoryBean schedulerFactory = new SchedulerFactoryBean();
schedulerFactory.setQuartzProperties(quartzProperties());
schedulerFactory.setWaitForJobsToCompleteOnShutdown(true);
schedulerFactory.setAutoStartup(true);
schedulerFactory.setTriggers(simpleJobTrigger);
schedulerFactory.setJobFactory(jobFactory());
return schedulerFactory;
}
#Bean
public SimpleTriggerFactoryBean simpleJobTrigger(#Qualifier("keywordPostJobDetail") JobDetail jobDetail,
#Value("${simplejob.frequency}") long frequency) {
LOG.info("simpleJobTrigger");
SimpleTriggerFactoryBean factoryBean = new SimpleTriggerFactoryBean();
factoryBean.setJobDetail(jobDetail);
factoryBean.setStartDelay(1000);
factoryBean.setRepeatInterval(frequency);
factoryBean.setRepeatCount(4); // factoryBean.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
return factoryBean;
}
#Bean
public JobDetailFactoryBean keywordPostJobDetail() {
JobDetailFactoryBean factoryBean = new JobDetailFactoryBean();
factoryBean.setJobClass(ImportCsvFilePostJob.class);
factoryBean.setDurability(true);
return factoryBean;
}
public Properties quartzProperties() throws IOException {
PropertiesFactoryBean propertiesFactoryBean = new PropertiesFactoryBean();
propertiesFactoryBean.setLocation(new ClassPathResource("/quartz.properties"));
propertiesFactoryBean.afterPropertiesSet();
return propertiesFactoryBean.getObject();
}
}
Quartz config:
org.quartz.scheduler.instanceName=wordscore-processor
org.quartz.scheduler.instanceId=AUTO
org.quartz.threadPool.threadCount=5
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
As you can see I wan to have 5 threads in order to execute 5 parallel jobs. Do you know how I can process the files without this exception?
new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)
This parts creates a resource. A resource is an object that represents an underlying heavy thing - a thing that you can have very few of. In this case, it represents an underlying OS file handle.
You must always safely close these. There are really only 2 ways to do it correctly:
Use try-with-resources
Save it to a field, and make yourself AutoClosable so the code that uses of instances of this class can use try-with-resources
try (var br = new FileReader(file, StandardCharsets.UTF_16)) {
List<CsvLine> beans = new CsvToBeanBuilder(br)
.....
.build()
.parse();
}
Is the answer.
Although I agree completely with the answer and comments of #rzwitserloot, note the following in your error stack trace:
java.nio.file.FileSystemException: C:\csv\nov\07_06_26.csv -> C:\csv\nov\07_06_26.csv\processed: The process cannot access the file because it is being used by another process
You are trying moving your file to the backup directory, but note you are doing it to the wrong path, C:\csv\nov\07_06_26.csv\processed, in the example.
Please, try the following:
#Override
public void execute(JobExecutionContext context) {
File directoryPath = new File("C:\\csv\\nov");
// Create a new subfolder called "processed" into source directory
// Hold a reference to the processed files directory path, we will
// use it later
Path processedDirectoryPath;
try {
processedDirectoryPath = Path.of(directoryPath.getAbsolutePath() + "/processed");
if (!Files.exists(processedDirectoryPath) || !Files.isDirectory(processedDirectoryPath)) {
Files.createDirectory(processedDirectoryPath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
FilenameFilter textFileFilter = (dir, name) -> {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".csv")) {
return true;
} else {
return false;
}
};
// List of all the csv files
File filesList[] = directoryPath.listFiles(textFileFilter);
System.out.println("List of the text files in the specified directory:");
for(File file : filesList) {
try {
try (var br = new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)){
List<CsvLine> beans = new CsvToBeanBuilder(br)
......
.build()
.parse();
for (CsvLine item : beans) {
.....
if (isFound.isPresent()) {
.........
}}
} catch (Exception e){
e.printStackTrace();
}
// Move here file into new subdirectory when file processing is finished
// In my opinion, here is the error:
// Path copied = Paths.get(file.getAbsolutePath() + "/processed");
Path originalPath = file.toPath();
try {
// Note the use of the path we defined before
Files.move(originalPath, processedDirectoryPath.resolve(originalPath.getFileName()),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
If you need to increase the throughput of files processed, you could try splitting them in batches, say for certain pattern in their name like a month name or a job number, for instance. The simple solution could be to use the provided JobExecutionContext of every job to include some split criteria. That criteria will be used in your FilenameFilter causing every job to process only a certain portion of the whole amount of files that need to be processed. I think the solution is preferable to any kind of locking or similar mechanism..
For example, consider the following:
#Override
public void execute(JobExecutionContext context) {
File directoryPath = new File("C:\\csv\\nov");
// Create a new subfolder called "processed" into source directory
// Hold a reference to the processed files directory path, we will
// use it later
Path processedDirectoryPath;
try {
processedDirectoryPath = Path.of(directoryPath.getAbsolutePath() + "/processed");
if (!Files.exists(processedDirectoryPath) || !Files.isDirectory(processedDirectoryPath)) {
Files.createDirectory(processedDirectoryPath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
// We obtain the file processing criteria using a job parameter
JobDataMap data = context.getJobDetail().getJobDataMap();
String filenameProcessingCriteria = data.getString("FILENAME_PROCESSING_CRITERIA");
// Use the provided criteria to restrict the files that this job
// will process
FilenameFilter textFileFilter = (dir, name) -> {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".csv") && lowercaseName.indexOf(filenameProcessingCriteria) > 0) {
return true;
} else {
return false;
}
};
// List of all the csv files
File filesList[] = directoryPath.listFiles(textFileFilter);
System.out.println("List of the text files in the specified directory:");
for(File file : filesList) {
try {
try (var br = new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)){
List<CsvLine> beans = new CsvToBeanBuilder(br)
......
.build()
.parse();
for (CsvLine item : beans) {
.....
if (isFound.isPresent()) {
.........
}}
} catch (Exception e){
e.printStackTrace();
}
// Move here file into new subdirectory when file processing is finished
// In my opinion, here is the error:
// Path copied = Paths.get(file.getAbsolutePath() + "/processed");
Path originalPath = file.toPath();
try {
// Note the use of the path we defined before
Files.move(originalPath, processedDirectoryPath.resolve(originalPath.getFileName()),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
You need to pass the required parameter to your jobs:
JobDetail job1 = ...;
job1.getJobDataMap().put("FILENAME_PROCESSING_CRITERIA", "job1pattern");
An even simpler approach, based on the same idea, could be splitting the files in different folders and pass the folder name that need to be processed as a job parameter:
#Override
public void execute(JobExecutionContext context) {
// We obtain the directory path as a job parameter
JobDataMap data = context.getJobDetail().getJobDataMap();
String directoryPathName = data.getString("DIRECTORY_PATH_NAME");
File directoryPath = new File(directoryPathName);
// Create a new subfolder called "processed" into source directory
// Hold a reference to the processed files directory path, we will
// use it later
Path processedDirectoryPath;
try {
processedDirectoryPath = Path.of(directoryPath.getAbsolutePath() + "/processed");
if (!Files.exists(processedDirectoryPath) || !Files.isDirectory(processedDirectoryPath)) {
Files.createDirectory(processedDirectoryPath);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
FilenameFilter textFileFilter = (dir, name) -> {
String lowercaseName = name.toLowerCase();
if (lowercaseName.endsWith(".csv")) {
return true;
} else {
return false;
}
};
// List of all the csv files
File filesList[] = directoryPath.listFiles(textFileFilter);
System.out.println("List of the text files in the specified directory:");
for(File file : filesList) {
try {
try (var br = new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16)){
List<CsvLine> beans = new CsvToBeanBuilder(br)
......
.build()
.parse();
for (CsvLine item : beans) {
.....
if (isFound.isPresent()) {
.........
}}
} catch (Exception e){
e.printStackTrace();
}
// Move here file into new subdirectory when file processing is finished
// In my opinion, here is the error:
// Path copied = Paths.get(file.getAbsolutePath() + "/processed");
Path originalPath = file.toPath();
try {
// Note the use of the path we defined before
Files.move(originalPath, processedDirectoryPath.resolve(originalPath.getFileName()),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
And pass a different folder to every different job:
JobDetail job1 = ...;
job1.getJobDataMap().put("DIRECTORY_PATH_NAME", "C:\\csv\\nov");
Please, consider refactor your code and define methods for file processing, file backup, etc, it will make your code easy to understand and handle.
Assuming we have File file = new File("c:/test.txt"), and print the the following paths:
Path copied = Paths.get(file.getAbsolutePath() + "/processed");
Path originalPath = file.toPath();
We will get the result:
copied: C:\test.txt\processed
originalPath: C:\test.txt
So its incorrect. You should try to get the parent path plus the processed folder plus the file name.
Path copied = Paths.get(file.getParentFile().getAbsolutePath() + "/processed/" + file.getName());
Path originalPath = file.toPath();
The line in error message
Caused by: java.lang.RuntimeException:
java.nio.file.FileSystemException: C:\csv\nov\07_06_26.csv ->
C:\csv\nov\07_06_26.csv\processed: The process cannot access the file
because it is being used by another process
I think you want to move the file from C:\csv\nov to C:\csv\nov\processed, so
you have to change following line:
Path copied = Paths.get(file.getAbsolutePath() + "/processed");
to
Path copied = Paths.get(file.getParent() + "/processed");
because file.getAbsolutePath() returns the complete path, include the name of file.
I’m pretty sure that the file is being locked by the file reader that you create but never close in the following line:
List<CsvLine> beans = new CsvToBeanBuilder(new FileReader(file.getAbsolutePath(), StandardCharsets.UTF_16))
Refactor your code so that you have that reader in a try finally block or close it explicitly.
The unintuitive behavior you might see is that those files are released at seemingly random times. This is because when the garbage collector frees up those readers, they will then release the files. Clean them up explicitly instead.

Read multiple files zip file with multiple sub directories

I have a zip file with the structure like:
xml.zip
Root Folder: package
Folder: Subfolder.zip
Inside Subfolder.zip :
Root Folder: _
Folder: var
Folder: run
Folder: xmls
Xml1.xml
Xml2.xml
Xml3.xml
Is there a way to read in these three files recursively with the above structure? I've tried using ZipInputStream and ZipArchiveInputStream, but zip.getNextEntry() keeps returning null.. due to the nested zip. Anyway to recursively use it?
private void readZipFileStream(final InputStream zipFileStream) {
final ZipInputStream zipInputStream = new ZipInputStream(zipFileStream);
ZipEntry zipEntry;
try {
zipEntry = zipInputStream.getNextEntry();
while(zipEntry.getName() != null) {
System.out.println("name of zip entry: " + zipEntry.getName());
if (!zipEntry.isDirectory()) {
System.out.println("1."+zipInputStream.available());
System.out.println("2."+zipEntry.getName());
System.out.println("3."+zipEntry.isDirectory());
System.out.println("4."+zipEntry.getSize());
} else {
readZipFileStream(zipInputStream);
}
zipEntry = zipInputStream.getNextEntry();
}
// }
} catch (IOException e) {
e.printStackTrace();
}
}
Can someone please help?
recursive call should be done when the zip entry is identified as zip file. Use zipEntry.getName().endsWith(".zip") to identify and then call the same function.
public static void readZip(InputStream fileStream) throws IOException
{
ZipInputStream zipStream = new ZipInputStream(fileStream);
ZipEntry zipEntry = zipStream.getNextEntry();
try {
while(zipEntry !=null) {
String fileName = zipEntry.getName();
if (fileName.endsWith(".zip")) {
//recur if the entry is a zip file
readZip(zipStream);
}
else {
System.out.println(zipEntry.getName());
}
zipEntry = zipStream.getNextEntry();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Started working after calling the same function again if getName().contains(".zip").

How to create file within folder from TreeItem selected in TreeView with JavaFX

I'm working on a TreeView in which I'm required to have folders and files. Right now the code that I have keeps creating files without taking into count the selected folder in which the file is supposed to be.
I have this:
TreeItem<String> newFile = new TreeItem<>(strFile);
TreeItem<String> selection = treeView.getSelectionModel().getSelectedItem();
/*
*
* GET FULL PATH FROM SELECTED FOLDER
*
*/
if(selection != null) {
StringBuilder pathBuilder = new StringBuilder();
for (selection = treeView.getSelectionModel().getSelectedItem(); selection != null ; selection = selection.getParent()) {
pathBuilder.insert(0, selection.getValue());
pathBuilder.insert(0, "/");
}
String path = pathBuilder.toString();
System.out.println(path);
createFile(path, strFile); // Create file
} else if(selection == null) {
treeView.setRoot(newFile);
} else {
selection.getChildren().add(newFile);
selection.setExpanded(true);
}
which calls the createFile function. Said function accepts two strings, the directory name(if not null) and the file name to be created. I instantiated 3 File objects for some validation.
The first one accepts both parameters and saves the file inside X folder.
The second one to verify if folder exists, and if it does, save the file in X folder.
The third one is fr creating individual files where a folder has not been specified.
private void createFile(String strDirectory, String strFile) {
File dirFile = new File(strDirectory, strFile);
File directory = new File(strDirectory);
File file = new File(strFile);
boolean result;
if(directory.exists()){
try {
result = dirFile.createNewFile();
if(result) {
System.out.println("Successfully created "+file.getCanonicalPath());
} else {
System.out.println("Filed creating "+file.getCanonicalPath());
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
try {
result = file.createNewFile();
if(result) {
System.out.println("Successfully created "+file.getCanonicalPath());
} else {
System.out.println("Filed creating "+file.getCanonicalPath());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
I'm just trying to create files inside selected folder from treeview.

File directory error in JAR deployment

I'm trying to get the names of all the files in a folder "clock" which is inside the working directory "src".
The snippet below works fine if I run it but when I build the JAR file and run that I get a null error.
try {
File directory = new File("src/clock/");
File[] files = directory.listFiles();
for (File f: files) {
text.appendText(f.getName() + " ");
}
} catch (Exception e) {
text.appendText(e.getMessage() + " ");
}
File structure:
Update: (I'm using the ResourceAsStream now but same problem runs fine, deployed JAR doesn't work)
public void setImage() {
List<String>fn;
try {
fn = getResourceFiles("/clock/graphics/backgrounds/");
for (String s: fn) {
text.appendText(s);
}
} catch (Exception e) {
label.setText(e.getMessage());
}
}
private List<String>getResourceFiles(String path) throws IOException {
List<String> filenames = new ArrayList<>();
try (
InputStream in = getResourceAsStream(path);
BufferedReader br = new BufferedReader(new InputStreamReader( in ))) {
String resource;
while ((resource = br.readLine()) != null) {
filenames.add(resource);
}
}
return filenames;
}
private InputStream getResourceAsStream(String resource) {
final InputStream in = getContextClassLoader().getResourceAsStream(resource);
return in == null ? getClass().getResourceAsStream(resource) : in ;
}
private ClassLoader getContextClassLoader() {
return Thread.currentThread().getContextClassLoader();
}
That's because File searches in the
/path/to/your/application.jar
which File cant unzip to find the files in the specified path. It considers application.jar as a folder name and tries to read it.
Instead use
ClassLoader classLoader = YourClassName.class.getClassLoader();
InputStream sam = classLoader.getResourceAsStream(fileName);
to read the file from resources folder of the project when you want your jar to read a file.

How to copy an entire content from a directory to another in Java?

File content[] = new File("C:/FilesToGo/").listFiles();
for (int i = 0; i < content.length; i++){
String destiny = "C:/Kingdoms/"+content[i].getName();
File desc = new File(destiny);
try {
Files.copy(content[i].toPath(), desc.toPath(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
This is what I have. It copies everything just fine.
But among the contents there are some folders. The folders are copied but the folder's contents are not.
Would recommend using FileUtils in Apache Commons IO:
FileUtils.copyDirectory(new File("C:/FilesToGo/"),
new File("C:/Kingdoms/"));
Copies directories & contents.
Recursion. Here is a method the uses rescursion to delete a system of folders:
public void move(File file, File targetFile) {
if(file.isDirectory() && file.listFiles() != null) {
for(File file2 : file.listFiles()) {
move(file2, new File(targetFile.getPath() + "\\" + file.getName());
}
}
try {
Files.copy(file, targetFile.getPath() + "\\" + file.getName(), StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
Didn't test the code, but it should work. Basically, it digs down into the folders, telling it to move the item, if its a folder, go through all its children, and move them, etc.
Just to clarify what needs to be changed in Alex Coleman's answer, for the code to work. Here is the modified version of Alex's code that I tested and that works fine for me:
private void copyDirectoryContents(File source, File destination){
try {
String destinationPathString = destination.getPath() + "\\" + source.getName();
Path destinationPath = Paths.get(destinationPathString);
Files.copy(source.toPath(), destinationPath, StandardCopyOption.REPLACE_EXISTING);
}
catch (UnsupportedOperationException e) {
//UnsupportedOperationException
}
catch (DirectoryNotEmptyException e) {
//DirectoryNotEmptyException
}
catch (IOException e) {
//IOException
}
catch (SecurityException e) {
//SecurityException
}
if(source.isDirectory() && source.listFiles() != null){
for(File file : source.listFiles()) {
copyDirectoryContents(file, new File(destination.getPath() + "\\" + source.getName()));
}
}
}
The question is old by now, but I wanted to share my copy methods using java.nio.file.
Copying source directory: src into a container directory: dst.
"Directory" is just a helper class. In this example you can think of it as "Path" container.
We separate the directory structure from the file content.
It's explicit, and easy to imagine. (Also, it avoids some potential Exceptions thrown by the Files.copy() method if you instead copied all files in "one go")
public static void copy(Directory src, Directory dst, boolean replace) throws IOException {
if (src == null || dst == null) throw new IllegalArgumentException("...");
Path sourcePath = src.path();
Path targetPath = dst.path().resolve(sourcePath.getFileName());
copyStructure(sourcePath,targetPath);
copyContent(sourcePath,targetPath,replace);
}
I.e. we want to copy the folder "top" into the "dst" folder "container".
sourcePath = ...some/location/top
targetPath = ...another/location/container/top
copyStructure: Iterates through the source files. If the source file is a directory and the a target file with equivalent name does not exist, we create
the target folder. (So "copy" is not accurate. We "assure" structure)
private static void copyStructure(final Path source, final Path target) throws IOException {
if (source == null || target == null) throw new IllegalArgumentException("...");
final String tarString = target.toString();
final String srcString = source.toString();
Files.walk(source).forEach(new Consumer<Path>() {
#Override
public void accept(Path srcPath) {
if (Files.isDirectory(srcPath)) {
String subString = srcPath.toString().substring(srcString.length());
Path newFolder = Path.of(tarString,subString);
if (!Files.exists(newFolder)) {
try { Files.createDirectory(newFolder);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
}
Now that we know that a target directory structure exists, we iterate the source files again. But now we only copy the "content" (regular files). Choosing whether to replace existing content. copyContent:
private static void copyContent(final Path source, final Path target, boolean replace) throws IOException {
if (source == null || target == null) throw new IllegalArgumentException("...");
final String tarString = target.toString();
final String srcString = source.toString();
Files.walk(source).forEach(new Consumer<Path>() {
#Override
public void accept(Path srcPath) {
if (Files.isRegularFile(srcPath)) {
String subString = srcPath.toString().substring(srcString.length());
Path newFile = Path.of(tarString,subString);
if (!Files.exists(newFile)) {
try { Files.copy(srcPath,newFile);
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (replace) {
try { Files.copy(srcPath,newFile,
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
});
}

Categories

Resources