Wednesday 7 | 11:40:PM
PHP Code:
// get which file you want to read and write
File file = new File("D://test.txt");
try {
// check whether the file is existed or not
if (file.exists()) {
// create a new file if the file is not existed
file.createNewFile();
}
// new a writer and point the writer to the file
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
// writer the content to the file
writer.write("I write something to a file.");
// always remember to close the writer
writer.close();
writer = null;
} catch (IOException e) {
e.printStackTrace();
}
try {
// new a reader and point the reader to the file
BufferedReader reader = new BufferedReader(new FileReader(file));
String line = "";
System.out.println("Content of the file\n================================");
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
reader = null;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}