copy不同编码类型的txt文本:
以字节的方式从源文件读取和写入副本文件:
String charset = "UTF-8";
InputStreamReader in = new InputStreamReader(new FileInputStream(
"E:\\test.txt"), charset);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream("E:\\copyOftest.txt"),charset);
int c=0;
while ((c = in.read()) != -1) {
out.write((char)c);
}
in.close();
out.close();自己写的时候忘了关闭流,出现问题是:能读取却不能写入,加上关闭流后问题就解决了。这是问什么呢?
以字节的方式从源文件读取和写入副本文件:
String charset = "UTF-8";
InputStreamReader in = new InputStreamReader(new FileInputStream(
"E:\\test.txt"), charset);
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream("E:\\copyOftest.txt"),charset);
int c=0;
while ((c = in.read()) != -1) {
out.write((char)c);
}
in.close();
out.close();自己写的时候忘了关闭流,出现问题是:能读取却不能写入,加上关闭流后问题就解决了。这是问什么呢?
OutputStream os.........byte[] buf = new byte[512];
int i;
while ((i = is.read(buf))!=-1){
os.write(buf,0,i);
}
is.close();
os.close();不关闭文件引起的错误,貌似只在用Buffer流时遇到过,平时一般读写文件后好像都没关闭。