Search code examples
javafileiobufferedreaderreset

Rereading a small text file in Java


Due to my inexperience in Java (I've only been learning it for the past two months for school), I have come across an issue.

I was making this "insulting machine" program for one of my friends, as a joke. The program refers to a text file and finds how many lines it has. It then generates a number from 1-the max line number and stores all the text in an array and prints out the text in that line number. The user can also create insults, and it updates the text file with the new insult.

When I now run the insulting part of it, (I'm using a BufferedReader, maybe there is a simpler way?) and its position is still at the bottom of the text file, and I cannot find a way to reread the text file again to update the array. If I go back to the insulting part, it prints out NULL as I've reached the end of the file. Any ideas on how to fix this please?

Just FYI, to reply to create a new BufferedReader, the user can go back and forth of the creation and the core program, so I'd need infinite BufferedReaders

(I do realize my code may not be the most efficient, I've only started to program)

import java.util.Scanner;
import java.io.*;
import java.util.Random;
public class InsultingMachine{
    public static void main(String args[]) throws IOException{
        File insult = new File("insults.txt");
        boolean exist = insult.exists();

        if(exist == false){
            System.out.println("The file does not exist, creating the file...?");
            PrintWriter writer = new PrintWriter("insults.txt", "UTF-8");
            writer.close();
            System.out.println("Done");
        }

        //objects
        Scanner input = new Scanner(System.in);
        Scanner inputIn = new Scanner(System.in);
        Scanner inputIn2 = new Scanner(System.in);
        Random ranGen = new Random();
        BufferedReader reader = new BufferedReader(new FileReader("insults.txt"));
        BufferedReader reader2 = new BufferedReader(new FileReader("insults.txt"));
        BufferedWriter wr = new BufferedWriter(new FileWriter("insults.txt", true));

        //vars
        int i, ranInt;
        int lineNum = 0;
        int lineNum2 = 0;
        int counter = 0, counter2 = 0, counter3 = 0, menuCounter = 0;
        String outputting;
        String s1, s2;

        while (menuCounter == 0){
            System.out.println("Hello, please select an option from the menu: ");
            System.out.println("(1) INSULTS");
            System.out.println("(2) Create insults");
            System.out.println("(3) Exit");

            i = input.nextInt();

            switch(i){
                case 1:
                    //Establish Line Number

                    while(reader.readLine() != null){
                        lineNum++;
                    }
                    //store the lines of the text file
                    String L[] = new String[lineNum];

                    lineNum2 = 0;
                    while(lineNum2 < lineNum){
                        L[lineNum2] = reader2.readLine();
                        lineNum2++;
                    }

                    System.out.println("You may start you fool...Enter an insult --type exit to exit-- :");
                    while (counter == 0){
                        s2 = inputIn.nextLine();

                        if(s2.equalsIgnoreCase("exit")){
                            break;
                        }

                        //generate number from 1 to max line number
                        ranInt = ranGen.nextInt(lineNum);
                        if (ranInt == 0)
                        {
                            ++ranInt;
                        }

                        System.out.println(L[ranInt]);
                    }
                    break;

                case 2:
                    while(counter3 == 0){
                        System.out.println("Please enter the insult you would like to add --type exit to exit-- : ");
                        s2 = inputIn2.nextLine();

                        if(s2.equalsIgnoreCase("exit"))
                        {
                            wr.close();
                            break;
                        }
                        wr.newLine();
                        wr.write(s2);
                    }
                    break;

                case 3:
                    menuCounter++;
                    break;
                default:
                    System.out.println("Please enter a correct value...");
                    break;
            }
        }
    }
}

Solution

  • For reading files by line use Apache Commons IO Library

    apache commons io

    It has FileUtils Class. FileUtils

    It has method readLines(File f) readLines(File f)

    This method will read the file and return you the List of lines and then you can do whatever you want with that List