简体   繁体   中英

FileWriter doesn't write

I'm playing a bit in Java just for fun. After seen some post here about my issue I'm not able to see my error. I close the FileWriter, so the flush it's called implicity but the content it's not written to the file...

private void overrideDefaultPropertiesFile(String logFile) {
    try {
        ClassLoader classLoader = this.getClass().getClassLoader();
        InputStream inputStream = classLoader.getResourceAsStream("log4j.properties");
        String fileText = "";
        String search = "log4j.appender.archivo.file";
        String adaptSlashes = logFile.replace("\\", "/");
        String replacement = "log4j.appender.archivo.file=" + adaptSlashes;
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        String line;
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        while ((line = bufferedReader.readLine()) != null) {
            if (!line.startsWith(search)) {
                fileText += line + "\r\n";
            }
        }
        bufferedReader.close();
        fileText += replacement + "\r\n";
        URI uri = getClass().getResource("/log4j.properties").toURI();
        File log4jProperties = new File(uri);
        FileWriter fileWriter = new FileWriter(log4jProperties, false);
        fileWriter.write(fileText);
        fileWriter.close();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }
}

What I've forgotten or what I'm doing wrong?

Regards.

-------- Updated --------

I'm on Eclipse and the application it's on Maven. Debugging the paths points to:

Eclipse (file I'm trying to modify) -> C:\\Users\\iagoaa\\Documents\\serpa\\src\\main\\resources\\log4j.properties

URI -> file:/C:/Users/iagoaa/Documents/serpa/target/classes/log4j.properties

(Until now, I didn't realize that wasn't the same path until I debugged again to write here the links . Although, I've checked the file on URI and doesn't have the changes written)

I'm using the last method from this blog ( Link info ) because when the project it's on JAR's you can't do new File("src/main/resources/log4j.properties") .

-------- Updated 2 --------

The code works, I had a mess with the paths when I saw the file in the Eclipse package browser. Also, in the output file it was written but instead of entering to see if there were changes I pressed F5 to update the folder and see if the modification date was changed but I do not know why it is not changed and I thought it wasn't writing, but I have forced to throw an exception and it is written.

There is nothing wrong with the code, it seems to be working just fine (maybe you were just missing the inputStream.close() after you close the buffered reader). The problem is that resource files are copied in the generated .class and artifacts folder (depending on which IDE you use). Try using the debugger and see the path where URI is pointing to in order to find out the path to your modified files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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