Solved

Java read new line character

Posted on 2007-03-20
6
21,082 Views
Last Modified: 2013-11-23
I want read a text file and output to another text file
This is my code  
      FileInputStream s = new FileInputStream(fileName);
      DataInputStream dis = new DataInputStream(s);
      InputStreamReader r = new InputStreamReader(s);

      FileOutputStream fos = new FileOutputStream(output_file);
      PrintWriter pw = new PrintWriter(fos, true);

      data = reader.readLine();

      while (data!=null)
     {
       pw.write(data);
      data = reader.readLine();
    }

my problem now is that when I open the new output file, there will be just one line without any new line ...

so, instead of using  pw.write(data+"\n")   for every new line, can I use another code?
0
Comment
Question by:goldragon
6 Comments
 
LVL 1

Expert Comment

by:clim1219
ID: 18762060
Hi goldragon,

Instead of using pw.write(data+"\n") you can use pw.println( data ); like:

while (data!=null) {
    pw.println( data );
    data = reader.readLine();
}


an alternative would be to use strictly streams without the writer like:

        InputStream inputStream = new FileInputStream( "test.txt" );
        FileOutputStream outputStream = new FileOutputStream( "test_output.txt" );

        int available = inputStream.available();
        while( available != 0 ) {
            outputStream.write( inputStream.read() );
            available = inputStream.available();
        }

this way the \n is read as is and written to the output stream properly.



Cheers!

Chris
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 18762352
Use
 
pw.println(data);
 
instead of
 
pw.write(data);
0
 

Author Comment

by:goldragon
ID: 18764090
what I actually want to do is to manipulate the string first before it is put on a new output file.

  data = reader.readLine();
  while (data!=null)
   {
    //break the data into characters including the new line...  
    //then write the result into the output file
//  pw.write(data);
      data = reader.readLine();
    }

what I find in readLine() is that it only read string, but exclude the new line (\n) character.
so can I actually modify the "data = reader.readLine();" part with something else that can include the new line character?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Accepted Solution

by:
Ajay-Singh earned 50 total points
ID: 18764156
> what I find in readLine() is that it only read string, but exclude the new line (\n) character.

Thats the expected behavior - api says so.

so can I actually modify the "data = reader.readLine();" part with something else that can include the new line character?
> Use data = data + "\n";
> If you *only* want to use new line , i suggest to use println method
0
 
LVL 20

Expert Comment

by:gatorvip
ID: 18766090
I would suggest using the line separator property

String NEWLINE = System.getProperty("line.separator");
data = reader.readLine();

 while (data!=null)
   {
    //break the data into characters including the new line...  
    //then write the result into the output file
//  pw.write(data+ NEWLINE);
      data = reader.readLine();
    }

 it seems like you want on one hand to process the text without the EOL character and on the other hand to output the text including the EOL character without any additional "work"
0
 

Author Comment

by:goldragon
ID: 18769264
@ajay-singh : yup, thanks for your suggestion
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now