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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T02:58:17+00:00 2026-06-03T02:58:17+00:00

there is a text file which we read from it , then we want

  • 0

there is a text file which we read from it , then we want to write it after some little changes to othere text file, but the question is that why it has different results if we use
System.out.println and when we use pwPaperAuthor.println?

the code is like :

package cn.com.author;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;

//input:"IndexAuthors1997-2010.txt"
//output:"PaperAuthor1997-2010.txt"
public class PaperAuthors {

    public static void main(String[] args) {

        BufferedReader brIndexAuthors = null;

        BufferedWriter bw = null;

        PrintWriter pwPaperAuthor = null;

        try {
            brIndexAuthors = new BufferedReader(new InputStreamReader(
                    new FileInputStream("IndexAuthors1997-2010.txt")));
            bw = new BufferedWriter(new FileWriter(new File(
                    "PaperAuthor1997-2010.txt")));
            pwPaperAuthor = new PrintWriter(new OutputStreamWriter(
                    new FileOutputStream("PaperAuthor1997-2010.txt")));
            /*
             * line = brIndexAuthors.readLine();
             * 
             * element=line.split("@"); String author=null; StringTokenizer st =
             * new StringTokenizer(element[1],","); while(st.hasMoreTokens()) {
             * author = st.nextToken(); pwPaperAuthor.println(element[0] + "+" +
             * author); //~i++; }
             */
            String line = null;
            String element[] = new String[3];
            String author = null;
            int i = 0;
            while ((line = brIndexAuthors.readLine()) != null) {
                element = line.split("#@");
                StringTokenizer st = new StringTokenizer(element[1], ",");

                int num=st.countTokens();

                while (st.hasMoreTokens()) {
                    author = st.nextToken();
                     pwPaperAuthor.println(element[0]+"@"+author+"@"+element[2]);
                    bw.write(element[0] + "@" + author + "@" + element[2]);
                    bw.newLine();
                    System.out.println(element[0]+"@"+author+"@"+element[2]);
                    i++;
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {

        }
    }
}

Ouput

if

System.out.println(element[0]+"@"+author+"@"+element[2]);------>620850@Henk Ern

if

pwPaperAuthor.println(element[0]+"@"+author+"@"+element[2]);
                        ----->620850@Henk Ernstblock@2001
  • 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-03T02:58:18+00:00Added an answer on June 3, 2026 at 2:58 am

    There’s no way you can read a file and write to it in the same loop, using the stream-based API. You will have to create a new file and copy everything that’s the same, adding what’s new. What you are doing now has unpredictable behavior. If you still want to read and write at the same time, you’ll have to use the RandomAccessFile, but that’s quite a bit more complicated.

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

Sidebar

Related Questions

How you can read a file (text or binary) from a batch file? There
Supposed a text file named name.txt . there are some contents as the following
I am using a text file to have some data there for later purposes.
I am reading in a String from a text file which contains a date
I have a program which reads data from 2 text files and then save
I have a huge line-separated text file and I want to make some calculations
I have a text file which was created using some Microsoft reporting tool. The
Here I have one file which contains some information, and I want to check
Is there a way to read a file's data but continue reading the data
I am running multiple APIs which load default properties from a text file. If

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.