Java IO 转换流的读入和写出
转换流主要是将字节流转换成字符流,主要目的是解决读取和写出乱码问题,以下情况会导致乱码:
1) 读取和写出编码字符集不一致;
2) 读取和写出数据不完整。
1、InputStreamReader 转换输入流
InputStreamReader 读入文本时将字节流转换成字符流。
1.1 InputStreamReader 的demo如下:
/**
* 使用转换流InputStreamReader 处理乱码问题
*
* @param file
* 文件对象
*/
public static void readFile(File file, String charset) {
BufferedReader br = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
String line = null;
while (null != (line = br.readLine())) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != br) {
br.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* 使用转换流InputStreamReader 处理乱码问题
*
* @param file
* 文件对象
*/
public static void readFileWithJdk7(File file, String charset) {
// java 7 新特性 try-with-resource
// 不用手动释放资源,程序自动释放,因为Reader实现了AutoCloseable接口
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset))) {
String line = null;
while (null != (line = reader.readLine())) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* @param filePath
* 文件路径
*/
public static void readFile(String filePath, String charset) {
readFile(new File(filePath), charset);
}
2、OutputStreamWriter 转换输出流
OutputStreamWriter 写出文本时将字节流转换成字符流。
2.1 OutputStreamWriter 的demo如下:
/**
* jdk 7 新特性
*
* 使用转换流输出流OutputStreamWriter,将字节流转换成字符流,主要处理乱码问题 乱码问题: 1、读入和写出字符集不同一
* 2、读写数据不全
*
* @param file
* 指定输出文件
* @param msg
* 输出内容
* @param append
* 是否追加,true 追加 false 不追加
* @param charset
* 字符集
*/
public static void writeToFileWithJdk7(File file, String msg, boolean append, String charset) {
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset))) {
writer.write(msg);
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
*
* 使用转换流输出流OutputStreamWriter,将字节流转换成字符流,主要处理乱码问题 乱码问题: 1、读入和写出字符集不同一
* 2、读写数据不全
*
* @param file
* 指定输出文件
* @param msg
* 输出内容
* @param append
* 是否追加,true 追加 false 不追加
* @param charset
* 字符集
*/
public static void writeToFile(File file, String msg, boolean append, String charset) {
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), charset));
writer.write(msg);
writer.newLine();
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != writer) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 默认不追加
*
* @param file
* 指定输出文件
* @param msg
* 输出内容
* @param charset
* 字符集
*/
public static void writeToFile(File file, String msg, String charset) {
writeToFile(file, msg, false, charset);
}
/**
*
* @param filePath
* 指定输出文件路径
* @param msg
* 输出内容
* @param append
* 是否追加,true 追加 false 不追加
* @param charset
* 字符集
*/
public static void writeToFile(String filePath, String msg, boolean append, String charset) {
writeToFile(new File(filePath), msg, append, charset);
}
/**
*
* @param filePath
* 指定输出文件路径
* @param msg
* 输出内容
* @param append
* 是否追加,true 追加 false 不追加
* @param charset
* 字符集
*/
public static void writeToFile(String filePath, String msg, String charset) {
writeToFile(new File(filePath), msg, false, charset);
}