开发者

Locating default root folder for an Eclipse project

I am reading a file as follows:

File imgLoc = new File("Player.gif");

BufferedImage image = null;

try {
    image = ImageIO.read(imgLoc);
}
catch(Exception ex)
{
    System.out.println开发者_如何学JAVA("Image read error");
    System.exit(1);
}

return image;

I do not know where to place my file to make the Eclipse IDE, and my project can detect it when I run my code.

Is there a better way of creating a BufferedImage from an image file stored in your project directory?


Take a look in the comments for Class.getResource and Class.getResourceAsStream. These are probably what you really want to use as they will work whether you are running from within the directory of an Eclipse project, or from a JAR file after you package everything up.

You use them along the lines of:

InputStream in = MyClass.class.getResourceAsStream("Player.gif");

In this case, Java would look for the file "Player.gif" next to the MyClass.class file. That is, if the full package/class name is "com.package.MyClass", then Java will look for a file in "[project]/bin/com/package/Player.gif". The comments for getResourceAsStream indicate that if you lead with a slash, i.e. "/Player.gif", then it'll look in the root (i.e. the "bin" directory).

Note that you can drop the file in the "src" directory and Eclipse will automatically copy it to the "bin" directory at build time.


In the run dialog you can choose the directory. The default is the project root.


From my experience it seems to be the containing projects directory by default, but there is a simple way to find out:

System.out.println(new File(".").getAbsolutePath());


Are you trying to write a plugin for Eclipse or is it a regular project?

In the latter case, wouldn't that depend on where the program is installed and executed in the end?

While trying it out and running it from Eclipse, I'd guess that it would find the file in the project workspace. You should be able to find that out by opening the properties dialog for the project, and looking under the Resource entry.

Also, you can add resources to a project by using the Import menu option.


The default root folder for any Eclipse project is also a relative path of that application.

Below are steps I used for my Eclipse 4.8.0 and Java 1.8 project.

I - Place your file you want to interact with along the BIN and SRS folders of your project and not in one of those folders.

II - Implement below code in your main() method.

public static void main(String [] args) throws IOException  {
    
FileReader myFileReader;
BufferedReader myReaderHelper;
    
try {
    
String localDir = System.getProperty("user.dir");
myFileReader = new FileReader(localDir + "\\yourFile.fileExtension");
myReaderHelper = new BufferedReader(myFileReader);

if (myReaderHelper.readLine() != null)  {
    
StringTokenizer myTokens =
    new StringTokenizer((String)myReaderHelper.readLine(), "," );
    System.out.println(myTokens.nextToken().toString()); // - reading first item
}
} catch (FileNotFoundException myFileException) {
    myFileException.printStackTrace();  }     } // End of main()

III - Implement a loop to iterate through elements of your file if your logic requires this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜