Hi,
I'm currently trying to store all the text in text area into a txt file in directories using java. I know how 2 use mkdirs() to create directories, for example C:/d1/d2, but if i type
(new File(C:/d1/d2/sample.txt)).mkdirs()
it makes the txt file into a folder too. I tried with mkdir() too but failed. Is there any way to create d directories and txt file togather so i can store the information into it?
Below is my sample code:
public static void saveToTextFile(){
String strFilePath = "C:/d1/d2/sample.txt";
File f = new File(strFilePath);
try{
if(f.exists()){
f.delete();
}else{
f.mkdirs();
}
String s = textArea.getText();
FileWriter fw = new FileWriter(f);
fw.write(s);
fw.flush();
fw.close();
System.out.println("Data has been saved to C:/d1/d2/sample.txt");
}catch(IOException e){
System.out.println("Error during writing");
}
}
I really appreciate for any help!
Cass