summaryrefslogtreecommitdiff
path: root/src/filebuffer.java
diff options
context:
space:
mode:
authorJacob McDonnell <jacob@simplelittledream.com>2021-12-08 21:52:01 -0500
committerJacob McDonnell <jacob@simplelittledream.com>2021-12-08 21:52:01 -0500
commit8d37ce9bf25ccd37e50e9f766bb8df710e855217 (patch)
tree6fad3a5759c560ea15d11d8b2dcb57ba9d1a8886 /src/filebuffer.java
parent7a84f94823aae2eb180b035e308e75c678fc434c (diff)
Rewrote Jed to have multiple classes and cleaned up the code
Diffstat (limited to 'src/filebuffer.java')
-rw-r--r--src/filebuffer.java76
1 files changed, 76 insertions, 0 deletions
diff --git a/src/filebuffer.java b/src/filebuffer.java
new file mode 100644
index 0000000..1ed0406
--- /dev/null
+++ b/src/filebuffer.java
@@ -0,0 +1,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;
+ }
+} \ No newline at end of file