package Files; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; // examples of reading and writing text files with multiple lines public class TextFileExample { // writing and a multi line text file from a list public static void textLines(List lines) { try { // create object that knows how to write to a file // use buffers for efficiency // throws IOException PrintWriter fileout = new PrintWriter(new BufferedWriter(new FileWriter(new File("filename.txt")))); // actually writing text to file for (String line : lines) { fileout.println(line); } // done with file fileout.close(); } catch (FileNotFoundException ex) { // subclass of IOException System.out.println("could not find file to read"); } catch (IOException ex) { System.out.println("could not access files to write"); } } // make a stream and write some text to file public static void fileWrite() { // dealing with files is dangerous try { // create stream that knows how to write to the file // PrintWriter has println // BufferedWriter uses a buffer to make output more efficient // FileWriter does actual writing PrintWriter firstWriter = new PrintWriter( new BufferedWriter( new FileWriter(new File("out.txt")))); // we can write as many lines to the file as we need to // print, println, printf all work firstWriter.println("text goes in the file"); firstWriter.println("more text"); // done, so release the file firstWriter.close(); } catch (FileNotFoundException ex) { System.out.println("could not create / replace existing file"); } catch (IOException ex) { System.out.println("could not access file to write"); } } // write to file, appending public static void fileWriteAppend() { try { // PrintWriter has println // BufferedWriter uses a buffer to make output more efficient // FileWriter does actual writing, allows append with this constructor boolean append = true; // do I want to append (true) or replace (false) PrintWriter smartWriter = new PrintWriter( new BufferedWriter( new FileWriter(new File("out.txt"), append))); smartWriter.println("look, even More text"); smartWriter.close(); } catch (FileNotFoundException ex) { System.out.println("could not find file"); } catch (IOException ex) { System.out.println("could not access file to write"); } } // read multiple lines from a file, just print them out public static void fileRead() { try { // scanner has various next methods // BufferedWriter uses a buffer to make output more efficient // FileWriter does actual reading Scanner scan = new Scanner( new BufferedReader( new FileReader(new File("out.txt")))); // check if there is still an unread line in the file while (scan.hasNext()) { // read the current line -- lines of file are already strings String line = scan.nextLine(); // all we're doing with this line from the file is printing it // probably could do more interesting things with it... System.out.println(line); } scan.close(); } catch (IOException ex) { System.out.println("could not access file to read"); } } // sometimes we want to hold onto the contents of a file // return all the lines from the file in a list public static List fileGetContents() { // to hold lines of file as we read them in List linesOfFile = new ArrayList<>(); try { // scanner has various next methods // BufferedWriter uses a buffer to make output more efficient // FileWriter does actual reading Scanner scan = new Scanner( new BufferedReader( new FileReader(new File("out.txt")))); // check if there is still an unread line in the file while (scan.hasNext()) { // read the current line -- lines of file are already strings String line = scan.nextLine(); // put it in the arraylist linesOfFile.add(line); } scan.close(); } catch (IOException ex) { System.out.println("could not access file to read"); } return linesOfFile; } public static void main(String[] args) { fileWrite(); fileWriteAppend(); fileRead(); List fileIn = fileGetContents(); System.out.println(); System.out.println("File Contents:"); int ln = 1; for (String line : fileIn) { System.out.println(ln + " " + line); ln++; } } }