Java I.O Basics
package Paths;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Hello World");
Path path = Paths.get("C:\\Users\\u0162479\\Code\\Java\\FileIO\\tmp\\art.txt");
try {
Files.createFile(path);
} catch (IOException e) {
e.printStackTrace();
}
/* Deleting File */
// try {
// Files.deleteIfExists(path);
// } catch (IOException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
}
}
package writer;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
System.out.println("Outputing File");
Date date = new Date();
SimpleDateFormat dateFormat = new SimpleDateFormat("MM_dd_yy");
String date_filename = dateFormat.format(date) + ".txt";
BufferedWriter buffWriter = null;
try {
buffWriter = new BufferedWriter(new FileWriter(date_filename, true));
buffWriter.write("HelloAayush");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
try {
buffWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
package reader;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String filename = "TestFile.txt";
BufferedReader buff_reader = null;
try {
buff_reader = new BufferedReader(new FileReader(filename));
String line;
while ( (line = buff_reader.readLine()) != null ) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
if ( buff_reader != null ) {
try {
buff_reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}