无法在Java中写入文件
我一直在尝试创建一个类,该类将遍历一个文件,并根据它找到的值创建一个HashMap,还可以将一个HashMap写入到同一个文件中,并将更改后的值与新的和现有的值合并。将文件读入HashMap是可行的,但是,当我试图用不同的值和键将所述HashMap写回文件时,该文件不会发生任何变化。 为了澄清,我使用的是UNIX风格的系统:Ubuntu14.10无法在Java中写入文件,java,unix,file-io,ubuntu-14.04,Java,Unix,File Io,Ubuntu 14.04,我一直在尝试创建一个类,该类将遍历一个文件,并根据它找到的值创建一个HashMap,还可以将一个HashMap写入到同一个文件中,并将更改后的值与新的和现有的值合并。将文件读入HashMap是可行的,但是,当我试图用不同的值和键将所述HashMap写回文件时,该文件不会发生任何变化。 为了澄清,我使用的是UNIX风格的系统:Ubuntu14.10 public class FConfig { public FConfig( String pathToFile ) throws IOExcep
public class FConfig {
public FConfig( String pathToFile ) throws IOException {
config = new File( pathToFile );
if( !config.exists() ) {
config.createNewFile();
}
}
public FConfig( File file ) throws IOException {
if( !file.isFile() ) {
throw new IllegalArgumentException();
}
config = new File( file.getAbsolutePath() );
if( !config.exists() ) {
config.createNewFile();
}
}
private final File config;
private BufferedReader fileIn;
private BufferedWriter fileOut;
public HashMap<String, String> loadAllProperties() throws IOException {
fileIn = new BufferedReader( new FileReader( config ) );
config.setReadable( true );
HashMap<String, String> props = new HashMap<>();
String line;
while( ( line = fileIn.readLine() ) != null ) {
if( line.contains( "=" ) ) {
props.put( line.split( "=" )[ 0 ], line.split( "=" )[ 1 ] );
}
}
return props;
}
public void writeAllProperties( HashMap<String, String> newProps ) throws IOException {
fileOut = new BufferedWriter( new FileWriter( config ) );
HashMap<String, String> props = loadAllProperties();
props.putAll( newProps );
config.delete();
config.createNewFile();
config.setWritable( true );
System.out.println( config.canWrite() );
for( Entry<String, String> entry : props.entrySet() ) {
System.out.println( entry.getKey() + "=" + entry.getValue() );
fileOut.write( String.format( "%1$s=%2$s", entry.getKey(), entry.getValue() ) );
}
fileOut.close();
}
}
我正在调用这个方法
FConfig c = new FConfig( new File( System.getProperty( "user.home" ), "fConfig.cfg" ).getAbsolutePath() );
HashMap<String, String> props = new HashMap<>();
props.put( "a", "value0" );
props.put( "c", "value1" );
props.put( "b", "value2" );
for( Entry<String, String> e : c.loadAllProperties().entrySet() ) {
System.out.println( e.getKey() + ":" + e.getValue() );
}
try {
c.writeAllProperties( props );
} catch( Exception e ) {
e.printStackTrace();
}
使用GEdit,我可以判断文件正在被修改,但实际上文件中没有任何更改,我已经确保了文件是可写和可读的。我真的很困惑为什么会发生这种情况,因为甚至没有引发异常。更改命令的顺序。创建文件后,在文件上创建FileWriter
public void writeAllProperties( HashMap<String, String> newProps ) throws IOException {
// fileOut = new BufferedWriter( new FileWriter( config ) );
HashMap<String, String> props = loadAllProperties();
props.putAll( newProps );
config.delete();
config.createNewFile();
config.setWritable( true );
// You just created a new file.
fileOut = new BufferedWriter( new FileWriter( config ) );
最后,您的构造函数保证创建一个新文件,因为您没有传入append。因此,您应该真正使用
public void writeAllProperties(HashMap<String, String> newProps)
throws IOException {
// fileOut = new BufferedWriter(new FileWriter(config));
HashMap<String, String> props = loadAllProperties();
props.putAll(newProps);
if (!config.canWrite()) { // <-- to check if it is writable
return;
}
try (BufferedWriter fileOut = new BufferedWriter(new FileWriter(config))) {
System.out.println(config.canWrite());
for (Entry<String, String> entry : props.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
fileOut.write(String.format("%1$s=%2$s", entry.getKey(),
entry.getValue()));
}
}
}
更改命令的顺序。创建文件后,在文件上创建FileWriter
public void writeAllProperties( HashMap<String, String> newProps ) throws IOException {
// fileOut = new BufferedWriter( new FileWriter( config ) );
HashMap<String, String> props = loadAllProperties();
props.putAll( newProps );
config.delete();
config.createNewFile();
config.setWritable( true );
// You just created a new file.
fileOut = new BufferedWriter( new FileWriter( config ) );
最后,您的构造函数保证创建一个新文件,因为您没有传入append。因此,您应该真正使用
public void writeAllProperties(HashMap<String, String> newProps)
throws IOException {
// fileOut = new BufferedWriter(new FileWriter(config));
HashMap<String, String> props = loadAllProperties();
props.putAll(newProps);
if (!config.canWrite()) { // <-- to check if it is writable
return;
}
try (BufferedWriter fileOut = new BufferedWriter(new FileWriter(config))) {
System.out.println(config.canWrite());
for (Entry<String, String> entry : props.entrySet()) {
System.out.println(entry.getKey() + "=" + entry.getValue());
fileOut.write(String.format("%1$s=%2$s", entry.getKey(),
entry.getValue()));
}
}
}
文件中a、b和c的旧值是什么?a、b和c的值不存在,它们用于测试向.cfg文件中添加新属性。您的文件是否包含任何换行符?在运行该程序之前或之后,我使用换行符分隔不同的属性,但在尝试写入该文件之后,该文件保持不变。在创建FileWriter之前,请将调用移动到delete。另外,您是否已使用调试器完成了此操作?文件中a、b和c的旧值是什么?a、b和c的值不存在,它们将测试向.cfg文件中添加新属性您的文件是否包含任何换行符?在运行该程序之前或之后,我使用换行符分隔不同的属性,但在尝试写入该文件之后,该文件保持不变。在创建FileWriter之前,请将调用移动到delete。另外,您是否已通过调试器了解了这一点?createNewFile和setWritable调用应该被删除,而不仅仅是移动。@EJP这些都是必需的。如果文件默认设置为只读,请先在UNIX风格的系统上阅读,然后再建议解决方案。@MatthewJ。他的建议是可靠的,不需要。您当然可以打电话,我已经编辑了我的答案。createNewFile和setWritable调用应该被删除,而不仅仅是移动。@EJP这些都是必需的。如果文件默认设置为只读,请先在UNIX风格的系统上阅读,然后再建议解决方案。@MatthewJ。他的建议是可靠的,不需要。你当然可以打电话,我已经编辑了我的答案。