Results 1 to 4 of 4
  1. #1
    Corpsecreate is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default Reading text from files in package

    Ok so I have a problem, its pretty annoying too. I want it so that when my program starts up, one of the first things that is done is that a String will be created and initialized to the contents of a text file in my package.

    I am using Netbeans 7.0 to create the program and perhaps my use of terminology is incorrect when I say package. By package I mean the text file is in the same folder as my .java files that are inside the folder contained in the 'src' folder which is in the project folder (ie C:\.....<project name>\src\<folder name>\myText.txt). In my case it the names follow C:\.....MyProject\src\MyPackage

    Heres a snippet of my method that deals with the reading from text part where PC is the name of my main class and 'dir' is the String "/myPackage/myText.txt":

    Java Code:
    public static String stringFromFile(String dir)
        {
            String s = "";
            File file = new File(dir);
            try 
            { 
               file = new File(PC.class.getResource(dir).toURI()); 
            } 
            catch (Exception e) {}
            // Deal with reading line by line adding each line to s
            return s;
        }
    Now when I run the program and check to see if it works, its fine. My String is indeed the contents of the text file I wanted to read from. The problem I have is that when I click "Clean and Build" to make it into a .jar file. When I run the .jar, the file is no longer read correctly.

    I can confirm the problem is with the line
    Java Code:
    file = new File(PC.class.getResource(dir).toURI());
    and not surprisingly, this is the one line that I basically copy/pasted off the internet without really understanding how it works. My understanding is that it checks the directory of the PC class and then searches the 'dir' within that directory.

    So what needs to be done so that after creating the .jar file, It will still read myText.txt without problems?
    Last edited by Corpsecreate; 05-12-2011 at 06:22 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    jars don't have files but rather resources. use the resource to initialize a stream and read from the stream.

  3. #3
    Corpsecreate is offline Member
    Join Date
    May 2011
    Posts
    2
    Rep Power
    0

    Default

    Ok I changed it around to use .getResourceAsStream and everything works fine now. Thank you so much Fubarable. I really need to learn how this i/o stuff works.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by Corpsecreate View Post
    Ok I changed it around to use .getResourceAsStream and everything works fine now. Thank you so much Fubarable. I really need to learn how this i/o stuff works.
    You're welcome and glad it helped!

Similar Threads

  1. Reading Text Files
    By javaman1 in forum New To Java
    Replies: 3
    Last Post: 02-27-2011, 03:13 AM
  2. Reading raw text files
    By DrSaturn in forum Android
    Replies: 0
    Last Post: 02-24-2011, 08:07 PM
  3. Reading text files
    By SuitMyPants in forum Advanced Java
    Replies: 2
    Last Post: 09-27-2010, 06:14 PM
  4. Reading text files
    By SuitMyPants in forum New To Java
    Replies: 2
    Last Post: 09-27-2010, 03:39 PM
  5. Reading In Text Files
    By Dukey in forum New To Java
    Replies: 4
    Last Post: 04-05-2009, 12:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •