1

If I do this

fis = new FileInputStream(new File(".").getAbsolutePath() + "/sudoinput.txt");

Its trying to write to this location on the server. I am not sure if this is a writable place.

FILE NAME (fos)::::::::::::::::::/opt/tomcat/temp/./sudoinput.txt
FILE NAME (fis)::::::::::::::::::/opt/tomcat/temp/./sudoinput.txt

I wanted to write to webapps/sudoku/WEB-INF/classes which is basically C:\Users...\git\sudo-project\sudo\src\main\resources

On Eclipse Windows 7 I get this error src\main\resources\sudoinput.txt (The system cannot find the path specified) if I give

fis = new FileInputStream("src/main/resources/sudoinput.txt");

I have tried this too:

fis = new FileInputStream("src\\main\\resources\\sudoinput.txt");

but doesn't work.

how should I create a fileinputstream to be able to write to src/main/resources ? please note that I am using eclipse windows to do dev and will be uploading the .war file on to a unix server if this changes the way in which the paths need to be specified.

tshepang
  • 12,111
  • 21
  • 91
  • 136
sbolla
  • 671
  • 3
  • 22
  • 39

4 Answers4

5

The src/main/resources folder is a folder that is supposed to contain resources for your application. As you noted, maven packages these files to the root of your file so that you can access them in your library.

Have a look at the Maven documentation about the standard directory layout.

In certain cases, it is possible to write to the context but it is not a good idea to try it. Depending on how your webapp is deployed, you might not be able to write into the directory. Consider the case when you deploy a .war archive. This would mean that you try to write into the war archive and this won't be possible.

A better idea would be to use a temporary file. In that way you can be sure this will work, regardless of the way your web application is deployed.

Behe
  • 7,572
  • 3
  • 33
  • 46
2

Agree with Sandiip Patil. If you didn't have folder inside your resources then path will be /sudoinput.txt or in folder /folder_name/sudoinput.txt. For getting file from resources you should use YourClass.class.getResource("/filename.txt");

For example

Scanner scanner = new Scanner(TestStats.class.getResourceAsStream("/123.txt"));

or

Scanner scanner = new Scanner(new `FileInputStream(TestStats.class.getResource("/123.txt").getPath()));`

Also look at: this

Community
  • 1
  • 1
lummycoder
  • 588
  • 1
  • 7
  • 20
1

You can keep the file created under resources and call .class.getresource(your_file_name_or_path_separated_with_forward_slash);

See if it works for you.

Sandiip Patil
  • 456
  • 1
  • 4
  • 21
  • i am not sure if i am following; how can i keep or create a file in src/main/resources? ; fis = new FileInputStream("/src/main/resources/sudoinput.txt"); isn't working. iam not creating this file from eclipse before hand to write to it. its something thats created from code – sbolla Jul 09 '13 at 17:37
0

If you like to create files in webapps/sudoku/WEB-INF/classes which is in the end within the created WAR file which can be achieved by putting the files you want into src/main/resources/ This means in other words you need to create the folder src/main/resources and put the files you like into this directory.

khmarbaise
  • 92,914
  • 28
  • 189
  • 235