Java IO流 实现文件复制
1、将指定文件转换成输入流
2、创建数组接收输入字节
3、将文件复制想要存放的轮径创建输出流
4、将输入流的字节输出出去
FileInputStream fis = new FileInputStream("D:11.mp4");
FileOutputStream fos = new FileOutputStream("D:1212.mp4");
byte[] b = new byte[1024];
while (fis.read(b)!=-1){
fos.write(b);
}
fis.close();
fos.close();
public class Test01 {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("a.txt");
FileOutputStream fos = new FileOutputStream("f.txt");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f.txt"));
InputStreamReader isr = new InputStreamReader(new FileInputStream("a.txt"));
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f.txt"));
FileReader fr = new FileReader("a.txt");
FileWriter fw = new FileWriter("f.txt");
BufferedReader br = new BufferedReader(new FileReader("a.txt"));
BufferedWriter bw = new BufferedWriter(new FileWriter("f.txt"));
copyFiles1(fis, fos);
copyFiles2(fis, fos);
copyFiles3(bis, bos);
copyFiles4(bis, bos);
copyFiles5(isr, osw);
copyFiles6(isr, osw);
copyFiles7(fr, fw);
copyFiles8(fr, fw);
copyFiles9(br, bw);
copyFiles10(br, bw);
copyFiles11(br, bw);
}
private static void copyFiles1(FileInputStream fis, FileOutputStream fos) throws IOException {
int bys = 0;
while ((bys = fis.read()) != -1) {
fos.write(bys);
}
fos.close();
fis.close();
}
private static void copyFiles2(FileInputStream fis, FileOutputStream fos) throws IOException {
byte[] bys = new byte[1024];
int len = 0;
while ((len = fis.read(bys)) != -1) {
fos.write(bys, 0, len);
}
fos.close();
fis.close();
}
private static void copyFiles3(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {
int bys = 0;
while ((bys = bis.read()) != -1) {
bos.write(bys);
}
}
private static void copyFiles4(BufferedInputStream bis, BufferedOutputStream bos) throws IOException {
byte[] bys = new byte[1024];
int len = 0;
while ((len = bis.read(bys)) != -1) {
bos.write(bys, 0, len);
bos.flush();
}
bos.close();
bis.close();
}
public static void copyFiles5(InputStreamReader isr, OutputStreamWriter osw) throws IOException {
int ch = 0;
while ((ch = isr.read()) != -1) {
osw.write(ch);
osw.flush();
}
isr.close();
osw.close();
}
public static void copyFiles6(InputStreamReader isr, OutputStreamWriter osw) throws IOException {
char[] ch = new char[1024];
int len = 0;
while ((len = isr.read(ch)) != -1) {
osw.write(ch, 0, len);
osw.flush();
}
isr.close();
osw.close();
}
public static void copyFiles7(FileReader fr, FileWriter fw) throws IOException {
int ch = 0;
while ((ch = fr.read()) != -1) {
fw.write(ch);
fw.flush();
}
fw.close();
fr.close();
}
public static void copyFiles8(FileReader fr, FileWriter fw) throws IOException {
char[] chs = new char[1024];
int len = 0;
while ((len = fr.read(chs)) != -1) {
fw.write(chs, 0, len);
fw.flush();
}
fw.close();
fr.close();
}
public static void copyFiles9(BufferedReader br, BufferedWriter bw) throws IOException {
int ch = 0;
while ((ch = br.read()) != -1) {
bw.write(ch);
bw.flush();
}
br.close();
bw.close();
}
public static void copyFiles10(BufferedReader br, BufferedWriter bw) throws IOException {
char[] chs = new char[1024];
int len = 0;
while ((len = br.read(chs)) != -1) {
bw.write(chs, 0, len);
bw.flush();
}
br.close();
bw.close();
}
public static void copyFiles11(BufferedReader br, BufferedWriter bw) throws IOException {
String string = null;
while ((string = br.readLine()) != null) {
bw.write(string);
bw.newLine();
bw.flush();
}
br.close();
bw.close();
}
}
借鉴出处
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)