Results 1 to 12 of 12
Thread: Problem with file address
- 08-04-2009, 04:46 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
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:
but I need to send the source files to another person ( my teacher ) and I need the code to work on his computer too...Java Code:public static final File kombinace = new File("D:\\Documents\\NetBeansProjects\\spalicky\\src\\spalicky\\kombinace.txt");
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 :)
- 08-04-2009, 06:49 PM #2
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.
- 08-04-2009, 06:54 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
well , this just doesnt work :(
-
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?
- 08-04-2009, 07:01 PM #5
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
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 it´s helpful here are the code files :
ww.edisk.cz/stahni/69229/spalicky.rar_45.43KB.htmlLast edited by ondra; 08-04-2009 at 07:13 PM. Reason: adding link to the code
-
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.
- 08-04-2009, 07:25 PM #7
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
here´s the code :
ww.edisk.cz/stahni/69229/spalicky.rar_45.43KB.html
i cant post al ink yet sou i just add another "w"
-
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.
- 08-05-2009, 07:58 PM #9
Member
- Join Date
- Aug 2009
- Posts
- 2
- Rep Power
- 0
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.
- 08-05-2009, 10:34 PM #10
Member
- Join Date
- Aug 2009
- Posts
- 6
- Rep Power
- 0
thank you, I´ll try to solve it another way, anyway thank you :)
-
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.
- 08-06-2009, 11:13 AM #12
Moderator
- Join Date
- Apr 2009
- Posts
- 10,459
- Rep Power
- 16
Similar Threads
-
Problem of getting erroneous IP address
By R O C K Y in forum Advanced JavaReplies: 1Last Post: 02-09-2009, 11:25 PM -
problem with jar file
By biba84 in forum Advanced JavaReplies: 7Last Post: 11-09-2008, 06:46 PM -
problem - using a jar file
By jon80 in forum New To JavaReplies: 6Last Post: 07-07-2008, 07:49 AM -
JSP – getting IP address
By Java Tip in forum Java TipReplies: 0Last Post: 01-29-2008, 09:05 AM -
problem with jar file pls help
By jinu5 in forum New To JavaReplies: 0Last Post: 08-15-2007, 10:41 PM


LinkBack URL
About LinkBacks


Bookmarks