package
demo.file;
import
java.io.BufferedWriter;
import
java.io.File;
import
java.io.FileWriter;
import
java.io.IOException;
public
class
WriteToFileExample {
/**
* @param args
*/
public
static
void
main(String[] args) {
try
{
String content =
"This is the content to write into file"
;
File file =
new
File(
"C:\\users\\nopphanan7\\newfile.txt"
);
// if file doesnt exists, then create it
if
(!file.exists()) {
file.createNewFile();
}
FileWriter fw =
new
FileWriter(file.getAbsoluteFile());
BufferedWriter bw =
new
BufferedWriter(fw);
bw.write(content);
bw.close();
System.out.println(
"Done"
);
}
catch
(IOException e) {
e.printStackTrace();
}
}
}
ผลลัพธ์ที่ได้คือ(ให้ไปดูที่ C:\\users\\....\\newfile.txt)
Done
ไม่มีความคิดเห็น:
แสดงความคิดเห็น