使用java语言编写一个程序,向 Properties 集合中存入3 个配置项,将 Properties 集合写入到学生姓名首字母命名的文本文件中。
时间: 2024-02-16 10:04:35 浏览: 71
好的,以下是使用 Java 语言编写的程序,实现向 Properties 集合中存入3个配置项,将 Properties 集合写入到学生姓名首字母命名的文本文件中:
```java
import java.io.*;
import java.util.*;
public class ConfigWriter {
public static void main(String[] args) {
Properties props = new Properties();
props.setProperty("server", "localhost");
props.setProperty("port", "8080");
props.setProperty("timeout", "60");
try {
String fileName = "S.txt";
File file = new File(System.getProperty("user.dir") + File.separator + fileName);
FileOutputStream fos = new FileOutputStream(file);
props.store(fos, "Student Configuration");
System.out.println("Properties saved successfully to " + fileName);
} catch (IOException e) {
System.err.println("Error occurred while saving properties: " + e.getMessage());
}
}
}
```
上述程序中,我们使用 Properties 类创建了一个 Properties 集合,并向其中存入了三个配置项。接着,我们创建了一个名为 S.txt 的文本文件,并将 Properties 集合写入该文件中。最后,我们输出了一个保存成功的消息。
运行以上程序后,会在程序所在的目录下生成一个名为 S.txt 的文件,并将 Properties 集合写入其中。这个文件的命名方式是根据学生姓名首字母命名的,这个程序中没有涉及到学生姓名,如果需要根据学生姓名来命名文件,可以根据具体需求进行修改。
阅读全文
相关推荐
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)
![-](https://img-home.csdnimg.cn/images/20241226111658.png)