读取文本文件始终返回0 - Java [英] Reading text file always returns 0 - Java

查看:151
本文介绍了读取文本文件始终返回0 - Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试读取文本文件以获取版本号,但出于某种原因,无论我放入文本文件中它总是返回0(零)。

I'm trying to read a text file to get a version number but for some reason no matter what I put in the text file it always returns 0 (zero).

文本文件名为version.txt,它不包含空格或字母,只包含1个字符作为数字。我需要它来返回那个号码。关于为什么这不起作用的任何想法?

The text file is called version.txt and it contains no spaces or letters, just 1 character that is a number. I need it to return that number. Any ideas on why this doesn't work?

static int i;
public static void main(String[] args) {
    String strFilePath = "/version.txt";
    try
    {
      FileInputStream fin = new FileInputStream(strFilePath);
      DataInputStream din = new DataInputStream(fin);
      i = din.readInt();
      System.out.println("int : " + i);
      din.close();
    }
    catch(FileNotFoundException fe)
    {
       System.out.println("FileNotFoundException : " + fe);
    }
    catch(IOException ioe)
    {
       System.out.println("IOException : " + ioe);
    }
}

    private final int VERSION = i; 


推荐答案

请不要使用 DataInputStream



根据链接的Javadoc,允许应用程序以与机器无关的方式从基础输入流中读取原始Java数据类型。应用程序使用数据输出流来写入数据,以后可以由数据输入流读取。

您想要读取文件(不是来自数据输出流的数据)。

You want to read a File (not data from a data output stream).

由于您似乎想要一个ascii整数,我' d建议您使用 扫描仪

And since you seem to want an ascii integer, I'd suggest you use a Scanner.

public static void main(String[] args) {
    String strFilePath = "/version.txt";
    File f = new File(strFilePath);
    try (Scanner scanner = new Scanner(f)) {
        int i = scanner.nextInt();
        System.out.println(i);
    } catch (Exception e) {
        e.printStackTrace();
    }
}



使用初始化块



初始化块将被复制到类构造函数中,在您的示例中删除 public static void main(String [] args),类似于

private int VERSION = -1; // <-- no more zero! 
{
    String strFilePath = "/version.txt";
    File f = new File(strFilePath);
    try (Scanner scanner = new Scanner(f)) {
        VERSION = scanner.nextInt(); // <-- hope it's a value
        System.out.println("Version = " + VERSION);
    } catch (Exception e) {
        e.printStackTrace();
    }
}



将其解压缩为方法



Extract it to a method

private final int VERSION = getVersion("/version.txt");
private static final int getVersion(String strFilePath) {
    File f = new File(strFilePath);
    try (Scanner scanner = new Scanner(f)) {
        VERSION = scanner.nextInt(); // <-- hope it's a value
        System.out.println("Version = " + VERSION);
        return VERSION;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

甚至

private final int VERSION = getVersion("/version.txt");
private static final int getVersion(String strFilePath) {
    File f = new File(strFilePath);
    try (Scanner scanner = new Scanner(f)) {
        if (scanner.hasNextInt()) {
            return scanner.nextInt();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1;
}

这篇关于读取文本文件始终返回0 - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆