Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copying file without messing up the content.

 
boonhui kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all. i'm writing a program to copy a file from the c:/ to a destinated directory. i'm able to read and copy the file to the directory but then when i opened up the file that have been created, all the content is messed up inside. is there a way to keep the format of the content after i copied the file?
below is the code that i used.
thanks in advance for the help.

import java.net.*;
import java.io.*;

public class fileCopier extends Thread
{
public fileCopier()
{

}

public void run()
{
try
{
File myFile = new File("c:/TotoResult.txt");

BufferedReader fileReader = new BufferedReader(new FileReader(myFile));

String line = null;
//String fileName = fileReader.readLine();

while(( line = fileReader.readLine()) != null)
{
System.out.println(line);

File newFile = new File("TotoResult.txt");
BufferedWriter print = new BufferedWriter(new FileWriter(newFile,true));
print.write(line);
print.flush();
print.close();
}
}
catch(IOException ioe)
{
ioe.printStackTrace();
}
}
}
public class copier
{
public static void main(String [] args)
{
try
{

fileCopier fc = new fileCopier();
Thread t = new Thread(fc);
t.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}

}
 
Joseph Maddison
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
FileReaders are character oriented and depend on the default character set of your environment and may incorrectly represent binary data. My experience has been that if you work with Streams instead, you won't run into that situation.

Hope this Helps,
jdmaddison
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's have a look at the Java API documentation for java.io.BufferedReader:


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


As you can see, readLine does not include line termination characters in the return value, so your output file looks all run together.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I would do would be using the read method in BufferedReader

Suppose the BufferedReader is called input

char[] chars = new char[0];
char[] buffer = new char[4000*1024];
int length = 0;

while ((length = input.read(buffer,0,buffer.length)) != -1) {
char[] temp = new char[chars.length+length];
System.arraycopy(chars,0,temp,0,chars.length);
System.arraycopy(buffer,0,temp,chars.length,length);
chars = temp;
}

input.close();

Then you could create a BufferedWriter and write the array using
the method write
 
Maki Jav
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I want to say that the original code posted is sufficient to get a un-messed up file. As we know that the method readLine(); does not return line termination characters but it does return a whole line of characters. So what we need to do? Well! we just need to add a little code, in addition to correct some....

-----------------------------------------------
File newFile = new File("TotoResult.txt");
BufferedWriter print = new BufferedWriter(new FileWriter(newFile,true));
while(( line = fileReader.readLine()) != null)
{
System.out.println(line);
print.write(line);
print.newLine();
print.flush();
}
print.close();
}
-----------------------------------------------

Hope you find it useful...



Maki Jav
 
Ramaswamy Narayanan
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To copy a file, we can simply use FileInputStream and FileOutputStream.
it will be used for all types of files.
the code is
try
{
FileInputStream fin=new FileInputStream(source file);
FileOutputStream fout=new FileOutputStream(destination file);
int i;
while((i=fin.read())!= -1)
fout.write(i);
}
catch(Exception e)
{}
 
Joe Ess
Bartender
Pie
Posts: 9264
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since this has become the "how I copy a file" thread, here's how you do it with New IO:
 
boonhui kay
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks alot for the replys. it helped alot. now the program is working fine.

thanks alot again

cheers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic