Solved

Java read new line character

Posted on 2007-03-20
6
21,108 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

617 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