【问题标题】:Java file read problemJava文件读取问题
【发布时间】:2009-10-24 14:40:08
【问题描述】:

我有一个 java 问题。我正在尝试读取一个 txt 文件,该文件每行具有可变数量的整数,并且对于每一行,我需要对每个第二个整数求和!我正在使用扫描仪读取整数,但是当一行完成时无法解决。有人可以帮忙吗?

【问题讨论】:

  • 通过添加代码完成您的问题。

标签: java file java.util.scanner


【解决方案1】:

查看用于读取文本文件的 BufferedReader 类和用于将每一行拆分为字符串的 StringTokenizer 类。

String input;
BufferedReader br = new BufferedReader(new FileReader("foo.txt"));
while ((input = br.readLine()) != null) {
  input = input.trim();
  StringTokenizer str = new StringTokenizer(input);
  String text = str.nextToken(); //get your integers from this string
}

【讨论】:

    【解决方案2】:

    如果我是你,我可能会使用 Apache Commons IO 中的 FileUtils 类。方法readLines(File file) 返回一个字符串列表,每行一个。然后你可以一次处理一行。

    类似这样的:

        File file = new File("test.txt");
        List<String> lines = FileUtils.readLines(file);
        for (String line : lines) {
            // handle one line
        }
    

    (不幸的是,Commons IO 不支持泛型,因此在分配给 List 时会出现未经检查的分配警告。要解决此问题,请使用 @SuppressWarnings,或仅使用无类型列表并强制转换为字符串.)

    这也许是一个例子,可以应用“know and use the libraries”并完全跳过编写一些较低级别的样板代码。

    【讨论】:

    • 这是以包含 jar 库的成本为代价的……尽管我同意如果可以的话,您应该尝试使用公共域库。
    • 是的,如果这是您需要从 Commons IO 获得的 唯一 东西,也许它不值得。但通常 Apache Commons 库(例如 Google Collections)有很多对您的项目有用的东西。
    【解决方案3】:

    或从公地中汲取要领,以学习良好的技术并跳过罐子:

    import java.io.*;
    import java.util.*;
    
    class Test
    {
        public static void main(final String[] args) 
        {
    
           File file = new File("Test.java"); 
    
           BufferedReader buffreader = null;
           String line = "";
    
            ArrayList<String> list = new ArrayList<String>(); 
    
            try 
                {
                    buffreader = new BufferedReader( new FileReader(file) );
                    line = buffreader.readLine();
                    while (line != null) 
                    {
                       line = buffreader.readLine();
                       //do something with line or:
                       list.add(line);
                    }
                 } catch (IOException ioe) 
                 {
                        // ignore
                 } finally 
                 {
                   try 
                   {
                      if (buffreader != null) 
                      {
                         buffreader.close();
                      }
                   } catch (IOException ioe) 
                   {
                        // ignore
                   }
    
                }
    
               //do something with list
               for (String text : list) 
               {       
                   // handle one line
                   System.out.println(text);       
               }
    
       }
    }   
    

    【讨论】:

      【解决方案4】:

      这是我会使用的解决方案。

      import java.util.ArrayList;
      import java.util.Scanner;
      public class Solution1 {
      
          public static void main(String[] args) throws IOException{
              String nameFile;
              File file;
              Scanner keyboard = new Scanner(System.in);
              int total = 0;
      
              System.out.println("What is the name of the file");
      
              nameFile = keyboard.nextLine();
              file = new File(nameFile);
      
              if(!file.exists()){
                  System.out.println("File does not exit");   
                  System.exit(0);
              }
      
      
              Scanner reader = new Scanner(file);
              while(reader.hasNext()){
                  String fileData = reader.nextLine();
                  for(int i = 0; i < fileData.length(); i++){
                      if(Character.isDigit(fileData.charAt(i))){
                          total = total + Integer.parseInt(fileData.charAt(i)+"");
                      }
      
                  }
                  System.out.println(total + " \n");
              }
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 2014-07-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多