• Post Reply Bookmark Topic Watch Topic
  • New Topic

File Download - Format Problem

 
Ashwin B Philar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!!
I have a java program that reads the content of a file located on a remote UNIX box and copies the content to a local txt file.
The problem is that the entire file comes in as a single line.
I am using the following to read the file
in = new BufferedReader (new InputStreamReader (content));
while ((inputLine = in.readLine()) != null)
{
out.write(inputLine);
}//end while
The actual file contains multiple lines, but the locally created file contains all data in just one line.
Server is UNIX and Client is Windows 2000 Application is in Java
Eagerly awaiting help
Regards,
Ashwin Philar
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each line you read you should end with a "\\n"
/Rene
[ July 03, 2002: Message edited by: Rene Larsen ]
 
Ashwin B Philar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rene,
Thanks for the guidance. But, shouldn't lineRead() just read a line with the newline character, why is it excluding that new line character when reading, I completele fail to understand?
Thanks again for your help.
Regards,
Ashwin Philar
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are reading one line from start to \r or \n - then you write to the output one line after each other without \r or \n therefore you'll get all line in one line. Therefore you need to add the newline '\n' by your self.
/Rene
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is what the javadoc says:
readLine
public String readLine() throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs

/Rene
[ July 03, 2002: Message edited by: Rene Larsen ]
[ July 04, 2002: Message edited by: Jim Yingst ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I fixed some overly-long lines in Rene's post which were affecting formatting of the rest of the page.
A problem with readLine() here is that after you've read the line, you have no way of knowing if the line terminator was \n, \r\n, or \r\n. So you may change the file in small ways by replacing \r\n with \n, for example.
Ashwin, it doesn't look like there's really any need for readLine() - or even a Reader - in this code. You can use a BufferedInputStream and BufferedOutputStream, with methods read(byte[]) and write(byte[], int) to read and write many bytes at a time without worrying about line terminators. You could also use similar Reader/Writer methods if you prefer, but if you're just copying files without needing to look at the contents as Strings or chars, there seems little need for those classes.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!