Reputation: 991
I keep getting file or directory does not exist. I am running within a Groovy script that creates a Spring Application Context. I am easily reading in a different file using the same type of pathing. However, the file I am reading is in the class path of Spring. This script might be run by any number of people with different file systems, so I can't hard code a path. I need a relative path.
This is above in the class but important info.
private static String saveFilesToLocation = "/retrieve/";
Here is the code.
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation') ) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileLocation);
file.withOutputStream {
it.write fileBytes
}
println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
The paths in Strings have what I would expect in them, no extra characters.
UPDATE:
Thanks loteq Your answer would work too, and has better grooviness than our final result that worked. Since it is a sort of one off, we don't have the time to change to the nicer version you have.
Here is the code that worked for us, it is identical to above except the saveFilesToLocation is set to a directory that already exists now. The one before didn't exist and we would have needed to call mkdir like loteq suggested.
private static String saveFilesToLocation = "/tmp/retrieve/";
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
if (nextLine != null && (nextLine[0] != 'FileLocation') ) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileName);
file.withOutputStream {
it.write fileBytes
}
println "$counter) Wrote file ${fileLocation} to ${saveFilesToLocation+fileLocation}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
} else {
counter++;
}
}
Upvotes: 0
Views: 1597
Reputation: 4811
There seems to be something add in your code, but I can't be certain that it's a bug.
You compute a fileName and don't really use it to create the target file. Instead you just append the original path to the prefix saveFilesToLocation:
String fileName = fileLocation.substring(fileLocation.indexOf("/") + 1, fileLocation.length());
File file = new File(saveFilesToLocation+fileLocation);
This seems strange.
Then, if fileLocation contains directories that need to be created, then you need to mkdirs() them, otherwise you will get an error.
I will give you 2 snippets, one atht assumes that youir code above is buggy, te other that does what you do in a safer way, in idiomatic groovy.
First lets work with actual File objects instead if Strings:
private static File saveFilesToLocationDir = saveFilesToLocation as File
Version that supposes a bug in the above code:
private static String saveFilesToLocation = "/retrieve/";
private static File saveFilesToLocationDir = saveFilesToLocation as File
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation')) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
int firstSlash = fileLocation.indexOf("/") + 1
String fileName = fileLocation[firstSlash..-1]
File destination = new File(saveFilesToLocationDir, fileName)
destination.parentFile.mkdirs()
destination.withOutputStream { it << fileBytes }
println "$counter) Wrote file ${fileLocation} to ${destination.absolutePath}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
Version that does not use the created fileName (like you):
private static String saveFilesToLocation = "/retrieve/";
private static File saveFilesToLocationDir = saveFilesToLocation as File
CSVReader reader = new CSVReader(new InputStreamReader(balanceFile), SEPARATOR)
String[] nextLine
int counter = 0;
while ((nextLine = reader.readNext()) != null) {
counter++
if (nextLine != null && (nextLine[0] != 'FileLocation')) {
counter++;
try {
//Remove 0, only if client number start with "0".
String fileLocation = nextLine[0];
byte[] fileBytes = documentFileService.getFile(fileLocation);
if (fileBytes != null) {
int firstSlash = fileLocation.indexOf("/") + 1
String fileName = fileLocation[firstSlash..-1]
File destination = new File(saveFilesToLocationDir, fileLocation)
destination.parentFile.mkdirs()
destination.withOutputStream { it << fileBytes }
println "$counter) Wrote file ${fileLocation} to ${destination.absolutePath}"
} else {
println "$counter) UNABLE TO RETRIEVE FILE: $fileLocation";
}
} catch (Exception e) {
e.printStackTrace()
}
}
}
Upvotes: 1