test.txt
111 aaa
222 bbb
333 ccc怎么把TXT的内容读出 放进集合中

解决方案 »

  1.   

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    public class test1 {
    public static void readFileByLines(String fileName){
       File file = new File(fileName);
       BufferedReader reader = null;
       try {
        System.out.println("以行为单位读取文件内容,一次读一整行:");
        reader = new BufferedReader(new FileReader(file));
        String tempString = null;
        Map map=new HashMap();
        int line = 1;
        //一次读入一行,直到读入null为文件结束
        while ((tempString = reader.readLine()) != null){
         //显示行号
         String a[]=tempString.split(" ");
         map.put(a[0], a[1]);
         System.out.println("line " + line + ": " + tempString);
         System.out.println(map);
         line++;
        }
        reader.close();
       } catch (IOException e) {
        e.printStackTrace();
       } finally {
        if (reader != null){
         try {
          reader.close();
         } catch (IOException e1) {
         }
        }
       }
    }
    public static void main(String[] args){
    new test1();
    test1.readFileByLines("C:/Documents and Settings/ly/桌面/text.txt");
    }
    }
      

  2.   

    import java.util.*;
    import java.io.*;public class FileToMap {
    static File file = new File("C:/value.txt");
    static HashMap<String, String> map = new HashMap<String, String>();

    public static void toMap(File file){
    try{
    BufferedReader bf = new BufferedReader(new FileReader(file));
    String text;

    while((text=bf.readLine()) != null){
    String[] str = text.split("\\s+");
    System.out.print(str[0] +"=="+ str[1]);
    System.out.println();
    map.put(str[0], str[1]);
    }
    }
    catch(IOException e){
    e.printStackTrace();
    }
    }
    public static void main(String args[]){
    toMap(file);
    }
    }
    1111==aaaa
    2222==bbbb
    22==sss
    333==eeee
    原文件:
    1111 aaaa
    2222 bbbb
    22 sss
    333                  eeee
      

  3.   

    public static void main(String[] args)  {
    // TODO Auto-generated method stub
    File file = new File("E:\\workspace\\test\\src\\com\\trisun\\mail\\test.txt");
    FileReader fd = null;
    try {
    fd = new FileReader(file);
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    StringBuffer sb =new  StringBuffer();
    BufferedReader br = new BufferedReader(fd);
    try {
    while(br.ready()){                            
    sb.append(br.readLine());
    }

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    String [] strArray = sb.toString().split(" ");
    List <String > list = new  ArrayList<String>();
    for(int i=0;i<strArray.length;i++){
    list.add(strArray[i]);
    }

    for(String strTemp :list){
    System.out.println(strTemp);
    }

    }
      

  4.   

    结果是:
    111
    aaa
    222
    bbb
    333
    ccc