Results 1 to 13 of 13

Thread: File Path

  1. #1
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default File Path

    FileWriter fstream = new FileWriter("abc.txt");

    The above code creates the file in the folder, where Eclipse is installed. However, I want to create the file in the bin folder of the Java project.

    Or
    What is the way to get the path of bin folder of the Java project, in Java?

    Please guide.

    regards,

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,777
    Rep Power
    25

    Default

    What is the way to get the path of bin folder of the Java project, in Java?
    You could use the JFileChooser class to allow the user to find the folder.

  3. #3
    Jodokus's Avatar
    Jodokus is offline Senior Member
    Join Date
    Jan 2011
    Location
    Amsterdam, the Netherlands
    Posts
    230
    Rep Power
    4

    Default

    FileWriter fstream = new FileWriter("abc.txt");
    The above code creates the file in the folder, where Eclipse is installed.
    I just tried it because I didn't believe you.
    It creates the file in your project-folder (under the Eclipse-folder).
    The bin is normally just under the project folder, so "bin\\abc.txt" will do the trick.
    (I guess I would prefer not to use the bin-directory, but a resource (or whatever) folder on the same level.
    The bin is often cleared (erased) by Eclipse, and files from src are copied there, apart from class-files of course).
    No bug ever had to calculate its fitnessfunction.

  4. #4
    stchman's Avatar
    stchman is offline Member
    Join Date
    Apr 2011
    Location
    Saint Louis, Missouri
    Posts
    97
    Rep Power
    0

    Default

    Quote Originally Posted by novice View Post
    FileWriter fstream = new FileWriter("abc.txt");

    The above code creates the file in the folder, where Eclipse is installed. However, I want to create the file in the bin folder of the Java project.

    Or
    What is the way to get the path of bin folder of the Java project, in Java?

    Please guide.

    regards,
    You can specify a path in the constructor if you wish. Remember to use File.separator.

  5. #5
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    I am using mac version of Eclipse (64 bit), may be this is one of the reason. On my computer, the file is stored at the below address

    /Users/usera/ECLIPSES/eclipse-classic-64/Eclipse.app/Contents/MacOS

    Jodokus advised "I guess I would prefer not to use the bin-directory, but a resource (or whatever) folder on the same level"

    yes, Thanks for advice. But still I do not know, how to get the path of that directory, or how to store newly created file in project folder.

    Norm advised "You could use the JFileChooser class to allow the user to find the folder."
    but I want to be bit more simple, so that user do not do any thing regarding file Path.

    Actually, I want to distribute a Java project(code) to some of my colleagues. Presently I have hard-coded the path in code. One can understand that this is not the right way.

    Please advise.

  6. #6
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    Please note, I run my project as an Eclipse Application (not a simple Java application)

    regards,

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,777
    Rep Power
    25

    Default

    I want to distribute a Java project(code)
    my project as an Eclipse Application(not a simple Java application)
    What is the difference between an Eclipse app and a simple Java app?

  8. #8
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    I mean, I run my project as an Eclipse application and this opens a new child "Eclipse Window". Whereas in a regular Java application, may be new child "Eclipse window" is not necessary. I guess this is the reason that it creates file at path

    /Users/usera/ECLIPSES/eclipse-classic-64/Eclipse.app/Contents/MacOS

    note the "Eclipse.app" in path.

    IWorkspace workspace = ResourcesPlugin.getWorkspace();
    String folder= workspace.getRoot().getLocation().toFile().getPath ().toString();

    I used above code, it provides some absolute path but still, not the one, I am looking for i.e the address of the project folder

    regards,

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,777
    Rep Power
    25

    Default

    the address of the project folder
    What is its value for a simple Java app? Which I assume is completely inside of a jar file.

  10. #10
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    my apology, I could not understand your recent reply, however as an explanation of my problem, please find below code

    public static void main(String[] args) throws IOException {

    FileWriter fstream = new FileWriter("zxy.txt");

    BufferedWriter out = new BufferedWriter(fstream);
    out.write("This is test\n");
    out.close();

    }

    I run above code and it stores the file at correct (wanted )path
    /Users/usera/Documents/workspace/Test

    However, when I run the actual project as an "Eclipse application", it creates file at unwanted path.

    regards,

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,777
    Rep Power
    25

    Default

    I run above code and it stores the file at correct (wanted )path
    I run the actual project as an "Eclipse application", it creates file at unwanted path.
    If the first one does what you want, do it that way.

  12. #12
    novice is offline Member
    Join Date
    May 2010
    Posts
    30
    Rep Power
    0

    Default

    It creates file at wanted path when project is run as "Java Application". However, it creates file at unwanted path, when it runs as "Eclipse Application". Please note, I am supposed to run my project as an Eclipse Application, as the developed project is an Eclipse plugin.

    I created a test project to check the behavior of FileWriter. I learned that Java runtime creates file in the project folder (as you think), if the project run as "Java Application", however, the behavior of Java runtime is different, if we run project as "Eclipse application".

    Anyhow, thanks for your advice.

    regards,




    regards,

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,172
    Rep Power
    20

    Default

    It's not the behaviour of the runtime that's different.
    It's the "home" directory of the app that is.
    Your plugin is run inside Eclipse as part of Eclipse, and it's "home" is in that directory inside Eclipse.
    When run as its own project its "home" is the project.

    Not much you can do about that.

    What (exactly) are you trying to get?
    A path to an open project?
    Any open project?

Similar Threads

  1. file path
    By billq in forum New To Java
    Replies: 2
    Last Post: 04-11-2010, 06:47 PM
  2. ant with a jar file in path to run
    By javastuden in forum Advanced Java
    Replies: 0
    Last Post: 02-22-2010, 06:59 AM
  3. file path
    By ras_pari in forum Advanced Java
    Replies: 2
    Last Post: 10-12-2009, 12:25 PM
  4. find file with * on the path
    By itaipee in forum New To Java
    Replies: 7
    Last Post: 07-08-2009, 09:05 AM
  5. file path
    By Arsench in forum New To Java
    Replies: 7
    Last Post: 10-30-2008, 06:39 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
  •