Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6979569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:54:13+00:00 2026-05-27T17:54:13+00:00

I have a program that add persons to an arraylist. What I’m trying to

  • 0

I have a program that add persons to an arraylist. What I’m trying to do is add these persons to a text file as well but the program overwrites the first line so the persons get erased.

How do I tell the compiler to write at the next free line?

import java.io.*;
import java.util.*;
import javax.swing.JTextArea;

public class Logic {

File file;
FileWriter fw;
FileReader fr;
BufferedWriter bw;
ArrayList<Person> person;

public Logic() {
    try {
        file = new File("register.txt");

        if (!file.exists()) {
            file.createNewFile();
        }
    } catch (IOException e) {
    }

    person = new ArrayList<Person>();
}

// Add person
public void addPerson(String name, int tele) {
    person.add(new Person(name, tele));
    savePerson(name, tele);
}

// Save person to external file
public void savePerson(String name, int tele) {
    try {
        fw = new FileWriter(file.getName());
        bw = new BufferedWriter(fw);
        String tel = Integer.toString(tele);
        bw.write(name + "\t" + tel);
        bw.newLine();
        bw.close();

    } catch (Exception e) {
        System.out.println("skrev inte ut med buffered");
    }
}

// Går in i alla objekt av klassen Person och skriver ut toString i
// textArean
public void visaAlla(JTextArea textRuta) {
    textRuta.setText("");
    // for(Person p:person)
    // {
    // textRuta.append(p.toString());
    // }

    try {
        fr = new FileReader(file.getName());
        BufferedReader in = new BufferedReader(fr);
        String str;

        while ((str = in.readLine()) != null) {
            textRuta.append(str);
        }

    } catch (Exception e) {
        System.out.println("gickcinte ");
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T17:54:14+00:00Added an answer on May 27, 2026 at 5:54 pm

    FileWriter takes an optional boolean argument which specifies whether it should append to or overwrite the existing content. Pass in true if you want to open the file for writing in append mode.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that takes a text file with values for example: 20
I have a simple program that lets you add, edit, list, search and delete
I have a program that runs fine in DEBUG mode but in RELEASE mode
I have a program that is just one big for loop. I first have
I have a program that does text processing on a html formatted document based
I have a program that I add into explorer's right click menu. When I
I have a program that reads from a txt file line by line splits
I have program that has a variable that should never change. However, somehow, it
I have program that runs fast enough. I want to see the number of
I have a program that spits out both standard error and standard out, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.