【问题标题】:Splitting line from a file and storing as separate array values从文件中拆分行并存储为单独的数组值
【发布时间】:2014-01-29 11:18:08
【问题描述】:

我有一个要求,我需要读取文件的内容,每行都需要读入一个数组并拆分行并将值存储到单独的变量中以供进一步处理。

文件中的样本数据

175 31  4   877108051
62  827 2   879373421
138 100 5   879022956
252 9   5   891456797
59  421 5   888206015

预期输出

arr[1][1] = 175
arr[1][2] = 31
arr[1][3] = 2
arr[1][4] = 879373421

arr[2][1] = 62
arr[2][2] = 827
arr[2][3] = 4
arr[2][4] = 877108051

.
.
.

我能够使用 StringTokenizer 正确拆分,但在形成令牌后无法访问单个元素。尝试使用 Array.split(),但它没有将值拆分为单独的数组值。请帮帮我。提前致谢。

我的代码

static public void main(String[] args) {

    File file = new File("small.txt");
    String[] lines = new String[100];
    FileReader reader = new FileReader(file);
    BufferedReader buffReader = new BufferedReader(reader);
    int x = 0;
    String s;

    while((s = buffReader.readLine()) != null){
            lines[x] = s;

                // Using StringTokenizer
        StringTokenizer st = new StringTokenizer(lines[x]);
        while (st.hasMoreTokens()){
            System.out.println("Next token:"+st.nextToken());
        }



        // Using Split() 
        String[] split1 = lines[x].split(" ");
        int size = split1.length;
        System.out.println("Split size:"+size);

        int i = 0;
        for (String value : split1) {

            System.out.println("Index:"+i+"  "+ value); i++;}
}

【问题讨论】:

    标签: java arraylist stringtokenizer


    【解决方案1】:

    您可以大大简化此代码。 试试这样的。

    1) 逐行读取文件,边走边拆分,
    为一些包含String[]ArrayList 添加值

    2) 关闭你的文件

    3) 将ArrayList 变成String[][]

    4) 打印结果

    另外,请注意,Java 中的数组从 0 开始索引,而不是从 1 开始。

    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    public class Test021 {
    
        static public void main(String[] args) throws Exception {
    
            File file = new File("small.txt");
            FileReader reader = new FileReader(file);
            BufferedReader buffReader = new BufferedReader(reader);
            String s = null;
    
            ArrayList<String[]> lst = new ArrayList<String[]>();
            String[][] res = null;
    
            while((s = buffReader.readLine()) != null){
                String[] arr = s.split("[\\s]+");
                lst.add(arr);
            }
    
            buffReader.close();
    
            res = new String[lst.size()][lst.get(0).length];
            res = lst.toArray(res);
    
            System.out.println();
    
            // System.out.println(res);
    
            // String result = Arrays.deepToString(res);
            // System.out.println(result);
            System.out.println();
    
            for (int i=0; i<res.length; i++){
                for (int j=0; j<res[i].length; j++){
                    System.out.println("res[" + (i+1) + "][" + (j+1) + "]=" + res[i][j]);
                }
            }
    
            System.out.println();
        }
    
    }
    

    输出:

    res[1][1]=175
    res[1][2]=31
    res[1][3]=4
    res[1][4]=877108051
    res[2][1]=62
    res[2][2]=827
    res[2][3]=2
    res[2][4]=879373421
    res[3][1]=138
    res[3][2]=100
    res[3][3]=5
    res[3][4]=879022956
    res[4][1]=252
    res[4][2]=9
    res[4][3]=5
    res[4][4]=891456797
    res[5][1]=59
    res[5][2]=421
    res[5][3]=5
    res[5][4]=888206015
    

    【讨论】:

    • res数组的最终输出又是完整的单行输出,你的代码输出.. System.out.println(res[0][0]); => 175 31 4 877108051 System.out.println(res[1][0]); => 62 827 2 879373421 而我希望 res[0][0] 为 175,res[0][1]=31 ... 并且 res[1][0] = 62,res[1][1] = 827 等...我希望将单行上的每个值存储在单独的数组单元格中。
    • 每个数字都在单独的“单元格”中。我假设您至少可以根据需要打印它。都在res,你喜欢打印res
    • 它仍在单个单元格中打印所有值,您的代码输出为... res[1][1]=175 31 4 877108051 res[2][1]=62 827 2 879373421 res [3][1]=138 100 5 879022956 res[4][1]=252 9 5 891456797 res[5][1]=59 421 5 888206015 一旦我改变了行[x].split(" " ) 到行[x].split("[\\s]+")
    • 不,输出不是这样。答案已更新。检查你身边的东西。
    • 这意味着您的文件不仅包含纯空格,还包含其他分隔符(例如制表符)。很好,它现在对你有用。
    【解决方案2】:

    这可能不是原因,但不要以空格分隔。您可能有制表符或 LF 字符,和/或前导和尾随空格。

    即不要使用lines[x].split(" ");

    使用正则表达式分割。

    lines[x].split("[\\s]+"); //one or more spaces. 
    

    【讨论】:

      【解决方案3】:

      试试这个代码

           List<String> lines=new ArrayList<>();
      

      读取文件并将行添加到行数组

          File file = new File("data.txt");
          FileReader reader = new FileReader(file);
          BufferedReader buffReader = new BufferedReader(reader);
          String line = buffReader.readLine();
          while(line!=null){
             lines.add(line);
             line = buffReader.readLine();
      
          }
      

      将线数组转换为字符串二维数组

         String string[][]=new String[lines.size()][0];
          for(int i=0;i<lines.size();i++){
              String s[]=lines.get(i).split(" ");
              string[i]=s;
         }
      

      【讨论】:

      • string[i][0] 包含完整行。“System.out.println("Index:" + i + " " + string[i][0]);" 的输出前 3 行是 Index:0 175 31 4 877108051 Index:1 62 827 2 879373421 Index:2 138 100 5 879022956 其中我正在寻找 string[0][0] 为 175,string[0][1] =31 ...和字符串[1][0] = 62,字符串[1][1] = 827等...
      猜你喜欢
      • 2022-01-14
      • 1970-01-01
      • 2019-01-06
      • 2016-02-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-07
      相关资源
      最近更新 更多