File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes unable to preserve newline Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "unable to preserve newline" Watch "unable to preserve newline" New topic
Author

unable to preserve newline

sv.srikrishna kitti
Greenhorn

Joined: Feb 14, 2010
Posts: 9
Hi All,
I have file which contains data in EBCIDC format.The data contains both english and arabic characters.I have to convert into UTF-8.Iam able to convert the data successfully into unicode.But iam losing out newline.All the charaters are appearing in the single line after conversion to unicode.Please help me.What is wrong with this code.

import java.io.*;
class FileRead1
{
public static void main(String args[])
{
try{
// Open the file that is the first
// command line parameter

FileInputStream fstream = new FileInputStream("C:\\sabb.txt");

File outfile = new File( "C:\\100.txt" );

FileOutputStream fout = new FileOutputStream( outfile);


// Get the object of DataInputStream

InputStreamReader is = new InputStreamReader( fstream, "cp420");

//DataInputStream in = new DataInputStream(fstream);

BufferedReader reader = new BufferedReader(is);

String strLine;
//Read File Line By Line
while ((strLine = reader.readLine()) != null) {

byte[] output =strLine.getBytes("UTF-8" );
fout.write(output);
fout.close();
}

}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Please Use Code Tags.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Reading the documentation might help:

http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html#readLine%28%29
sv.srikrishna kitti
Greenhorn

Joined: Feb 14, 2010
Posts: 9


I tried this way also.Still getting the same problem .All the converted text appears in a single line.Please help me.


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

Both your versions contain ugly hacks or sloppy mistakes. Here's what you should do:

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Except that OutputStreamReader should be OutputStreamWriter
sv.srikrishna kitti
Greenhorn

Joined: Feb 14, 2010
Posts: 9
Thank you very much guys
sv.srikrishna kitti
Greenhorn

Joined: Feb 14, 2010
Posts: 9

Now newline issue is resolved but some arabic characters are appearing as ?.please help me
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14350
    
  22

sv.srikrishna kitti wrote:Now newline issue is resolved but some arabic characters are appearing as ?.please help me

The reason why that happens probably doesn't have anything to do with your program, but with the software that you use to display the output text. Where and how are you displaying the output? Does the font that is being used contain arabic characters? If not, they might appear as '?'. Does the software that you use to display the output understand UTF-8 encoding?

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
sv.srikrishna kitti
Greenhorn

Joined: Feb 14, 2010
Posts: 9
I use microsoft word with the font windows arabic for display purporse.I don't think it is something to do with software because when i was reading byte by byte as mentioned in previous posts all the arabic characters were getting displayed properly but new line was the problem.But now newline issue is resolved but now having problem with some arabic characters.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unable to preserve newline