zoukankan      html  css  js  c++  java
  • 从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();
          }
        }
        
        
        
      }
      
      
    
    }
    

      

  • 相关阅读:
    再见了亲爱的学生们,再见了敬爱的同事们,再见了信狮
    #if defined 和#ifdef的区别
    C# ASP.NET Core开发学生信息管理系统(二)
    modelsim与debussy联合的问题
    C#批量生成大数据量无重复随机数据的另类高效实现
    DataColumn.Caption属性应用到DataGridView.HeaderText的方法
    [转载]安装网银安全控件
    近期学习SQL Server,收藏的几个学习教程网址备忘
    asp.net根据域名查ip C#版
    asp.net 2.0教程 成员资格和角色管理
  • 原文地址:https://www.cnblogs.com/lh-masteryi/p/9087252.html
Copyright © 2011-2022 走看看