• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

I/O Eating Data

 
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,
Hey!
I have made a code for copying one file to another using I/O reader and writer. Apart from that, I am also making the use of BufferedReader and BufferedWriter. When I use the "int read()" method of BufferedReader, and copy the file, the source file and the destination file both have the same size in bytes as shown by "windows properties facility". But if I use the
"String readLine()" method of BufferedReader, the size of the destination file and the source file are different. The destination file is small by some 300 bytes or so from the source file. Is this thing normal, or is there any data loss in the transfer process.
Thanks in Advance!!
Bye,
Tualha Khan
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post you source code, which causes the problem?
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is the first code in which I am using the "int read()" based method:-
import java.io.*;
public class iokam13
{
public static void main(String args[]) throws IOException
{
FileReader fr=new FileReader(args[0]);
BufferedReader br=new BufferedReader(fr, 1000);
FileWriter fw=new FileWriter(args[1]);
BufferedWriter bw=new BufferedWriter(fw, 1000);

int ii;

while((ii=br.read())!=-1)
{
bw.write(ii);
}
br.close();
bw.close();
System.out.println("Copy Process Complete");
}
}
**********************************************
Below is the Second code in which I am using the "String readLine()" method:-

import java.io.*;
public class iokam14
{
public static void main(String args[]) throws IOException
{
FileReader fr=new FileReader(args[0]);
FileWriter fw=new FileWriter(args[1]);
BufferedReader br=new BufferedReader(fr, 1000);
BufferedWriter bw=new BufferedWriter(fw, 1000);

String ss;
while((ss=br.readLine())!=null)
{
bw.write(ss);
}
br.close();
bw.close();
System.out.println("Copy Process Complete");
}
}

*************************************
In both the codes, I used an 11.7 MB .rtf file as an source argument. I get the file size of the destination file different for both the approaches. In case of the "int read()" method based code, I get the size as 11.7 MB, and in the code using "String readLine()" method, I get the size as 11.4 MB. Now, what happened to those 0.3 MB of data.
Thanks in advance!!
Bye,
Tualha Khan
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think String readLine() is supposed to be used on text files only, not for .rtf file which is binary. If you use readLine() on binary file, it will try to interpret the file as chars, and try to find the char of LF and CR to readLine. My guess that is your problem. However, I'll test your code out tonight.
Roseanne
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tested your code with a text file (a .html file). I noticed that br.read() is reading one character including NL at a time. write(ii) will write NL character out. However br.readLine() is reading one line at a time. NL character is not part of string ss. writeLine(ss) did not add NL character when writing string ss. So the difference between those 2 files are tne NL characters.
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Friend,
Thanks a lot for clearing my doubt!!
Thank you,
Bye,
Tualha Khan
I Checked your site, and have even taken some printouts for later revision. A nicely compiled set of questions and answers. Thanks for that as well!
Bye!
 
Tualha Khan
Ranch Hand
Posts: 287
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The above mentioned site is one from Roseanne Zhang!!
Bye
 
So it takes a day for light to pass through this glass? So this was yesterday's tiny ad?
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic