Results 1 to 12 of 12
  1. #1
    ondra is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Arrow Problem with file address

    Hi,

    I have a problem with my java program I wrote:

    On my computer it works fine, because the file I use in the code has an absolute address to an specific place on my hard drive:

    Java Code:
    public static final File kombinace = new File("D:\\Documents\\NetBeansProjects\\spalicky\\src\\spalicky\\kombinace.txt");
    but I need to send the source files to another person ( my teacher ) and I need the code to work on his computer too...

    do you have any idea how to edit the code to fix this problem???

    I thought maybe be the relative address to the file or some including of the file like a resource might work but I dont not how to do this or if its even possible ..

    thank for every reply :)

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Yes, do not use absolute paths, use relative. Simply include a file and reference it directly:
    File f = new File("someFile.txt");

    where someFile.txt is in the same folder as your jar or class files.

  3. #3
    ondra is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    well , this just doesnt work :(

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

    Default

    I'll bet if you tell us more we'll be better able to help you better. What code are you trying now and where is this file in relation to your class files or your jar?

  5. #5
    ondra is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    The code is a game named "spalicky" and it uses the lines in the file as a combinations of the game items ..

    i put the file right to the same folder as the jar file is created and I also put it in the folder with java classes ...

    if its helpful here are the code files :

    ww.edisk.cz/stahni/69229/spalicky.rar_45.43KB.html
    Last edited by ondra; 08-04-2009 at 07:13 PM. Reason: adding link to the code

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

    Default

    Again, we really would benefit by seeing your current code. Also, are you getting this file's data as a resource of the class? If not, you should look into doing this. Show us your code and we may be able to improve on it (hopefully!). Best of luck.

  7. #7
    ondra is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    heres the code :


    ww.edisk.cz/stahni/69229/spalicky.rar_45.43KB.html

    i cant post al ink yet sou i just add another "w"

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

    Default

    Hopefully someone will go look at your link, but if you don't get help soon, then you might want to try posting the code used to access this file here. As before, best of luck.

  9. #9
    varunPandey is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Talking Generalization

    Hey this may help,
    whenever you code any program with file address, try to keep the file in very common path. Eg:- the path can be "c:\" or "d:\".
    So you can make it more generalized by
    1. Creating a new project(at a generalized place)
    2. Copy the file path at a generalized path.
    3. Copy your new code in this new project - .java file.
    4. Edit the addresses with the new address.
    5. Mention it to the user of the project where to keep the files.
    This will be easy no b'coz all your paths will be there in every PC.

  10. #10
    ondra is offline Member
    Join Date
    Aug 2009
    Posts
    6
    Rep Power
    0

    Default

    thank you, Ill try to solve it another way, anyway thank you :)

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

    Default

    Read the file as a resource stream. Something like this:
    Java Code:
    import java.io.FileNotFoundException;
    import java.io.InputStream;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Scanner;
    import javax.swing.JList;
    import javax.swing.JOptionPane;
    import javax.swing.JScrollPane;
    
    public class Main2 {
      public static final String KOMBINACE_PATH = "kombinace.txt";
    
      public static List<String> testFile() throws FileNotFoundException {
        List<String> stringList = new ArrayList<String>();
        InputStream inStream = Main2.class.getResourceAsStream(KOMBINACE_PATH);
        if (inStream != null) {
          Scanner scanner = new Scanner(inStream);
          while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
              stringList.add(line);
            }
          }
        } else {
          throw new FileNotFoundException(KOMBINACE_PATH + " not found");
        }
    
        return stringList;
      }
    
      private static void createAndShowGUI() {
    
        try {
          String[] strings = Main2.testFile().toArray(new String[0]);
          JList jList = new JList(strings);
          JOptionPane.showMessageDialog(null, new JScrollPane(jList));
        } catch (FileNotFoundException e) {
          JOptionPane.showMessageDialog(null, "File Not Found");
        }
    
      }
    
      public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
          public void run() {
            createAndShowGUI();
          }
        });
    
      }
    }
    Last edited by Fubarable; 08-06-2009 at 12:19 AM.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    Quote Originally Posted by varunPandey View Post
    Hey this may help,
    whenever you code any program with file address, try to keep the file in very common path. Eg:- the path can be "c:\" or "d:\".
    So you can make it more generalized by
    1. Creating a new project(at a generalized place)
    2. Copy the file path at a generalized path.
    3. Copy your new code in this new project - .java file.
    4. Edit the addresses with the new address.
    5. Mention it to the user of the project where to keep the files.
    This will be easy no b'coz all your paths will be there in every PC.
    Unless, of course, that other user is not on a Windows box.

    In other words, this is not good practice.

Similar Threads

  1. Problem of getting erroneous IP address
    By R O C K Y in forum Advanced Java
    Replies: 1
    Last Post: 02-09-2009, 11:25 PM
  2. problem with jar file
    By biba84 in forum Advanced Java
    Replies: 7
    Last Post: 11-09-2008, 06:46 PM
  3. problem - using a jar file
    By jon80 in forum New To Java
    Replies: 6
    Last Post: 07-07-2008, 07:49 AM
  4. JSP getting IP address
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-29-2008, 09:05 AM
  5. problem with jar file pls help
    By jinu5 in forum New To Java
    Replies: 0
    Last Post: 08-15-2007, 10:41 PM

Tags for this Thread

Posting Permissions

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