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
amit_vickey
  • 45
  • 1
  • 9
  • Possible duplicate of [Create CSV file using java](https://stackoverflow.com/questions/17837009/create-csv-file-using-java) – sauumum Jun 17 '18 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](https://docs.oracle.com/javase/7/docs/api/java/io/File.html#File(java.io.File,%20java.lang.String)). Or, ever better, use the [`Path` API](https://docs.oracle.com/javase/tutorial/essential/io/pathOps.html) rather than the mostly obsoltete `File` API. – Boris the Spider Jun 17 '18 at 19:38

2 Answers2

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();
Timur Levadny
  • 510
  • 4
  • 13
-1

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