在java中我们需要进行文件的读写,分别有字节流和字符流。
首先我们要说的是字节流FileInputStream和FileOutputStream,顾名思义字节流以字节为单位对文件进行读写,而这样就会产生一个问题就是对汉字的读取不完整。汉字在java中占2~4个字节,而刚好一次的读取只读到了这个汉字的第一个字节那么就会产生乱码问题
File file = new File("C:\\Users\\zls\\Desktop\\XSteam所需jar\\a.txt");
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
int temp;
byte[] bytes = new byte[5];
StringBuffer stringBuffer = new StringBuffer();
try {
fileInputStream = new FileInputStream(file);
fileOutputStream = new FileOutputStream(new File("C:\\Users\\zls\\Desktop\\XSteam所需jar\\b.txt"));
while ((temp = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, temp);
stringBuffer.append(new java.lang.String(</