您的位置:首页 > 编程语言 > Java开发

java用write()拷贝一个文本文件

2013-11-15 13:46 225 查看
总结:灵活运用循环语句,或条件判断语句。每一种流的正确使用方法;

这里是两种方法;

package com.ds;

import java.io.*;

public class tyut {

/*public void copyFile(FileInputStream in, FileOutputStream out)
throws IOException {
int length;
byte[] b = new byte[23533];
try {
while ((length = in.read()) != -1) {

out.write(b, 0, 23453);
}
} catch (IOException E) {
System.out.println("Error:" + E);
System.out.println(-4);
}

}
*/
public void copyFileByte(FileInputStream in, FileOutputStream out) {
int i = 0;
try {

do {
i = in.read();
if (i != -1)
out.write(i);

} while (i != -1);

} catch (IOException E) {
E.printStackTrace();
}// 只要是输入流输出流都会抛出非运行时异常IoXception

}

public static void main(String[] args) {
FileCopy demo = new FileCopy();
FileInputStream in;
FileOutputStream out;
try {
in = new FileInputStream("dfa.ydy");
out = new FileOutputStream("dsfa.tx");
demo.copyFile(in, out);

} catch (IOException e) {

System.out.println("error:" + e);
System.out.println(-4);
}
}

}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: