So my 2009 new years resolution is to learn Java. I recently acquired "Java for Dummies" and have been following along with the demo code in the book by re-writing it using Eclipse. Anyway, every example in the book that uses a relative path does not seem to read the .txt file it's supposed to read from.
Here is the sample code:
import java.util.Scanner; import java.io.File; import java.io.IOException; import javax.swing.JFrame; import javax.swing.JLabel; import java.awt.GridLayout; class TeamFrame extends JFrame { public TeamFrame() throws IOException { PlayerPlus player; Scanner myScanner = new Scanner(new File("Hankees.txt")); for (int num = 1; num <= 9; num++) { player = new PlayerPlus(myScanner.nextLine(), myScanner.nextDouble()); myScanner.nextLine(); addPlayerInfo(player); } add(new JLabel()); add(new JLabel(" ------")); add(new JLabel("Team Batting Aberage:")); add(new JLabel(PlayerPlus.findTeamAverageString())); setTitle("The Hankees"); setLayout(new GridLayout(11,2)); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } void addPlayerInfo(PlayerPlus player) { add(new JLabel(player.getName())); add(new JLabel(player.getAverageString())); } }
And you can see in the below screen shot I have included this file.
image no longer available
Also, I have verified that when I build the application that a copy of Hankees.txt is placed in the bin folder with the compiled .class files.
Lastly, if I change line 12 to the following and place Hankees.txt in the root of my C:\ drive the program compiles and runs fine.
Scanner myScanner = new Scanner(new File("C:\\Hankees.txt"));
So basically, my question is what am I doing wrong? Or is Eclipse responsible for this in some way?
Thanks for any and all help!
A relative path refers to a location that is relative to a current directory. Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy.
A relative path is a path that points from one path (the base path) to a directory or file. The full path (the absolute path) of a relative path is derived by combining the base path with the relative path. The Java NIO Path class can also be used to work with relative paths. You create a relative path using the Paths.
An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).
In simple words, an absolute path refers to the same location in a file system relative to the root directory, whereas a relative path points to a specific location in a file system relative to the current directory you are working on.
You need "src/Hankees.txt"
Your file is in the source folder which is not counted as the working directory.\
Or you can move the file up to the root directory of your project and just use "Hankees.txt"
A project's build path defines which resources from your source folders are copied to your output folders. Usually this is set to Include all files.
New run configurations default to using the project directory for the working directory, though this can also be changed.
This code shows the difference between the working directory, and the location of where the class was loaded from:
public class TellMeMyWorkingDirectory { public static void main(String[] args) { System.out.println(new java.io.File("").getAbsolutePath()); System.out.println(TellMeMyWorkingDirectory.class.getClassLoader().getResource("").getPath()); } }
The output is likely to be something like:
C:\your\project\directory /C:/your/project/directory/bin/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With