Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Rereading a Text File in Java

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Rereading a 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?

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

    Thank you in advance

    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;
     
                    }
                }
            }
        }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Rereading a Text File in Java

    Why are you re-reading the file rather than just using the array in which the file contents have already been stored?

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rereading a Text File in Java

    If the user decides to add in an entry, I need to update the file contents, so I reread the file to get the new entry added (if that makes sense?)

    --- Update ---

    And I don't think that I can append the array, and as I just learned how to use the IO, I'm not entirely sure how to go about this issue

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Rereading a Text File in Java

    I suggest using one array to hold the file contents, another array to hold the user's changes, and then write both arrays to the file when the program exits. As data is needed while the program is running, access it from the arrays, not the file.

  5. The Following User Says Thank You to GregBrannon For This Useful Post:

    JoaoDDUarte (December 17th, 2013)

Similar Threads

  1. How do you store attributes for a java text file in the file itself?
    By GoodbyeWorld in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 15th, 2013, 08:22 PM
  2. Java code Split text file into multiple text file
    By jayraj in forum What's Wrong With My Code?
    Replies: 26
    Last Post: April 19th, 2013, 06:14 AM
  3. How do I copy a simple text-file to a new text-file?
    By get703 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 5th, 2012, 10:59 PM
  4. Conversion of any image file to text file in Java...'
    By suyog53 in forum File I/O & Other I/O Streams
    Replies: 17
    Last Post: September 23rd, 2012, 08:37 AM
  5. java program to copy a text file to onother text file
    By francoc in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: April 23rd, 2010, 03:10 PM

Tags for this Thread