码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-05-25 14:02:11      阅读:162      评论:0      收藏:0      [点我收藏+]

标签:oid   pre   main   rgs   tac   xmlrpc   ati   enc   static   

/**.
 */

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();
      }
    }
    
    
    
  }
  
  

}

  

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

标签:oid   pre   main   rgs   tac   xmlrpc   ati   enc   static   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!