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

  • Home
  • SEARCH
  • 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 8663657
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T17:02:28+00:00 2026-06-12T17:02:28+00:00

I have a program reading from a text file (currently 653 lines long) all

  • 0

I have a program reading from a text file (currently 653 lines long) all separated by a comma. But when I go to save the file to a new location, it only saves 490 lines. It also seems that the last line in the newly created text file is cut in half. Any ideas on what might be the problem?

Here is the code that I used to open and sort the data in the list:

try {
    scanner = new Scanner(file);

    // Put the database into an array and 
    // Make sure each String array is 13 in length
    while (scanner.hasNext()) {
        line = scanner.nextLine();
        word = line.split(",");

        if (word.length < 13) {
            String[] word2 = {"","","","","","","","","","","","",""};
            for (int i = 0; i < word.length; i++) {
                word2[i] = word[i];
            }
            dataBaseArray.add(word2);
        }
        else {
            dataBaseArray.add(word);
        }
    }
}
catch (FileNotFoundException exc) {
    JOptionPane.showMessageDialog(this, "File cannot be found.", "error finding file", JOptionPane.ERROR_MESSAGE);
}

// Splitting the database into vacant numbers/dead lines/vacant cubicles
for (int i = 0; i < dataBaseArray.size(); i++) {
    if (dataBaseArray.get(i)[8].equals("VACANT")) {
        vacantNums.add(dataBaseArray.get(i));
    }
    else if (dataBaseArray.get(i)[4].equals("DEAD")) {
        deadLines.add(dataBaseArray.get(i));
    }
    else if (dataBaseArray.get(i)[6].equals("") && dataBaseArray.get(i)[7].equals("")) {
        vacantCubs.add(dataBaseArray.get(i));
    }
    else if (dataBaseArray.get(i)[7].equals("")) {
        people.add(dataBaseArray.get(i));
    }
    else {
        people.add(dataBaseArray.get(i));
    }
}

// Resetting the DB Array to put the values back in it
dataBaseArray = new ArrayList<>();

// Ordering the arrays I want them to appear in the list
// Orering the people to appear in alphabetical order
Collections.sort(people, new Comparator<String[]>() {

    @Override
    public int compare(String[] strings, String[] otherStrings) {
        return strings[7].compareTo(otherStrings[7]);
    }
});

// Put the people in the DB Array
for (int i = 0; i < people.size(); i++) {
    dataBaseArray.add(people.get(i));
}

// Put the vacant numbers in the AB Array
for (int i = 0; i < vacantNums.size(); i++) {
    dataBaseArray.add(vacantNums.get(i));
}

// Put the vacant cubicles in the AB Array
for (int i = 0; i < vacantCubs.size(); i++) {
    dataBaseArray.add(vacantCubs.get(i));
}

// Put the dead lines in the AB Array
for (int i = 0; i < deadLines.size(); i++) {
    dataBaseArray.add(deadLines.get(i));
}

list = new String[dataBaseArray.size()];

// Add the DB Array to the list
for (int i = 0; i < list.length; i++) {
    if (dataBaseArray.get(i)[8].equals("VACANT")) {
        list[i] = "VACANT";
    }
    else if (dataBaseArray.get(i)[4].equals("DEAD")) {
        list[i] = "DEAD";
    }
    else if (dataBaseArray.get(i)[6].equals("") && dataBaseArray.get(i)[7].equals("")) {
        list[i] = "Vacant Cubicle";
    }
    else if (dataBaseArray.get(i)[7].equals("")) {
        list[i] = dataBaseArray.get(i)[6];
    }
    else {
        list[i] = dataBaseArray.get(i)[7] + ", " + dataBaseArray.get(i)[6];
    }
}

// Populate the list
lstAdvance.setListData(list);

Here is what I used to save the file:

try {
    saveFile = new FileWriter("Save Location");
    String newLine = System.getProperty("line.separator");

    for (int i = 0; i < dataBaseArray.size(); i++) {
        for (int j = 0; j < dataBaseArray.get(i).length; j++) {
            saveFile.append(dataBaseArray.get(i)[j] + ",");
        }
        saveFile.append(newLine);
    }
}
catch (IOException exc) {
    JOptionPane.showMessageDialog(this,"error", "error", JOptionPane.ERROR_MESSAGE);
}
  • 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-06-12T17:02:29+00:00Added an answer on June 12, 2026 at 5:02 pm

    Writing to a file is buffered. You have to close() or flush() your writer (saveFile) at the end of writing.

    Even better: you should do close() on your writer in the finally block.

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

Sidebar

Related Questions

I have a Perl program that is reading html tags from a text file.
I have a Java program that reads lines from text file. I want to
I have a C++ program that is reading in info from a file, that
I have a program which reads data from 2 text files and then save
The program is basically about reading from a text file, storing the current data
I currently have a simple database program that reads keys in from a text
I have written a program in Java to read in a text file of
I have a large text file. Each time my program runs, it needs to
Anyone got anything about reading a sequential number from text file per line and
In my program, I am reading a series of text files from the disk.

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.