输入流:
一、文件字节流,一个字节一个字节的读,不能解决中文乱码的问题,且效率低下
二、通过文件字节流和转换流读,避免乱码,InputStreamReader转换流的构造方法可以设置编码格式
三、通过while用1024的char小桶,一行一行的读,用小桶之后,转换流返回的int就是数量,而不是数据
四、文件字符流
五、缓冲流(文件输入流-转换流-缓冲流),支持一行一行的读,返回的是内容,读完时返回null
输出流:
一、通过文件字符流写入磁盘,不能设置编码集,不能和转换流连用,默认UTF-8
二、通过文件字节流+转换流写入磁盘,可以在转换流设置编码集
三、缓冲流+转换流+文件字节流写入磁盘
代码:
package com.sunny.business.test;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.*;
@Data
@AllArgsConstructor//添加一个包含所有参数的构造方法
@NoArgsConstructor//添加一个无参构造方法
class Cat {//implements Serializable
Stringname ="tom猫";
}
/**
* show 用流测试jackson的输入和输出
*/
public class InputAndOutputText {
//一、文件字节流,一个字节一个字节的读,不能解决中文乱码的问题,且效率低下
private static StringreadJacksonFromDisk(String filePath) {
StringBuffer stringBuffer =null;
try (FileInputStream fis =new FileInputStream(filePath)) {
//牵扯到读,就用while,注意读到末尾返回-1不是0
int data = -1;
stringBuffer =new StringBuffer();
while(-1 != (data = fis.read())) {
//将读到的每个字节拼接
stringBuffer.append((char)data);
}
System.out.println("字符流读取的每一个字符拼接成stringBuffer: " + stringBuffer);
}catch (Exception e) {
}
return stringBuffer.toString();
}
//二、通过文件字节流和转换流读,避免乱码,InputStreamReader转换流的构造方法可以设置编码格式
private static StringreadJacksonFromDiskV2(String filePath) {
//设置编码格式
String charsetName ="UTF-8";
StringBuffer stb =new StringBuffer();
//用转换流,把一个一个字节转换成字符
try(InputStreamReader isr =new InputStreamReader(new FileInputStream(filePath), charsetName)) {
int data = -1;
while (-1 != (data = isr.read())) {
stb.append((char)data);
}
System.out.println("通过("+charsetName+")转换流从磁盘读取:" + stb);
}catch (Exception e) {
}
return stb.toString();
}
//三、通过while用1024的char小桶,一行一行的读,用小桶之后,转换流返回的int就是数量,而不是数据
private static StringreadJacksonFromDiskV3(String filePath) {
//设置编码格式
String charsetName ="UTF-8";
StringBuffer stb =new StringBuffer();
char[] buff =new char[1024];
//用转换流,把一个一个字节转换成字符
try(InputStreamReader isr =new InputStreamReader(new FileInputStream(filePath), charsetName)) {
int length = -1;
while (-1 != (length = isr.read(buff))) {
//读多少,拼多少,用于小桶装不满的情况下
System.out.println("每桶长度为:" + length);
stb.append(buff,0,length);
}
System.out.println("通过("+charsetName+")转换流,利用char小桶,从磁盘读取:" + stb);
}catch (Exception e) {
}
return stb.toString();
}
//四、文件字符流
private static StringreadJacksonFromDiskV4(String filePath) {
StringBuffer stringBuffer =null;
try (FileReader fr =new FileReader(filePath)) {
//牵扯到读,就用while,注意读到末尾返回-1不是0
int data = -1;
stringBuffer =new StringBuffer();
while(-1 != (data = fr.read())) {
//将读到的每个字节拼接
stringBuffer.append((char)data);
}
System.out.println("通过“文件字符流(默认UTF-8)”读取,拼接每一个字符stringBuffer: " + stringBuffer);
}catch (Exception e) {
}
return stringBuffer.toString();
}
//五、缓冲流(文件输入流-转换流-缓冲流),支持一行一行的读,返回的是内容,读完时返回null
private static StringreadJacksonFromDiskV5(String filePath) {
StringBuffer stringBuffer =new StringBuffer();
try (BufferedReader br =new BufferedReader(new InputStreamReader(new FileInputStream(filePath),"UTF-8"))) {
//一行一行的读,读到的就是字符串,所以不用小桶
String text =null;
while (null != (text = br.readLine())) {
stringBuffer.append(text);
}
System.out.println("缓冲流(文件输入流-转换流-缓冲流)读取文件并拼接:" + stringBuffer);
}catch (Exception e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
//一、通过文件字符流写入磁盘,不能设置编码集,不能和转换流连用,默认UTF-8
private static void writeJackson2Disk(String filePath, String text) {
//新建文件
File file =new File(filePath);
try (FileWriter fileWriter =new FileWriter(file)) {
//写操作,数据多少写多少
fileWriter.write(text, 0, text.length());
fileWriter.flush();
System.out.println("类序列化通过”文件字符流(默认UTF-8)“存盘成功");
}catch (Exception e) {
e.printStackTrace();
}
}
//二、通过文件字节流+转换流写入磁盘,可以在转换流设置编码集
private static void writeJackson2DiskV2(String filePath, String text) {
String charsetName ="UTF-8";
try (OutputStreamWriter osw =new OutputStreamWriter(new FileOutputStream(filePath), charsetName)) {
//写操作
osw.write(text);
osw.flush();
System.out.println("类序列化通过”输出流+转换流("+ charsetName +")“存盘成功");
}catch (Exception e) {
e.printStackTrace();
}
}
//三、缓冲流+转换流+文件字节流写入磁盘
private static void writeJackson2DiskV3(String filePath, String text) {
String charsetName ="UTF-8";
try (BufferedWriter br =new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filePath),charsetName))) {
br.write(text);
br.newLine();
br.flush();
}catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String filePath ="doc/jackson.txt";
//测试jackson的输入和输出
//序列化工具对象
ObjectMapper objectMapper =new ObjectMapper();
//实例化一个类供序列化
Cat cat =new Cat();
String text =null;
try {
//序列化并打印
text = objectMapper.writeValueAsString(cat);
System.out.println("Cat类被序列化并打印:" + text);
//反序列化并打印
Cat catByJackson = objectMapper.readValue(text, Cat.class);
System.out.println("反序列化并打印:" + catByJackson.getName());
//存盘
System.out.println("开始存盘");
writeJackson2DiskV3(filePath, text);
//从磁盘读取并反序列化打印
String s =readJacksonFromDiskV5(filePath);
Cat catByJacksonFromDisk = objectMapper.readValue(s, Cat.class);
System.out.println("将读取字符反序列化并打印:" + catByJacksonFromDisk.getName());
}catch (Exception e) {
e.printStackTrace();
}
}
}