• Post Reply Bookmark Topic Watch Topic
  • New Topic

copying files using fileReader, fileWriter

 
Bhasker Reddy
Ranch Hand
Posts: 176
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public void copyfiles(String order_id) throws IOException {
File inputFile = new File("C:/XML/xmlfile.xml");
File outputFile = new File("C:/XML/" + order_id + ".xml");

FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;

while ((c = in.read() )!= -1)
out.write(c);
in.close();
out.close();
System.out.println("Renaming the XML FILE");
}
I am copying files using this method.
Input file is 10kb(356 lines) and output file
is 8kb(309 lines). It's not copying the files
completely. It is not copying last 50 lines.
Does anyone have any idea regarding this.
I will appreciate
 
hennie louw
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Im not sure if this will work but try to use a filereader / writer wrapped around a bufferdreader / bufferdwriter
eg
public void readFiles() {
String data;
try {
BufferReader read = new BufferdReader(new FileReader(inputFile));
BufferdWriter write = new BufferdWriter(new FileWriter(outpurFile));

while ((data = read.readLine()) != null {
write.write(data);
write.newLine();
}
read.close();
write.close();
} catch (IOException e) {
}
}
 
J Ochoa
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bhasker Reddy:

out.close();

You might want to use the flush() method before this line.
out.flush();
out.close();
I had that issue until I used the flush() method. Writes out everything now.
-- Jayne
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!