【问题标题】:Reading a textfile using scanner/reader/bufferedreader to read numbers in .txt-file使用扫描仪/阅读器/缓冲阅读器读取文本文件以读取 .txt 文件中的数字
【发布时间】:2012-11-26 11:56:38
【问题描述】:

我正在尝试用 java 编写一个小程序,它将根据球体的半径计算球体的表面积和体积。这些半径来自带有单列数字的 .txt 文件。

我尝试过搜索一下: Reading numbers in java

代码示例对我来说看起来有点复杂,因为我在阅读 Java 代码方面还不够舒服和经验丰富。我也试过读这个:

Opening and reading numbers from a text file

我对“try”关键字感到困惑,除此之外,它有什么用?

第二个示例在哪里说 File("file.txt"); 我要输入文本文件的路径吗?

如果有人可以向我指出一个可以让初学者了解这些内容的教程,我非常想知道。

到目前为止,这是我的代码:

import java.io.*;

// 这个类读取一个包含单列数字的文本文件(.txt)

公共类 ReadFile {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub

    String fileName = "/home/jacob/Java Exercises/Radii.txt";

    Scanner sc = new Scanner(fileName);

}

}

最好的问候,

雅各布·科斯特鲁普

【问题讨论】:

    标签: java eclipse text-files


    【解决方案1】:

    这是一个小而简单的 sn-p:

    Scanner in = null;
    try {
        in = new Scanner(new File("C:\\Users\\Me\\Desktop\\rrr.txt"));
        while(in.hasNextLine()) {
            int radius = Integer.parseInt(in.nextLine());
    
            System.out.println(radius);
            // . . .
        }
    } catch(IOException ex) {
        System.out.println("Error reading file!");
    } finally {
        if(in != null) {
            in.close();
        }
    }
    

    try-catch 块是 Java 中用来处理异常的东西。你可以在这里阅读所有关于它们的信息,以及它们为何有用:http://docs.oracle.com/javase/tutorial/essential/exceptions/


    当然,如果您使用的是 Java 7 或更高版本,则可以使用 try-with-resources 来简化前面的代码。这是另一种类型的 try 块,除了它会自动为您关闭任何“自动关闭”流,删除代码中丑陋的 finally 部分:

    try (Scanner in = new Scanner(new File("C:\\Users\\Me\\Desktop\\rrr.txt"))) {
        while(in.hasNextLine()) {
            int radius = Integer.parseInt(in.nextLine());
    
            System.out.println(radius);
            // . . .
        }
    } catch(IOException ex) {
        System.out.println("Error reading file!");
    }
    

    您可以在此处阅读有关try-with-resources 的更多信息:http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

    rrr.txt 文件的每一行应该只有一个数字,像这样

    10
    20
    30

    【讨论】:

    • 我尝试了你建议的第二部分,在我试图确保我理解它在做什么之后。效果很好!!谢谢。现在我只需要弄清楚如何从另一个程序中调用它以使用那里的数据。
    【解决方案2】:

    - try/catch 最后是处理在执行某些工作时出现的 ExceptionsErrors (作为两个扩展 Throwable Class)的方法.

    例如:文件I/O、网络操作等

    Scanner in = null;  
    
    try {
    
        in = new Scanner(new File("C:\\Users\\Me\\Desktop\\rrr.txt"));
    
        while(in.hasNextLine()) {
    
      int radius = Integer.parseInt(in.nextLine());  // If this is     
                                                     // not an integer
                                                     // NumberFormatException is thrown.
            System.out.println(radius);
    
    
        }
    
    } catch(IOException ex) {
    
        System.out.println("Error reading file!");
    
    } catch(NumberFormatException ex){
    
        System.out.println("Its not an integer");
    }
     finally {
        if(in != null) {
            in.close();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 1970-01-01
      • 2015-08-30
      • 1970-01-01
      • 2013-04-17
      • 2021-06-30
      相关资源
      最近更新 更多