【问题标题】:How to read integers from a file that are separated with semi colon?如何从用分号分隔的文件中读取整数?
【发布时间】:2016-11-12 04:06:28
【问题描述】:

所以在我的代码中,我试图读取一个类似的文件:

100
22
123;22
123 342;432

但是当它输出时会包含“;” (例如 100,22,123;22,123,342;432})。 我正在尝试将文件制作成一个数组(例如 {100,22,123,22,123...} )。 有没有办法读取文件,但忽略分号? 谢谢!

    public static void main(String args [])
{
    String[] inFile = readFiles("ElevatorConfig.txt");
    for ( int i = 0; i <inFile.length; i = i + 1)
    {
        System.out.println(inFile[i]);
    }
    System.out.println(Arrays.toString(inFile));
}
public static String[] readFiles(String file)
{
    int ctr = 0;
    try{
        Scanner s1 = new Scanner(new File(file));
        while (s1.hasNextLine()){
            ctr = ctr + 1;
            s1.next();

        }
        String[] words = new String[ctr];
        Scanner s2 = new Scanner(new File(file));
        for ( int i = 0 ; i < ctr ; i = i + 1){
            words[i] = s2.next();


        }
        return words;
    }
    catch(FileNotFoundException e)
    {
        return null;
    }

}

【问题讨论】:

  • 您不想忽略分号,您想将它们视为分隔符,就像空格一样。忽略123;22 中的分号会变成12322
  • 顺便说一句,从不假装异常没有发生只是为了让代码编译。仅当您要对异常做一些有用的事情时才处理它。
  • 试试 nextInt() 而不是 next()
  • 尽可能只读取一个文件一次。您可以使用ArrayLIst&lt;String&gt; 而不是String[],并且只解析一次文件。
  • @J.L.Louis 将在第一个分号处爆炸。

标签: java io


【解决方案1】:
    public static String[] readFiles(String file)
{
    int ctr = 0;
    try{
        Scanner s1 = new Scanner(new File(file));
        while (s1.hasNextLine()){
            ctr = ctr + 1;
            s1.next();

        }
        String[] words = new String[ctr];
        Scanner s2 = new Scanner(new File(file));
        for ( int i = 0 ; i < ctr ; i = i + 1){
            words[i] = s2.next();


        }
        return words;
    }
    catch(FileNotFoundException e)
    {
        return null;
    }

}

替换为

public static String[] readFiles(String file) {

 List<String> retList = new ArrayList<String>();
  Scanner s2 = new Scanner(new File(file));
    for ( int i = 0 ; i < ctr ; i = i + 1){
         String temp = s2.next();
         String[] tempArr = se.split(";");
         for(int k=0;k<tempArr.length;k++) {
             retList.add(tempArr[k]);

         }
}

return (String[]) retList.toArray();

}

【讨论】:

  • OP希望123;22成为12322
【解决方案2】:

使用正则表达式。将整个文件读入字符串(将每个标记读取为字符串,并在字符串中的每个标记后附加一个空格),然后将其拆分为空格和分号。

String x <--- contains all contents of the file
String[] words = x.split("[\\s\\;]+");

words[]的内容是:

"100", "22", "123", "22", "123", "342", "432"

记得在用作数字之前将它们解析为int

【讨论】:

  • 我做了类似的事情,但不是读取整个文件。它只会读取最后一行。如果我打印出数组,它将是每个元素的最后一行。
  • @TheEWL 很好。阅读您想要的任何内容,然后使用 split 语句。
【解决方案3】:

使用 BufferedReader 的简单方法 逐行读取然后用 ; 分割

public static String[] readFiles(String file)
{
BufferedReader br = new BufferedReader(new FileReader(file))) 
StringBuilder sb = new StringBuilder();
String line = br.readLine();
while (line != null) {
    sb.append(line);
    sb.append(System.lineSeparator());
    line = br.readLine();
}
 String allfilestring = sb.toString();
 String[]  array = allfilestring.split(";");

return array;
 }

【讨论】:

    【解决方案4】:

    您可以使用split() 使用正则表达式根据您的要求将字符串拆分为数组。

    String s; // string you have read from the file
    String[] s1 = s.split(" |;"); // s1 contains the strings separated by space and ";"
    

    希望对你有帮助

    【讨论】:

      【解决方案5】:

      保留计算数组大小的代码。 我只想改变你输入值的方式。

          for (int i = 0; i < ctr; i++) {
             words[i] = "" + s1.nextInt();
          }
      

      【讨论】:

        【解决方案6】:

        另一种选择是将完整文件字符串中的所有非数字字符替换为空格。这样任何非数字字符都会被忽略。

        BufferedReader br = new BufferedReader(new FileReader(file))) 
        StringBuilder sb = new StringBuilder();
        String line = br.readLine();
        while (line != null) {
            sb.append(line);
            line = br.readLine();
        }
        
        String str = sb.toString();
        str = str.replaceAll("\\D+"," ");
        

        现在你有了一个由空格分隔的数字字符串,我们可以将它们标记为数字字符串。

        String[] final = str.split("\\s+");

        然后转换为 int 数据类型。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-02-06
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多