import java.io.*; public class TextFileIO{ public static void main(String[] args) { // ************************************************************************* // READING FROM COMMAND LINE String commandLineInput; BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in)); try { commandLineInput = bufferedReader.readLine(); } catch (IOException e) { e.printStackTrace(); System.exit(1); } // **************************************************************** // READING FROM FILE LINE BY LINE String readLine; try{ BufferedReader reader = new BufferedReader(new FileReader ("input.txt")); try{ readLine=reader.readLine(); //now you have the first line of the file in readLine variable. while(readLine!=null){ // in this while loop you read the rest of the file // line by line till you get to the end of the file // you can do what you want to do with the text of the file here // e.g., by sending the text as an argument of a method // e.g., doSomeProcessOn(readLine); readLine= reader.readLine(); } }catch(IOException e){ e.printStackTrace(); } }catch(FileNotFoundException e){ e.printStackTrace(); } // **************************************************************** // WRITING TO FILE LINE BY LINE String outputString = "Whatever you want to write in the file in a single line"; try { BufferedWriter outputFile = new BufferedWriter(new FileWriter("output.txt")); outputFile.write(outputString); outputFile.newLine();//going to the next line. outputFile.close(); // It is important to close the output file. }catch(IOException e){ e.printStackTrace(); } } }