且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在src / main / resources中创建一个文件

更新时间:2022-10-19 17:29:35

同意Sandiip Patil。如果您的资源中没有文件夹,则路径将为 /sudoinput.txt 或文件夹 /folder_name/sudoinput.txt。为了从资源获取文件,您应该使用 YourClass.class.getResource(/ filename.txt);



例如

 扫描仪扫描程序= new Scanner(TestStats.class.getResourceAsStream(/ 123.txt)) ; 

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

另请看:这个


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.

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