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

      

  • 相关阅读:
    怎样快速缩小问题的排查范围
    Android Activity启动模式图解
    只有理解了部分是怎么组合到整体上的,才能理解元素的真正功能
    aop收口要寻找最窄的切面
    错误的本质是不一致
    代理:接口一致、创建混入(替代)、消息转发
    分解与组合是终极方法论(还原论)
    OC之NSURLsession
    一个高频问题:异步操作会创建线程吗?
    关于Chrome跨域The request client is not a secure xxx相关提示的解决
  • 原文地址:https://www.cnblogs.com/lh-masteryi/p/9087252.html
Copyright © 2020-2023  润新知