import java.io.*;
public class file4 {
public static void main(String[] args) throws Exception
{
File f=new File("d:\\filetest\\file.txt");
File f1=new File("d:\\filetest\\file2.txt");
FileInputStream fis = new FileInputStream(f);
FileOutputStream fos = new FileOutputStream(f1);
byte[] b=new byte[(int) f.length()];
fis.read(b);
for(int i=0;i<f.length();i++)
{
fos.write(b[i]);
}
fis.close();
}
}
我在file.txt里面写几个简体中文,在file2.txt里面能读进去,不报错。
我看书说FileInputStream是用来处理字节的。为什么上面的编译后能正常运行呢?
我用的是win7 32位系统,是不是跟这个有关?谢谢大家指点。
------最佳解决方案--------------------------------------------------------
你程序中不是有把字符转换成字节的么?
byte[] b=new byte[(int) f.length()];
你想想你这句话的作用是什么?
------其他解决方案--------------------------------------------------------
算了,帮你写一个吧,你自己研究吧:
package iotest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
public class CopyChineseFile {
public static void main(String[] args) {
String encodeName = System.getProperty("file.encoding");
System.out.println("当前你的OS编码是" + encodeName);
String inputFilename = "d:\\input.txt";
String outputFilename = "d:\\output.txt";
BufferedReader bin = null;
BufferedWriter bout = null;
try {
bin = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFilename), encodeName));
bout = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outputFilename), encodeName));
int c = -1;
while ((c = bin.read()) != -1) {
bout.write(c);
}
bin.close();
bout.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
------其他解决方案--------------------------------------------------------
byte[] b=new byte[(int) f.length()];你一次读整个文件的大小,当然不会出现乱码了= =
------其他解决方案--------------------------------------------------------
iso-8859-1 汉字 length = 2
------其他解决方案--------------------------------------------------------