从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) + "\r\n";
        }
        
        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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值