【问题标题】:Read File in Java, output the first comma delimited String在Java中读取文件,输出第一个逗号分隔的字符串
【发布时间】:2015-06-25 14:11:08
【问题描述】:

我想使用分隔符“,”提取文件中的第一个String。 为什么这段代码生成的行数大于一?

public static void main(String[] args) {
    BufferedReader in = null;
    try {
        in = new BufferedReader(new FileReader("irisAfter.txt"));
        String read = null;
        while ((read = in.readLine()) != null) {
            read = in.readLine();
            String[] splited = read.split(",");
            for (int i =0; i<splited.length;i++) {
                System.out.println(splited[0]);
            } 
        }
    } catch (IOException e) {
            System.out.println("There was a problem: " + e);
            e.printStackTrace();
    } finally {
        try {
            in.close();
        } catch (Exception e) { e.printStackTrace(); }
    }
}

【问题讨论】:

  • 请格式化您的代码。
  • 您能否更具体地说明您的问题是什么?这个网站是给你方向或答案,但不是为你写代码。
  • 你为什么一遍又一遍地打印出同一个词到控制台?
  • 另外,您将跳过 1 行并在每次迭代中阅读下一行。

标签: java string file-io


【解决方案1】:

您正在循环内打印。这就是它多次打印的原因(如果这是您的要求)。

String[] splited = read.split(",");    
System.out.println(splited[0]);

会做的

编辑:正如 Abishek 还提到的,不要在 while 循环中再次 read = in.readLine();,因为这样做会跳过一行。

while ((read = in.readLine()) != null) {
   String[] splited = read.split(",");
   System.out.println(splited[0]);
}

【讨论】:

  • 基本上只是将 for 循环的内容移出它并一起删除 for 循环。
【解决方案2】:

你说的行数优于原来的行数是什么意思

如果您使用splited[0],为什么要保持循环。它总是会得到相同的字符串

【讨论】:

    【解决方案3】:

    不确定您的代码为什么会这样工作,但您可以尝试使用带有分隔符的扫描仪。试试:

    Scanner sc = new Scanner( new File("myNumbers")).useDelimiter(",");
    String firstString = sc.next(); 
    /// check for null..
    

    【讨论】:

      【解决方案4】:

      您从“irisAfter.txt”中读取每一行,然后将“,”上的每一行拆分为多个元素,然后将该行的第一个元素打印在其自己的行中,其次数与该行中的元素一样多.多行*每行多个元素 = 输出行数多于输入行数。

      改变

      for (int i =0; i<splited.length;i++) {
            System.out.println(splited[0]);
      
      }
      

      if (splited.length > 0)
      {
           System.out.println(splited[0]);
      }
      

      这样,您只在自己的行上打印出每一行的第一个元素,并且只有在实际存在第一个元素时才打印出来。

      你也跳过了每一行。如果您不想这样做,请删除该行 read = in.readLine(); 略低于 while ((read = in.readLine()) != null) {.

      (您现在正在读取一行,然后读取下一行,丢弃第一个读取的行。然后处理第二行,之后循环再次开始,您读取第三行,然后读取第四行,丢弃第三行,等等)

      【讨论】:

        【解决方案5】:

        如果你像这样修改你的代码,你应该得到你期望的结果。

         public static void main(String[] args) {
        
                BufferedReader in = null;
                try {
                    String[] splited;
                    in = new BufferedReader(new FileReader("irisAfter.txt"));
                    String read = null;
                    while ((read = in.readLine()) != null) {
                        read = in.readLine();
                        splited = read.split(",");
                    }
        
                    System.out.println(splited[0]);
        
        
                } catch (IOException e) {
                    System.out.println("There was a problem: " + e);
                    e.printStackTrace();
                } finally {
                    try {
                        in.close();
                    } catch (Exception e) {
                    }
                }
        }
        

        【讨论】:

          猜你喜欢
          • 2012-05-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-27
          相关资源
          最近更新 更多