读取文件:
原始字节输入流(低级):
public class CharCacheIOReader {
public static void main(String[] args) {
try(
//原始字节输入流(低级)
Reader fr= new FileReader("src\\OutputStream.txt");
//创建一个字符缓冲输入流包装原始的字符输入流********
BufferedReader br= new BufferedReader(fr)
) {
// char[]buffer=new char[3];
// int len;
// while ((len=br.read(buffer))!=-1){
// System.out.print(new String(buffer, 0, len));
// }
//读取每一行的字符 readLine(),用上循环。
String line;
while ((line=br.readLine())!=null){
System.out.println(line);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
字节输出流:
public static void main(String[] args) {
try (Writer fw= new FileWriter("src\\OutputStream.txt",true);
//创建一个字符缓冲输出流管道
BufferedWriter bw=new BufferedWriter(fw);//有换行功能
) {
bw.write('a');
bw.write(97);
bw.write('有');
bw.newLine();
bw.write("我爱你");
bw.newLine();
}catch (Exception e){
e.printStackTrace();
}
}
}
Java对象输出json文本:
pom文件导入fastjson2坐标:
<dependency>
<groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId>
<version>2.0.51</version>
</dependency>
自定义一个UserJson类:
package IO.jsonIO;
public class UserJson {
public String getName() {
return name;
}
@Override
public String toString() {
return "UserJson{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public UserJson(String name, Integer age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
private String name;
private Integer age;
private String gender;
}
测试代码:
public class JsonIO {
public static final String JSON_SRC="src\\jsonText.json";
public static void main(String[] args) {
UserJson jsonObj = new UserJson("小美",25,"女");
//转Json
byte[] bytes = JSON.toJSONBytes(jsonObj);
//写出
try(OutputStream fo = new FileOutputStream(JSON_SRC,false);)//true:追加数据,false:不追加数据
{
fo.write(bytes);
System.out.println(new String(bytes)+"输出成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("Error");
}
//读取json文件
try ( InputStream fi = new FileInputStream(JSON_SRC);)
{
byte[] jsonBytes = fi.readAllBytes();
JSONReader jsonObject = JSONReader.of(jsonBytes);
System.out.println(jsonObject.readString()+"读取成功!");
//转换成java对象
UserJson obj = JSON.parseObject(jsonBytes, UserJson.class);
System.out.println(obj.toString());
System.out.println("姓名:"+obj.getName());
System.out.println("年龄:"+obj.getAge());
System.out.println("性别:"+obj.getGender());
}catch (Exception e){
e.printStackTrace();
System.out.println("Error");
}
}
}
json文件样式:
测试结果:
{"age":25,"gender":"女","name":"小美"}输出成功
{"age":25,"gender":"女","name":"小美"}读取成功!
UserJson{name='小美', age=25, gender='女'}
姓名:小美
年龄:25
性别:女
json文件输出:
{"age":25,"gender":"女","name":"小美"}
嵌套一层Json:
第一层类:
public class UserJson {
private User user;
public UserJson(User user) {
this.user = user;
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
嵌套层类User:
package IO.jsonIO;
public class User {
private String name;
private Integer age;
private String gender;
@Override
public String toString() {
return "User{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public User(String name, Integer age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
}
测试代码:
public class JsonIO {
public static final String JSON_SRC="src\\jsonText.json";
public static void main(String[] args) {
User user = new User("小美",25,"女");
UserJson jsonObj = new UserJson(user);
//转Json
byte[] bytes = JSON.toJSONBytes(jsonObj);
//写出
try(OutputStream fo = new FileOutputStream(JSON_SRC,false);)//true:追加数据,false:不追加数据
{
fo.write(bytes);
System.out.println(new String(bytes)+"输出成功");
}catch (Exception e){
e.printStackTrace();
System.out.println("Error");
}
//读取json文件
try ( InputStream fi = new FileInputStream(JSON_SRC);)
{
byte[] jsonBytes = fi.readAllBytes();
JSONReader jsonObject = JSONReader.of(jsonBytes);
System.out.println(jsonObject.readString()+"读取成功!");
//只读user的name JSONPath
JSONPath userGet = JSONPath.of("$.user.name");
//user.name
Object extract = userGet.extract(jsonBytes);
System.out.println(extract);
//转换成java对象
UserJson obj = JSON.parseObject(jsonBytes, UserJson.class);
System.out.println(obj.getUser());
System.out.println("姓名:"+obj.getUser().getName());
System.out.println("年龄:"+obj.getUser().getAge());
System.out.println("性别:"+obj.getUser().getGender());
}catch (Exception e){
e.printStackTrace();
System.out.println("Error");
}
}
}
测试结果:
{"user":{"age":25,"gender":"女","name":"小美"}}输出成功
{"user":{"age":25,"gender":"女","name":"小美"}}读取成功!
小美
User{name='小美', age=25, gender='女'}
姓名:小美
年龄:25
性别:女
Json文件:
{"user":{"age":25,"gender":"女","name":"小美"}}