-5

I use new File() to create a file in memory and then I want to write on it but not creating file in disk.

            File file = new File("hello.txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));
            writer.write("This is the text of my file");
            writer.close();

I want it to not create a file on disk.

User1
  • 127
  • 1
  • 14

1 Answers1

1

File is a filelocation, so it doesn't create a file, it point to one.

however when you create a filewriter, and write something to it (like in this example a with a wrapper class buffered writer)

then you will create an file, especially in your case you close the bufferedwriter, which prompts it to flush it's buffer to the filewriter. That filewriter is what's makes your file, cause it needs to write some data to the file called 'hello.txt'

the placement in your tomcatfolder(bin) is because that's what your current dir for the java application is.(startup of you jvm, and without changes to catalina.bat or .sh thats also your working dir)

equivalent is that you would touch a file in console, your working dir is where the file points toward, and unless you specify the full pathname the working directory will be used to place your file in.

If your looking for storing 'file' data in memory then take a look at https://stackoverflow.com/a/17595282/11515649

Joris D R.
  • 186
  • 8