How to save data from a Properties object to a File + How to load the properties-formatted file to a Properties object in another Method?

huangapple go评论65阅读模式
英文:

How to save data from a Properties object to a File + How to load the properties-formatted file to a Properties object in another Method?

问题

以下是翻译好的内容:

我想要将属性对象 config 中的数据保存到参数中的文件 configFile,代码如下所示:

@Override
public void saveConfig(Properties config, File configFile) {
    
    try {
        ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(configFile));
        os.writeObject(config);
        os.close();
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    
}

在下一个方法中,我想要将以属性格式编排的 configFile 加载到一个 Properties 对象中并返回:

@Override
public Properties loadConfig(File configFile) {
    
    Properties prop = new Properties();
    
    try(InputStream input = new FileInputStream(configFile)){
        prop.load(input);
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    return prop;
}

不知何故,JUnit 测试显示出空指针异常(注意:这是一次考试)

if (!config.getProperty("testKey").equals("testValue"))
        fail("样例配置数据与读取配置数据不匹配!");

我在这里漏掉了什么?

英文:

I want to save the data from the properties objecet config to the file configFile in the parameter like following:

@Override
public void saveConfig(Properties config, File configFile) {
	
	try {
		ObjectOutputStream os = new ObjectOutputStream(new FileOutputStream(configFile));
		os.writeObject(config);
		os.close();
	}
	catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	catch (IOException e) {
		e.printStackTrace();
	}
	
}

In the next method I want to load the properties-formatted configFile to a Properties object and return it:

@Override
public Properties loadConfig(File configFile) {
	
	Properties prop = new Properties();
	
	try(InputStream input = new FileInputStream(configFile)){
		prop.load(input);
	}
	catch (FileNotFoundException e) {
		e.printStackTrace();
	}
	catch (IOException e) {
		e.printStackTrace();
	}
	return prop;
}

Somehow the JUnit test is showing me a NullPointerExeption (note: it is an exam)

if (!config.getProperty("testKey").equals("testValue"))
		fail("sample config data doesn't match read config data!");

What am I missing here?

答案1

得分: 1

以下示例使用 java.nio.file 包,由于其改进的错误处理,应优先使用它,而不是 java.io.File。然而,对于 java.io.File,代码看起来也会很相似。

写入属性

@Override
public void saveConfig(Properties config, Path configFile) throws IOException {
    // 要写在文件开头的注释;
    // `null` 表示无注释
    String comments = ...

    // 使用 try-with-resources 在写入完成后立即关闭写入器
    // java.nio.file.Files.newBufferedWriter​(...) 默认使用 UTF-8
    try (Writer writer = Files.newBufferedWriter(configFile)) {
        config.store(writer, comments);
    }
}

读取属性

@Override
public Properties loadConfig(Path configFile) throws IOException {
    Properties config = new Properties();

    // 使用 try-with-resources 在读取完成后立即关闭读取器
    // java.nio.file.Files.newBufferedReader(...) 默认使用 UTF-8
    try (Reader reader = Files.newBufferedReader(configFile)) {
        config.load(reader);
    }

    return config;
}
英文:

The following examples use the java.nio.file package which should be preferred to java.io.File due to its improved error handling. However, the code will look similarly for java.io.File as well.

Writing Properties

@Override
public void saveConfig(Properties config, Path configFile) throws IOException {
    // Comments to be written at the beginning of the file;
    // `null` for no comments
    String comments = ...

    // try-with-resources to close writer as soon as writing finished
    // java.nio.file.Files.newBufferedWriter​(...) uses UTF-8 by default
    try (Writer writer = Files.newBufferedWriter(configFile)) {
        config.store(writer, comments);
    }
}

Reading Properties

@Override
public Properties loadConfig(Path configFile) throws IOException {
    Properties config = new Properties();

    // try-with-resources to close reader as soon as reading finished
    // java.nio.file.Files.newBufferedReader(...) uses UTF-8 by default
    try (Reader reader = Files.newBufferedReader(configFile)) {
        config.load(reader);
    }

    return config;
}

huangapple
  • 本文由 发表于 2020年9月4日 20:26:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63741169.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定