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 8944479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:01:58+00:00 2026-06-15T12:01:58+00:00

I am trying to update a file using Java with new data. Let ‘s

  • 0

I am trying to update a file using Java with new data. Let ‘s say I have a txt file where I have saved the following data:

id     grade
3498   8
2345   9
5444   7
2222   5

So I am trying to update the grade, depending the id the user has typed, but the new (updated) file has the type that follows:

id   grade3498   62345   95444   72222   5

and so on….

I can t find the reason why this is not working, I guessed it has something to do with not adding a new line while re-writing the data, but even if I add new line character (“\n”) in outobj.write(fileContent.toString()); nothing changes.

Here is a snippet of my code :

public String check(int num) throws RemoteException
{
    String textinLine;
    String texttoEdit;
    File file=new File ("c:\\students.txt");
    FileInputStream stream = null;
    DataInputStream in =null;
    BufferedReader br = null;
    try
        {
        stream = new FileInputStream(file);
        in =new DataInputStream(stream);
        br = new BufferedReader(new InputStreamReader(in));
        StringBuilder fileContent = new StringBuilder();
        if ((num>0) && (num<6001))
            {
            while ((textinLine=br.readLine())!=null)
                {
                texttoEdit=Integer.toString(num);
                    System.out.println(textinLine);
                    String[] parts = textinLine.split(" ");
                    if (parts.length>0)
                        {
                        if (parts[0].equals(texttoEdit))
                            {
                            int value =       Integer.parseInt(parts[1]);
                            value-=2;
                            String edit=Integer.toString(value);
                            String newLine = "\n"+parts[0]+"    "+edit+"\n";
                            msg="You can pass2";
                            fileContent.append(newLine);
fileContent.append("\n");               }
                        else
                            {
                            fileContent.append(textinLine);
fileContent.append("\n");
                            }
                        }
                }
            }
        in.close();
        FileWriter fstream = new FileWriter(file);
        BufferedWriter outobj = new BufferedWriter(fstream);
        outobj.write(fileContent.toString());
        outobj.close();
        }
    catch (FileNotFoundException e)
        {
        e.printStackTrace();
        }
    catch (IOException e)
        {
        e.printStackTrace();
        }

Finally, let me say that the new file is rightly edited, which means that if the user enters the id 3498, the grade value would change to 8-2=6, but the new file will be in a single line, as I explained before.

  • 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-15T12:02:01+00:00Added an answer on June 15, 2026 at 12:02 pm

    On some OS (typically Windows) you need to use \r\n for a new line. Even better, you can use:

    String newLine = System.getProperty("line.separator");
    

    for the line separator which will adjust depending on the platform it is run on.

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

Sidebar

Related Questions

I'm trying to update a random-access binary file using the std::iostream interface with separate
Q: I'm trying to update a file in place, by using fopen mode r+
I'm trying to update (add/modify files) an existing JAR file, and this code (using
I'm trying to write a Java app that imports a data file. The process
I have been trying to encrypt a PDF using Java. So far I can
I'm trying to update an Excel file from Outlook (Office 2010). How do I
I am trying to update the app.config file at runtime. I get the error
I am trying to update a database with values from a csv file, the
I am trying to update a child node of an XML file IE Changing
I am trying to use an xmlpoke task to update a VS Project File

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.