2

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

4

4 回答 4

2

查看用于读取文本文件的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
}
于 2009-10-24T14:44:35.547 回答
0

如果我是你,我可能会使用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<String> 时会出现未经检查的分配警告。要解决此问题,请使用@SuppressWarnings,或者只是一个无类型的列表并强制转换为字符串。)

这也许是一个可以应用“了解并使用库”并完全跳过编写一些较低级别的样板代码的情况的示例。

于 2009-10-24T15:04:23.700 回答
0

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

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);       
           }

   }
}   
于 2009-10-27T21:01:45.810 回答
0

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

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");
        }

    }

}
于 2017-09-15T22:45:22.243 回答