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

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; 
VirtualJunky
  • 81
  • 1
  • 7

2 Answers2

0

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;
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • I guess DataInputStream is for bynary files what where write with a DataOuptStream and does not work with text/plain files – Mauricio Gracia Gutierrez Jun 17 '15 at 02:06
  • @VirtualJunky Reads as 0 where? Attach a debugger, because the above code works... note that I removed your odd `VERSION` field... that wasn't doing what you think it was doing. – Elliott Frisch Jun 17 '15 at 02:17
  • http://pastebin.com/AeVN61dg Thats my full file then, I get an error on line 79 because it can't find i. That used to be the VERSION field you removed. Why is it not able to find it even though it's public? Thanks for the help so far by the way. – VirtualJunky Jun 17 '15 at 02:22
  • `String strFilePath = "https://dl.dropboxusercontent.com/u/87363031/cacheVersion9.dat";` isn't a file. That's a URL. – Elliott Frisch Jun 17 '15 at 02:24
  • I just removed it, it's using version.txt. It doesn't work either way. – VirtualJunky Jun 17 '15 at 02:25
  • Next, where does `CacheDownloader` get instantiated? Because your `main` isn't creating instances... and since `main` is the entry-point, I'm pretty sure your's isn't running. Try an initializer block. – Elliott Frisch Jun 17 '15 at 02:25
  • The way it used to work was, in the CacheDownloader it had a hardcoded version number, thats part of the client. So for people to get the updated Cache they would have to manually download the new Client, which then would download the Cache. All I've been trying to do here, is instead I want to make it read a text file on a server that has the version number, so I can change the text file to have peoples clients download the newest version without a new client. Everything else is the same and I know works, it just can't read the text file for some reason. – VirtualJunky Jun 17 '15 at 02:30
  • @VirtualJunky i suggest you either RE-CREATE this question with all this context or ask a new one to get more feedback from other people – Mauricio Gracia Gutierrez Jun 17 '15 at 02:37
  • I was going to originally but every time I ask a question on these types of forums, if it's to lengthy I just never get replies cause no one wants to take the time to read it all, which is understandable. – VirtualJunky Jun 17 '15 at 02:40
  • I found my problem but I don't know how to fix it. http://pastebin.com/7as00SJZ Line 34, I had to add that because line 56 was giving me an error saying no symbol found for i. I think because I added that it's completely ignoring the entire main method and just getting the static in i which is default 0. How can I get the i from in the method to be usable else where? – VirtualJunky Jun 17 '15 at 02:44
  • @VirtualJunky Edited to show you an initializing block. – Elliott Frisch Jun 17 '15 at 04:06
  • I tried using your new method but it is still unable to find i. Cannot find symbol i. I'm still kind of new to this I don't really get why I'm not able to use that variable outside of that method or whatever. – VirtualJunky Jun 17 '15 at 04:36
  • @VirtualJunky Edited one more time. Next time, please create a [mcve](http://stackoverflow.com/help/mcve) before you post your question. – Elliott Frisch Jun 17 '15 at 04:45
  • I'm sorry, new to the site didn't really know regulation and all that. My new error with that is "error: variable VERSION might not have been initialized" line 46. I made a small snip for you http://pastebin.com/9PGCgTjM line 46 is the last line on that pastebin. Thank you so much for your help so far! – VirtualJunky Jun 17 '15 at 04:58
  • I saw your new edits, I tested them an they all return -1, what exactly does that mean? It's not able to read the text file? Until now it was returning 0 every time which as far as I know is default for an int? – VirtualJunky Jun 17 '15 at 05:20
  • Correct. It's reading the text file. It was never returning 0. It was never set before. Now it's set to -1 (by default). Perhaps you should try running the code **without** your downloader to see if you get the `VERSION` then... you might even make a mcve if that doesn't read the text file. But I've already provided you with code that reads a text file. – Elliott Frisch Jun 17 '15 at 05:25
  • I'm just really confused because whatever I set return to is what I get back. Which would mean the try is failing right? I get no error or anything though so I'm just not even sure where to go from here. If I set the return to VERSION it makes it 0 again. – VirtualJunky Jun 17 '15 at 05:32
0

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.

VD007
  • 267
  • 2
  • 15