字节(输入输出)流
package com.test.io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Test1 {
private void m1() throws Exception {
InputStream in = new FileInputStream("D:/my.doc");
BufferedInputStream bin = new BufferedInputStream(in);
OutputStream out = new FileOutputStream("D:/mycopy.doc");
BufferedOutputStream bout = new BufferedOutputStream(out);
byte[] b = new byte[in.available()];
// byte[] b = new byte[bin.available()];
bin.read(b, 0, b.length);
bout.write(b, 0, b.length);
bout.close();
out.close();
bin.close();
in.close();
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test1 t = new Test1();
t.m1();
}
}
字符(输入输出)流
package com.io;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Reader;
import java.io.Writer;
public class Test1 {
private void m1() throws Exception {
File file1 = new File("D:/my.txt");
Reader reader = new FileReader(file1);
BufferedReader br = new BufferedReader(reader);
File file2 = new File("D:/mycopy1.txt");
Writer writer = new FileWriter(file2);
BufferedWriter bw = new BufferedWriter(writer);
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
}
bw.close();
writer.close();
br.close();
reader.close();
}
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
Test1 t = new Test1();
t.m1();
}
}