标题翻译
File empty after writing to file obtained with classLoader
问题
以下是您要求的代码的翻译部分:
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class ProgressionYAMLUtil {
private final static File PROGRESSFILE_FILE = getProgressionFile();
public static Boolean write(Progression progression)
{
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
try {
objectMapper.writeValue(PROGRESSFILE_FILE, progression);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static Progression read()
{
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Progression progression = null;
try {
progression = objectMapper.readValue(PROGRESSFILE_FILE, Progression.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return progression;
}
private static File getProgressionFile() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("progress.yaml").getFile());
return file;
}
public static void main(String[] args) {
Progression p1 = new Progression("foo");
p1.newRun(2);
p1.newRun(9);
p1.newRun(11);
System.out.println("WRITE");
System.out.println(p1);
Boolean b = write(p1);
System.out.println("write successfull: " + b);
System.out.println("READ");
Progression p2 = read();
System.out.println(p2);
}
}
请注意,由于您要求只返回翻译好的部分,我已经按照您的要求进行了提取。如果您有其他问题或需要进一步的帮助,请随时提问。
英文翻译
My simple program is as follows:
import java.io.File;
import java.io.IOException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class ProgressionYAMLUtil {
private final static File PROGRESSFILE_FILE = getProgressionFile();
public static Boolean write(Progression progression)
{
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
try {
objectMapper.writeValue(PROGRESSFILE_FILE, progression);
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}
public static Progression read()
{
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Progression progression = null;
try {
progression = objectMapper.readValue(PROGRESSFILE_FILE, Progression.class);
} catch (IOException e) {
e.printStackTrace();
return null;
}
return progression;
}
private static File getProgressionFile() {
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
File file = new File(classLoader.getResource("progress.yaml").getFile());
return file;
}
public static void main(String[] args) {
Progression p1 = new Progression("foo");
p1.newRun(2);
p1.newRun(9);
p1.newRun(11);
System.out.println("WRITE");
System.out.println(p1);
Boolean b = write(p1);
System.out.println("write successfull: " + b);
System.out.println("READ");
Progression p2 = read();
System.out.println(p2);
}
}
For some reason after running this code, my progress.yaml
file is still empty. The program works fine and the printout is very much expected:
WRITE
Progression: foo
0 - 2
1 - 9
2 - 11
write successfull: true
READ
Progression: foo
0 - 2
1 - 9
2 - 11
If I run the main method without the write-block, the program still reads successfully. How can it read content that isn't there? I looked up the internet whether there are some file cache I should clear to get a better understanding of the issue, but I came out short.
If I change the writeValue
statement in the write
method to objectMapper.writeValue(new File("src/main/resources/progress.yaml"), progression);
, that is obtaining the file without classLoader
, the program writes to file and the changes are observable.
How come this happens? Thanks for help
专注分享java语言的经验与见解,让所有开发者获益!
评论