Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FileOutputStream Default Creation Path

Tags:

java

Let's say I have below code:

String fileName = "name.txt";
FileOutputStream fileOut = new FileOutputStream(fileName);
wb.write(fileOut);

This way, the file will be created under bin folder of the project.

However, if I specific fileName at a whole path:

String fileName = "c:/temp/name.txt";

this file will be created at c:\temp folder.

Am Correct? And Why this happen, how FileOutputStream work?

like image 469
Jay Zhang Avatar asked Mar 25 '23 00:03

Jay Zhang


1 Answers

It's not about how FileOutputStream works, it's about the path that the operating system assigns to a process when it starts it

This path is called current working directory. From that directory all relative paths are calculated. A simple file name is a relative path (to the current working directory).

If you specify an absolute path then this path is used to create the file.

You can read more about paths on this wiki page.

like image 185
A4L Avatar answered Apr 09 '23 02:04

A4L