I am trying to write some content into file inside my spring controller. Before writing i am creating the directory. But the file is not getting written. I am really confused. here is the code
public String storeText(String title, String description) {
String randomName = null;
try {
String baseDir = "C:/MyProjects/eclipse/DreamFolder/";
randomName = Long.toHexString(Double.doubleToLongBits(Math.random()));
String folderName = baseDir + randomName;
String fileName = folderName + "/textCon.txt";
File fileFolder = new File(folderName);
fileFolder.mkdir();
boolean exists = fileFolder.exists();
if (!exists) {
System.out.println("storeText folder does not exist");
}
System.out.println("storeText folderName - " + folderName);
System.out.println("storeText fileName - " + fileName);
File file = new File(fileName);
BufferedWriter out = new BufferedWriter(new FileWriter(file));
out.write(title);
out.newLine();
out.write(randomName);
out.newLine();
out.write(description);
out.close();
} catch (Exception ex) {
}
return randomName;
}
The last portion where i have this File file = new File(fileName); is what is having issues.
Exception exseems like a bad idea here...catch (Exception e) {}, you won't even know where your code is breaking, if it is breaking at all!