简体   繁体   English

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

[英]How to create a file in src/main/resources

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 我想写webapps / sudoku / WEB-INF / classes,基本上是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 在Eclipse Windows 7上,如果给出以下错误,则出现此错误src \\ main \\ resources \\ sudoinput.txt(系统找不到指定的路径)

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 ? 我应该如何创建一个fileinputstream以便能够写入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. 请注意,我正在使用eclipse窗口进行开发,如果更改了需要指定路径的方式,则将.war文件上传到UNIX服务器上。

The src/main/resources folder is a folder that is supposed to contain resources for your application. src/main/resources文件夹是应该包含资源,为应用程序的文件夹。 As you noted, maven packages these files to the root of your file so that you can access them in your library. 如前所述,maven将这些文件打包到文件的根目录,以便您可以在库中访问它们。

Have a look at the Maven documentation about the standard directory layout . 看看有关标准目录布局Maven文档

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. 根据webapp的部署方式,您可能无法写入该目录。 Consider the case when you deploy a .war archive. 考虑部署.war存档时的情况。 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. 这样,无论Web应用程序的部署方式如何,您都可以确保它可以正常工作。

Agree with Sandiip Patil. 同意Sandiip Patil。 If you didn't have folder inside your resources then path will be /sudoinput.txt or in folder /folder_name/sudoinput.txt. 如果您的资源中没有文件夹,则路径将为/sudoinput.txt或文件夹/folder_name/sudoinput.txt. For getting file from resources you should use YourClass.class.getResource("/filename.txt"); 为了从资源获取文件,您应该使用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 另外看看: 这个

You can keep the file created under resources and call .class.getresource(your_file_name_or_path_separated_with_forward_slash); 您可以保留在资源下创建的文件,然后调用.class.getresource(your_file_name_or_path_separated_with_forward_slash);

See if it works for you. 看看是否适合您。

如果您想在webapps/sudoku/WEB-INF/classes中创建文件,而该文件最终位于已创建的WAR文件中,则可以通过将所需文件放入src/main/resources/来实现,这意味着您需要创建文件夹src/main/resources并将所需的文件放入此目录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM