How to create a file in src/main/resources

前端 未结 4 1151
盖世英雄少女心
盖世英雄少女心 2021-01-23 12:42

If I do this

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

Its trying to write to this location on the ser

相关标签:
4条回答
  • 2021-01-23 13:15

    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.

    0 讨论(0)
  • 2021-01-23 13:24

    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

    0 讨论(0)
  • 2021-01-23 13:25

    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.

    0 讨论(0)
  • 2021-01-23 13:33

    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.

    0 讨论(0)
提交回复
热议问题