简体   繁体   English

写入资源文件Java?

[英]Write to resource file java?

I'm trying to dynamically write data to a resource file in java but I can't seem to succeed. 我正在尝试将数据动态写入Java中的资源文件,但似乎无法成功。 Is it possible at all? 有可能吗? I tried a few approaches but none of them worked. 我尝试了几种方法,但是都没有用。

I have a main method which represents: 我有一个主要的方法代表:

public static void main(String[] args) throws Exception {

    ClassLoader loader = ClassLoader.getSystemClassLoader();
    File file = new File(loader.getResource("authors/authors.log").getFile());

    FileWriter writer = new FileWriter(file);
    System.out.println(file.getAbsolutePath());

    writer.write("Test test \n");

    writer.flush();

    System.out.println(file.getAbsolutePath());

    writer.write("Test 2");

    writer.flush();
    writer.close();

}

After executing the main method an exception isn't thrown but the authors.log file doesn't seem to be changed and no data is written. 执行main方法后,不会引发异常,但是authors.log文件似乎没有更改,也没有写入任何数据。

Files are : 文件是:

src/main/java/main/Main.java src / main / java / main / Main.java

src/main/resource/authors/authors.log src / main / resource / authors / authors.log

target/classes/authors/authors.log 目标/课程/作者/authors.log

However the target directory contains an authors.log file and all the data is written there. 但是,目标目录包含一个authors.log文件,所有数据都写入那里。 Am I wrong somewhere or just java doesn't allow writing to a resource file ? 我在某个地方错了吗,或者只是Java不允许写入资源文件? If so where is the best place to store files which are changed by the program (eg log files) 如果是这样,最好的地方是存储程序更改的文件(例如日志文件)的地方

However the target directory contains an authors.log file and all the data is written there. 但是,目标目录包含一个authors.log文件,所有数据都写入那里。

It is rather expected as it is the file that you opened and modified. 可以预料,因为它是您打开和修改的文件。

The following statement uses the classloader to retrieve the file and not the filesystem : 以下语句使用类加载器来检索文件,而不是文件系统:

File file = new File(loader.getResource("authors/authors.log").getFile());

So it will retrieve the file located in target/classes and not which one located in src/main/resources . 因此它将检索位于target/classes classs中的文件,而不是位于src/main/resources

Use the filesystem by specifying the relative path of the current working directory to be able to change the content of src/main/resources : 通过指定当前工作目录的相对路径来使用文件系统,以便能够更改src/main/resources

For example : 例如 :

File file = new File("src/main/resources/authors/authors.log");

or better with the java.nio API : 或使用java.nio API更好:

Path file = Paths.get("src/main/resources/authors/authors.log");

If so where is the best place to store files which are changed by the program (eg log files) 如果是这样,最好的地方是存储程序更改的文件(例如日志文件)的地方

Beyond the technical question, you should wonder why your application needs to modify the source code. 除了技术问题之外,您应该想知道为什么您的应用程序需要修改源代码。 It should be done only in very specific corner cases. 仅应在非常特殊的情况下执行此操作。
For example, the log files don't have to be located in the source code, these have to be outside the source code project because these don't make part of the application source code and are not tools to build the application either. 例如,日志文件不必位于源代码中,它们必须位于源代码项目之外,因为它们不属于应用程序源代码,并且也不是构建应用程序的工具。

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

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