Solved

Java read new line character

Posted on 2007-03-20
6
21,105 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 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