File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes I/O and Streams and the fly likes copying file without messing up the content. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "copying file without messing up the content." Watch "copying file without messing up the content." New topic
Author

copying file without messing up the content.

boonhui kay
Greenhorn

Joined: Feb 07, 2005
Posts: 4
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

Joined: Nov 04, 2004
Posts: 53
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

Joined: Oct 29, 2001
Posts: 8996
    
    9

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.


[How To Ask Questions On JavaRanch]
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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

Joined: May 09, 2002
Posts: 437
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


Help gets you when you need it!
Ramaswamy Narayanan
Greenhorn

Joined: Jan 19, 2005
Posts: 24
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

Joined: Oct 29, 2001
Posts: 8996
    
    9

Since this has become the "how I copy a file" thread, here's how you do it with New IO:
boonhui kay
Greenhorn

Joined: Feb 07, 2005
Posts: 4
thanks alot for the replys. it helped alot. now the program is working fine.

thanks alot again

cheers
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copying file without messing up the content.