001package intermediate;
002
003import com.sandwich.koan.Koan;
004
005import java.io.*;
006import java.util.logging.Logger;
007
008import static com.sandwich.koan.constant.KoanConstants.__;
009import static com.sandwich.util.Assert.assertEquals;
010
011public class AboutFileIO {
012
013    @Koan
014    public void fileObjectDoesntCreateFile() {
015        File f = new File("foo.txt");
016        assertEquals(f.exists(), __);
017    }
018
019    @Koan
020    public void fileCreationAndDeletion() throws IOException {
021        File f = new File("foo.txt");
022        f.createNewFile();
023        assertEquals(f.exists(), __);
024        f.delete();
025        assertEquals(f.exists(), __);
026    }
027
028    @Koan
029    public void basicFileWritingAndReading() throws IOException {
030        File file = new File("file.txt");
031        FileWriter fw = new FileWriter(file);
032        fw.write("First line\nSecond line");
033        fw.flush();
034        fw.close();
035
036        char[] in = new char[50];
037        int size = 0;
038        FileReader fr = new FileReader(file);
039        size = fr.read(in);
040        // No flush necessary!
041        fr.close();
042        assertEquals(size, __);
043        assertEquals(new String(in), __);
044        file.delete();
045    }
046
047    @Koan
048    public void betterFileWritingAndReading() throws IOException {
049        File file = new File("file.txt");
050        file.deleteOnExit();
051        FileWriter fw = new FileWriter(file);
052        PrintWriter pw = new PrintWriter(fw);
053        pw.println("First line");
054        pw.println("Second line");
055        pw.close();
056
057        FileReader fr = new FileReader(file);
058        BufferedReader br = null;
059        try {
060            br = new BufferedReader(fr);
061            assertEquals(br.readLine(), __); // first line
062            assertEquals(br.readLine(), __); // second line
063            assertEquals(br.readLine(), __); // what now?
064        } finally {
065            // anytime you open access to a file, you should close it or you may
066            // lock it from other processes (ie frustrate people)
067            closeStream(br);
068        }
069    }
070
071    private void closeStream(BufferedReader br) {
072        if (br != null) {
073            try {
074                br.close();
075            } catch (IOException x) {
076                Logger.getAnonymousLogger().severe("Unable to close reader.");
077            }
078        }
079    }
080
081    @Koan
082    public void directChainingForReadingAndWriting() throws IOException {
083        File file = new File("file.txt");
084        PrintWriter pw = new PrintWriter(file);
085        pw.println("1. line");
086        pw.println("2. line");
087        pw.close();
088
089        StringBuffer sb = new StringBuffer();
090        // Add the loop to go through the file line by line and add the line
091        // to the StringBuffer
092        assertEquals(sb.toString(), "1. line\n2. line");
093    }
094}
095