如何在src / main / resources中创建一个文件 [英] How to create a file in src/main/resources

查看:1639
本文介绍了如何在src / main / resources中创建一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  fis = new FileInputStream(new File(。)。getAbsolutePath()+/ sudoinput.txt);它试图写入服务器上的这个位置。我不确定这是不是一个可写的
的地方。

$ p $ FILE NAME(fos):::::: :::::::::::: / opt / tomcat / temp /./ sudoinput.txt
文件名(fis)::::::::::::::::: :/ opt / tomcat / temp /./ sudoinput.txt

我想写入
webapps / sudoku / WEB-INF / classes
基本上是
C:\ Users ... \git\sudo-project\sudo\src\main\resources



在Eclipse Windows 7上,我得到这个
错误
src\main\resources\sudoinput.txt(系统找不到指定的路径)
如果我给$
$ b $ $ p $ f $ = new FileInputStream(src / main / resources / sudoinput.txt);

我也试过这个:

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

但是不起作用。

我应该如何创建一个fileinputstream来写入src / main / resources?
请注意,我正在使用eclipse窗口做开发,并将上传.war文件到一个unix服务器,如果这改变了路径需要指定。

解决方案

同意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

这篇关于如何在src / main / resources中创建一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆