【问题标题】:Create dynamically lists for every column of an input file为输入文件的每一列动态创建列表
【发布时间】:2016-04-03 08:13:47
【问题描述】:

我需要创建一个工具来创建一些数据的图形表示。 我需要查看输入文件,它可以是任何具有不同列数的 .txt 或 .csv,之后,用户可以选择要在图表上表示的数据。

假设我有一个这样的输入文件:

Column1;Row1;50;20;Column5 
Column1;Row2;60;30;Column5
Column1;Row3;70;40;Column5
Column1;Row4;80;50;Column5
Column1;Row5;90;60;Column5

我需要一些东西来为输入文件的每一列创建一个列表。对于此示例,创建 5 个列表。如果它有 10 列来创建 10 个列表。 我尝试了一些列表列表,但我不知道如何动态创建对象:

try {
            fis = new FileInputStream("C:/Users/User/Desktop/test1.txt");
            reader = new BufferedReader(new InputStreamReader(fis));


            String line = reader.readLine();
            String[] column = line.split(";");
            while(line != null){
                singleList.add(column[0]);
                line = reader.readLine();
            } 
            listOfLists.add(singleList);

有人可以帮助我吗?如果你可以举一个具体的例子(我是 Java 新手)。

【问题讨论】:

    标签: java list file


    【解决方案1】:

    请不要使用自定义实现

    opencsv

    Apache Commons CSV

    使用 opencsv 的示例

    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.List;
    
    import com.opencsv.CSVReader;
    
    /**
     *
     * @author V.Ladynev
     */
    public class Example {
    
        public static void main(String[] args) throws Exception {
            // Build reader instance
            CSVReader reader = new CSVReader(
                    new FileReader("C:/Users/User/Desktop/test1.txt"), ';', '"');
            // Read all rows at once
            List<String[]> allRows = readAll(reader);
    
            int colsCount = size(first(allRows));
            if (colsCount == 0) {
                return;
            }
    
            List<List<String>> result = new ArrayList<List<String>>();
    
            for (int colIndex = 0; colIndex < colsCount; colIndex++) {
                List<String> col = new ArrayList<String>();
                for (String[] row : allRows) {
                    col.add(get(row, colIndex));
                }
                result.add(col);
            }
    
            System.out.println(Arrays.toString(result.toArray()));
        }
    
        public static List<String[]> readAll(CSVReader reader) throws IOException {
            try {
                return reader.readAll();
            } finally {
                reader.close();
            }
        }
    
        public static <T> T first(List<T> items) {
            return items == null || items.size() == 0 ? null : items.get(0);
        }
    
        public static <T> int size(T[] array) {
            return array == null ? 0 : array.length;
        }
    
        public static <T> T get(T[] array, int index) {
            return size(array) > index ? array[index] : null;
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      我想这就是你想要的:

      FileInputStream fis = new FileInputStream("C:/Users/User/Desktop/test1.txt");
      BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
      
      List<List<String>> listOfList = new ArrayList<>();
      
      String line = reader.readLine();
      String[] column = line.split(";");
      
      //initial
      for(int i = 0; i<column.length;i++){
          listOfList.add(new ArrayList<String>());
      }
      
      while(line != null){
          column = line.split(";");
          for(int i = 0; i<column.length;i++){
              istOfList.get(i).add(column[i]);
          }
      
          line = reader.readLine();
      }
      
      System.out.println(listOfList);
      

      【讨论】:

      • 你需要分割每一行而不是第一行
      • 请把我的名字添加到您的评论中,以便下次投票:)
      猜你喜欢
      • 2022-11-23
      • 1970-01-01
      • 2018-04-14
      • 2015-08-04
      • 2017-05-21
      • 2016-10-15
      • 1970-01-01
      • 1970-01-01
      • 2020-10-22
      相关资源
      最近更新 更多