Solved

Java read new line character

Posted on 2007-03-20
6
21,085 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

930 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

12 Experts available now in Live!

Get 1:1 Help Now