0

So I have this console app with line of Java code intended to modify the Book data inside txt file. User will prompted to enter the book ID of the book that is going to be modified and then just basically enter all the book details.

public void UpdatingBookData()
    {
        int bid; String booktitle; String author; String desc; String Alley;
        System.out.println("Enter Book ID: ");
        bid = sc.nextInt();
        System.out.println("Enter Book Title: ");
        booktitle = sc.next();
        System.out.println("Enter Book Author: ");
        author = sc.next();
        System.out.println("Enter Book Description: ");
        desc = sc.next();
        System.out.println("Enter Book Alley: ");
        Alley = sc.next();

        UpdateBook(bid, booktitle, author, desc, Alley);
    }

    public static void UpdateBook(int bid, String booktitle, String author, String desc, String Alley)
    {
        ArrayList<String> TempArr = new ArrayList<>();
        try
        {
            File f = new File("book.txt");
            FileReader fr = new FileReader(f);
            BufferedReader br = new BufferedReader(fr);
            String line;
            String[] lineArr;
            line = br.readLine();
            while(line != null)
            {
                lineArr = line.split(" ");
                if(lineArr[0].equals(bid))
                {
                    TempArr.add(

                             bid + " " +
                             booktitle + " " +
                             author + " " +
                             desc + " " +
                             Alley );
                }
                else
                {
                    TempArr.add(line);
                }
            }
            fr.close();
        }
        catch(IOException ex)
        {
            System.out.println(ex);
        }

        try
        {
           FileWriter fw = new FileWriter("book.txt");
           PrintWriter pw = new PrintWriter(fw);

           for(String str : TempArr)
           {
               pw.println(str);
           }
           pw.close();
        }
        catch(IOException ex)
        {
            System.out.println(ex);
        }

    }

but when I run it, I keep receiving this error

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:3181)
    at java.util.ArrayList.grow(ArrayList.java:261)
    at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:235)
    at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:227)
    at java.util.ArrayList.add(ArrayList.java:458)
    at lmsconsole.Administrator.UpdateBook(Administrator.java:438)
    at lmsconsole.Administrator.UpdatingBookData(Administrator.java:409)
    at lmsconsole.Administrator.adminPanel(Administrator.java:52)
    at lmsconsole.MainMenu.loginAdmin(MainMenu.java:68)
    at lmsconsole.MainMenu.MainPanel(MainMenu.java:45)
    at lmsconsole.LMSConsole.main(LMSConsole.java:24) 

Is it because of the ArrayList or what? Thanks in advance!

zdrasvutye
  • 21
  • 2
  • 8
  • The first thing to check is how the application is started? What memory settings assigned? Try adding -Xmx1024m – Minh Kieu May 10 '17 at 08:32
  • you're storing all lines in memory in spite of that you're going to change one line, you need to find a way to update a line in a file without having to rewrite its full content – niceman May 10 '17 at 08:37
  • Possible duplicate of [Modify a .txt file in Java](http://stackoverflow.com/questions/822150/modify-a-txt-file-in-java) – niceman May 10 '17 at 08:42
  • I'd go for `RandomAccessFile` :) – niceman May 10 '17 at 08:44
  • @niceman: That only works if the new data is exactly the same size as (or, with a little work, smaller than) the old data. Plus, you have the problem of determining the position of the line in the file. In general, it's difficult to update a text file without rewriting everything that comes after the point at which you make the modification. – Jim Mischel May 12 '17 at 13:48

0 Answers0