summaryrefslogtreecommitdiff
path: root/src/filebuffer.java
blob: 1ed0406963bb7123214c90c176a57bd093f08144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package jed;

import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

public class filebuffer {

    public ArrayList<String> buffer;
    public String fileName;
    public boolean hasName;

    public filebuffer(String fn, boolean hn) {
        buffer = new ArrayList<String>(); // Arraylist that stores every line of a file in strings
        hasName = hn;
        if (hasName)
            fileName = fn;
    }

    /* printFile: prints strings from file arraylist, if num is true it includes line numbers */
    public void printFile(boolean num) {
        for (int i = 0; i < buffer.size(); i++) {
            if (num)
                System.out.printf("%d %s\n", i+1, buffer.get(i));
            else
                System.out.println(buffer.get(i));
        }
    }

    /* createFile: creates a file, only to be called by writeFile */
    private int createFile() {
        try {
            File f = new File(fileName);
            if (f.createNewFile())
                return 1;
            return 0;
        } catch (IOException e) {
            return -1;
        }
    }

    /* writeFile: reads strings from file arraylist and prints them into a file */
    public int writeFile() {
        if (!hasName) {
            System.out.println("?");
            return 1;
        }
        int err = createFile();
        if (err == -1)
            return 1;
        try {
            FileWriter fw = new FileWriter(fileName);
            PrintWriter pw = new PrintWriter(fw);
            for (int i = 0; i < buffer.size(); i++)
                pw.println(buffer.get(i));
            pw.close();
        } catch (IOException e) {
            return 1;
        }
        return 0;
    }

    /* readFile: opens a file and stores each line in a string arraylist called file */
    public int readFile() {
        try {
            File fileObj = new File(fileName);
            Scanner fileReader = new Scanner(fileObj);
            while(fileReader.hasNextLine())
                buffer.add(fileReader.nextLine());
            fileReader.close();
        } catch (FileNotFoundException e) {
            createFile();
        }
        return 0;
    }
}