Search code examples
javaeclipsefileoutputstream

Fileoutputstream in eclipse. relative path


is there a way to save a .txt file in eclipse?

Currently I'm doing:

FileOutputStream fout = new FileOutputStream("C:\\Users\\xxxx\\mytxtfile.txt", false);

I want it to be in the eclipse folder from the project and not an absolute path.


Solution

  • try this

    FileOutputStream fout = new FileOutputStream("mytxtfile.txt", false);
    

    If you want to use a folder within the project root I recommend this:

    File root = new File("yourfolder");
    root.mkdir(); //this makes sure the folder exists
    File file = new File(root,"mytextfile.txt");
    FileOutputStream fout = new FileOutputStream(file, false);
    

    To get the install location of eclipse for eclipse 3.3 (i don't know why you would do this, but still) System.getProperty("eclipse.home.location");

    For newer eclipse versions i don't really know.