FileInputStream f = new FileInputStream("xxx.txt"); //用已存在的文件,创建输入流,用于读出来
FileOutputStream fileOutputStream = new FileOutputStream("hahaha.txt");//创建输出流,用于写入
//读
int a;
while ((a = f.read())!=-1) {
//写
fileOutputStream.write(a);
}
//关闭
f.close();
fileOutputStream.close();
改进读与写:
FileInputStream f = new FileInputStream("xxx.txt"); //用已存在的文件,创建输入流,用于读出来
FileOutputStream fileOutputStream = new FileOutputStream("hahaha.txt");//创建输出流,用于写入
//读
byte []arr = new byte[1024 * 8];
int a;
while ((a = f.read())!=-1) {
//写
fileOutputStream.write(arr, 0,a);;
}
//关闭
f.close();
fileOutputStream.close();
或者
FileInputStream f = new FileInputStream("xxx.txt"); //用已存在的文件,创建输入流,用于读出来
FileOutputStream fileOutputStream = new FileOutputStream("hahaha.txt");//创建输出流,用于写入
BufferedInputStream bufferedInputStream = new BufferedInputStream(f);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
int c;
while ( (c = bufferedInputStream.read()) != -1) {
bufferedOutputStream.write(c);
}
bufferedInputStream.close();
bufferedOutputStream.close();
关于字符操作
FileReader //读字符
FileWrite //写字符
网友评论