Solved

Java read new line character

Posted on 2007-03-20
6
21,101 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login jsp example 24 102
JAVA API design with micro service cloud in mind 1 109
dao vs facade design patterns 2 68
Html split(text) 2 46
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

739 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