从TXT读取文本和将数据写入TXT文本的两种方式

/**.
 */

package com.encdata.lihao;

import com.mysql.fabric.xmlrpc.base.Array;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**.
 *
 * @author admin
 *
 */
public class ListMap2TxtTwo {
  
  public static void main(String[] args) {
    
    List<Map<String, Object>> mapList = new ArrayList<>();
    
    Map<String, Object> map1 = new HashMap<>();
    
    Map<String, Object> map2 = new HashMap<>();
    
    Map<String, Object> map3 = new HashMap<>();

    map1.put("aaaa", "1111");
    
    map2.put("bbbb", "2222");
    
    map3.put("cccc", "3333");
    
    mapList.add(map1);
    mapList.add(map2);
    mapList.add(map3);
    
    saveInfoToTxt(mapList,"D://lihao.txt");
    
    readTxtToList();
    
    readTxtToListTwo();
  }
  
  public static void saveInfoToTxt(
      List<Map<String, Object>> mapList,String fileName) {
    
    FileOutputStream fo = null;
    
    OutputStreamWriter os = null;
    
    String strInput = "";
    
    try {
      fo = new FileOutputStream(fileName);
      
      os = new OutputStreamWriter(fo);
      
      for (Map<String, Object> map : mapList) {
        
        for (String str : map.keySet()) {
          strInput = str + "," + map.get(str) + "
";
        }
        
        try {
          os.write(strInput);
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      
      
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        os.flush();
        os.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
      
    } 
  }
  
  public static void readTxtToList(){
    
    File file = new File("D://lihao.txt");
    
    FileInputStream fileInputStream;
    InputStreamReader iReader =  null;
    BufferedReader bReader = null;
    try {
      fileInputStream = new FileInputStream(file);
      iReader = new InputStreamReader(fileInputStream);
      bReader = new BufferedReader(iReader);
      
      String line = null;
      
      try {
        line = bReader.readLine();
        while (line != null) {
          System.out.println(line);
          line = bReader.readLine();
        }
      } catch (IOException e1) {
        e1.printStackTrace();
      }
      
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        bReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    
    
  }
  
public static void readTxtToListTwo(){
    
    File file = new File("D://lihao.txt");
    
    /*FileInputStream fileInputStream;
    InputStreamReader iReader =  null;*/
    FileReader fReader = null;
    BufferedReader bReader = null;
    try {
      /*fileInputStream = new FileInputStream(file);
      iReader = new InputStreamReader(fileInputStream);*/
      fReader = new FileReader(file);
      bReader = new BufferedReader(fReader);
      
      String line = null;
      
      try {
        while((line = bReader.readLine()) != null){
          
          System.out.println(line);
          
        }
      } catch (IOException e) {
        e.printStackTrace();
      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      try {
        bReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    
    
  }
  
  

}

  

原文地址:https://www.cnblogs.com/lh-masteryi/p/9087252.html