Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

I am getting junk, when trying file copy

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I am trying to copy a existing file to a new file. Here is what i am doing
I am creating 2 File objects
File f = new File("c:"+"\\kareem"+"\\resume"+"\\Test.doc");
File x = new File("c:"+"\\kareem"+"\\resume"+"\\Resume.doc");
I am creating FileReader object and passing x
FileReader r = new FileReader(x);
I am creating a FileWriter Object and passing f
FileWriter w = new FileWriter(f);
And rest of the code is
int c;
while((c = r.read()) != -1)
{
w.write(c);
System.out.println(r.read());
}
r.close();
w.close();
}
catch(IOException e){}
}
It is executing and i am getting Test file but its size is half of the original file which is Word document, and contains junk in it.
Please clear this as i am very new to I/O programming. Thanks in advance
kareem
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
since word documents are binary format, you have to use binary streams(InputStream/OutputStream), not character streams(reader/writer).
 
Zakaria Haque
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is some untested code, which should serve your purpose
InputStream in = new BufferdInputStream(new FileInputStream("sourcefile"));
OutputStream out = new BufferedOutputStream(new FileOutputaStream("detination file"));
byte[] buffer = new byte[512];
while(true) {
int datLength = in.read(buffer);
if(dataLength == -1) {
break;
}
out.write(buffer,0,dataLength)
}
in.close();
out.flush();
out.close();
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Zakaria
I was under the impression that since word document contains characters, so character stream classes should work.
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic