Create a new Abc.txt file in E:\JavaIO and write some text inside it. Now to do is, in Eclipse, with FileInputStream and
FileOutputStream the Read () and write () methods, implement, abc.txt the copy of the file to create a new Iouitl.java, the code is as follows:
Import Java.io.File;
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStreamReader;
public class Iouitl {public
static void CopyFile (File src,file des) throws ioexception{
if (!src.exists ()) {
throw new IllegalArgumentException (src+ "original file does not exist");
}
if (!src.isfile ()) {
throw new IllegalArgumentException (src+ "not a file")
;
FileInputStream in = new FileInputStream (SRC);
The file does not exist directly created, the file is deleted and created
fileoutputstream out = new FileOutputStream (DES);
byte[] buf = new byte[8*1024];
int b;
In.read (buf,0,buf.length) will read data in BUF array, starting at 0 to array length while
((B=in.read (buf,0,buf.length))!=-1) {
Out.write (buf, 0, b);
Out.flush ();
}
In.close ();
}
Then define a Test.java class to test:
Import Java.io.File;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
Import java.util.ArrayList;
Import Java.util.Arrays;
public class Test2 {public
static void Main (string[] args) {
file src = new File ("E:\\javaio\\abc.txt");
File des = new file ("E:\\javaio\\abc1.txt");
try {
iouitl.copyfile (src, des);
} catch (IOException e) {
//TODO auto-generated catch
block E.printstacktrace ();}}
Run the results, open the file directory, you can see the Abc1.txt file, the contents of the same as abc.txt, the implementation of the copy