Use third-party json conversion tool, Alibaba json conversion tool Fastjson1.2.7.
//www.VeVB.COM/softs/530842.html
Note: I will not repeat the import of jar packages, please refer to Baidu for details.
User class, define two properties, and create constructors, get and set methods
public class User {public String userName; //name public double balance; //amount public User() {super();}public User(String userName, double balance) {super(); this.userName = userName; this.balance = balance;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public double getBalance() {return balance;}public void setBalance(double balance) {this.balance = balance;}}Byte streaming to store json data to txt file
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;public class ListFile {public static void main(String[] args){List<User> list=new ArrayList<>();list.add(new User("Zhang San",100));list.add(new User("Zhang Si",200));list.add(new User("Zhang Wu",300));File file=new File("D:/uselist.txt"); //Stored object file FileOutputStream fos=null;BufferedOutputStream bos=null;try{fos=new FileOutputStream(file);bos=new BufferedOutputStream(fos);String json=JSON.toJSONString(list); //Convert the object to jsonbos.write(json.getBytes("utf-8")); //Json string writes to file bos.flush(); System.out.println("json data writing completed");}catch(Exception e){e.printStackTrace();} finally{try{fos.close();bos.close();}catch(Exception e){e.printStackTrace();}}//Read the file content and output String str="";int num=0;FileInputStream fis=null;BufferedInputStream bis=null;byte buff[]=new byte[1024];try{fis=new FileInputStream(file);bis=new BufferedInputStream(fis);while((num=bis.read(buff))!=-1){str+=new String(buff,"utf-8");}System.out.println(str); //Print the json string that reads the file}catch(Exception e){e.printStackTrace();} finally{try{fis.close();bis.close();}catch(Exception e){e.printStackTrace();}}//Convert the read json data into an object, and output list=JSON.parseArray(str.trim(),User.class); //The read json data has spaces, and the trim method removes for(User obj:list){System.out.println(obj.getUserName()+"/t"+obj.getBalance());}}}}Storing json data to txt file in a character stream
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;import java.util.ArrayList;import java.util.List;import com.alibaba.fastjson.JSON;public class ListFile {public static void main(String[] args){List<User> list=new ArrayList<>();list.add(new User("Zhang San",100));list.add(new User("Zhang Si", 200));list.add(new User("Zhang Wu", 300));File file=new File("D:/uselist.txt"); //Stored object file FileWriter fw=null;BufferedWriter bw=null;try{fw=new FileWriter(file);bw=new BufferedWriter(fw);String json=JSON.toJSONString(list); //Convert object to jsonbw.write(json); //Json string write file bw.flush(); System.out.println("json data writing completed");}catch(Exception e){e.printStackTrace();} finally{try{bw.close();fw.close();}catch(Exception e){e.printStackTrace();}}//Read the file content and output String str="";String s="";FileReader fr=null;BufferedReader br=null;try{fr=new FileReader(file);br=new BufferedReader(fr); while((s=br.readLine())!=null){str+=s;}System.out.println(str); //Print the json string that reads the file}catch(Exception e){e.printStackTrace();} finally{try{br.close();fr.close();}catch(Exception e){e.printStackTrace();}}//Convert the read json data into an object and output list=JSON.parseArray(str.trim(),User.class); for(User obj:list){System.out.println(obj.getUserName()+"/t"+obj.getBalance());}}}}Running results:
json data writing completed[{"balance":100,"userName":"Zhang San"},{"balance":200,"userName":"Zhang Si"},{"balance":300,"userName":"Zhang Wu"}] Zhang San 100.0 Zhang Si 200.0 Zhang Wu 300.0Question: Why do you need to convert the object to json and then store it? Why not store objects directly into files? What are the benefits of using json?
Question 12: If you write the object directly into a file, garbled code will occur and you need to convert the json string before storing it.
Three questions and answers: The advantages of json are easy to transmit, fewer redundant characters, and are easy to convert and read.