Reading text file always returns 0 - Java

空扰寡人 提交于 2019-12-29 09:41:37

问题


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).

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; 

回答1:


Here is the default solution that i use whenever i require to read a text file.

public static ArrayList<String> readData(String fileName) throws Exception
{
    ArrayList<String> data = new ArrayList<String>();
    BufferedReader in = new BufferedReader(new FileReader(fileName));

    String temp = in.readLine(); 
    while (temp != null)
    {
        data.add(temp);
        temp = in.readLine();
    }
    in.close();
    return data;
}

Pass the file name to readData method. You can then use for loop to read the only line in the arraylist, and can use the same loop to read multiple lines from different file...I mean do whatever you like with the arraylist.




回答2:


Please don't use a DataInputStream

Per the linked Javadoc, it lets an application read primitive Java data types from an underlying input stream in a machine-independent way. An application uses a data output stream to write data that can later be read by a data input stream.

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

Please do use try-with-resources

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

Use an initializing block

An initializing block will be copied into the class constructor, in your example remove public static void main(String[] args), something like

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

or even

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


来源:https://stackoverflow.com/questions/30881088/reading-text-file-always-returns-0-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!