0

I have to create a new CSV file and write data to that file. Here's my code snippet

String path = "D:\\cradius-data-local\\files\\webapps\\vcm";
String javaPath = path.replace("\\", "/");
String tempFolderPath = "zips"+File.separator+dto.getFileName();
File csvFile = null;
CSVWriter csvWriter = null;
csvFile = new File(javaPath+File.separator+tempFolderPath+File.separator+dto.getFileName()+".csv");
if(!csvFile.getParentFile().exists()){
    csvFile.getParentFile().mkdirs();
}
csvWriter = new CSVWriter(new FileWriter(csvFile), ',');

But when I'm trying to execute the above code, I'm getting the below error

java.io.IOException: The system cannot find the path specified

The location where I want to create my new csv file is

javaPath+File.separator+tempFolderPath+File.separator+dto.getFileName()+".csv"

Which evaluates to

D:/cradius-data-local/files/webapps/vcm\ocr_zips\AMIT_COOL_123\AMIT_COOL_123.csv
2
  • Possible duplicate of Create CSV file using java Commented Jun 17, 2018 at 12:43
  • 1
    D:/cradius-data-local/files/webapps/vcm\ocr_zips\AMIT_COOL_123\AMIT_COOL_123.csv mixes both forward and backward slashes. You don't need to do any replacement. Let Java do the work for you. Or, ever better, use the Path API rather than the mostly obsoltete File API. Commented Jun 17, 2018 at 19:38

2 Answers 2

1

As Boris wrote in the comment, you can let Java do the work for you. Consider following snippet:

String path = "D:\\cradius-data-local\\files\\webapps\\vcm";
File file = Paths.get(path, "zips", dto.getFileName() + ".csv").toFile();
Sign up to request clarification or add additional context in comments.

Comments

-1

use this:
String javaPath = path.replace("\\", "\"); // Create a new variable
instead of:
String javaPath = path.replace("\\", "/"); // Create a new variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.