关于字节流乱码问题:OutputStreamWriter
package ioChar;
import org.junit.Test;
import javax.annotation.processing.Filer;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
public class CharStream {
//字符流
@Test
public void charS() throws Exception {
//建立输出流
FileWriter fileWriter = new FileWriter("h:\java.txt");
//写入数据
fileWriter.write("this is ");
//关闭连接
fileWriter.close();
//BufferedWriter
//建立输出流
FileWriter fileWriter1 = new FileWriter("h:\java.txt");
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter1);
//写数据
bufferedWriter.write("thiidllda daadas kk");
//关闭连接
bufferedWriter.close();
fileWriter.close();
//FileReader
//建立输入流
FileReader fileReader = new FileReader("h:\java.txt");
//read data
// while ( (fileReader.read())!=-1){
// System.out.println((char)fileReader.read());
// }
//BufferedReader
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line = null;
while ((line = (bufferedReader.readLine())) != null) {
System.out.println(line);//整行读取
}
bufferedReader.close();
fileReader.close();
}
}
package com.zrm.io;
import org.junit.Test;
import java.io.*;
public class ByteLuan {
//处理字节流乱码问题
@Test
public void doLuanMa() throws IOException {
//创建流
FileOutputStream fio = new FileOutputStream("h:\java.txt", true);
OutputStreamWriter osw = new OutputStreamWriter(fio, "utf-8");
//写数据
osw.write("java is the best ");
osw.write("中国是最强大的");
//关闭流
osw.close();
fio.close();
//读取数据
//创建流
FileInputStream fis = new FileInputStream("h:\java.txt");
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
//read
char[] car = new char[1024];
int len = 0;
while ((len = isr.read(car)) != -1) {
System.out.println(len);
}
//关闭流
isr.close();
fis.close();
}
}