0

image: https://i.stack.imgur.com/nB9Ys.jpg

When ever i use file creation code .file is creating in eclipse home folder not inside my project

FileOutputStream fos = new FileOutputStream("filename.txt"); 
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
Writer writer = new BufferedWriter(osw);
writer.write("something");
System.out.println("new file created ");[enter image description here][1]

do i need to change anything in eclipse conf file ? its really making me so trouble to read any file.i cant able to use relative path inside the any project

  • *"not inside my project"* An app. should not be attempting to write files within its own 'project' in any case. That path will not work once the app. is deployed. Instead store the file in a reproducible, accessible sub-directory of `user.home`. – Andrew Thompson Feb 15 '19 at 02:55

1 Answers1

0

There is some environment variable that determines where the root is for your relative path. It is easy enough to find out which one, but I think your solution shouldn't rely on this. The simplest way to resolve your issue is to use an absolute path (Something like "C:/myfolder/filename.txt" in stead of "filename.txt". But if you want to be more flexible you can add a property to your properties file (or in any other way you choose to add a property) In that property store your root path. and then, when you want to create read or write file build your path with that property:

@Value("${root.path}")
public String ROOT_PATH;
...
FileOutputStream fos = new FileOutputStream(ROOT_PATH + "filename.txt");
...
Michael Gantman
  • 7,315
  • 2
  • 19
  • 36
  • Thanks @michael .. can you please tell how to find "where the root is for your relative path" ? and need to file should create inside my folder because if i solve this its may resolve my relative path issue ? – Vinner Vinoth Feb 14 '19 at 17:21
  • It is most likely environment variable "user.dir" but I STRONGLY discourage you from changing the value of that variable as it may effect your entire OS and not in a good way. Set your own root path either hardcoded or through a property and work with that - construct full path using your specified root and relative path - just like I wrote in the answer – Michael Gantman Feb 14 '19 at 17:39