用java读取nginx.conf内容,保留原有格式写入到nginx2.conf
时间: 2023-02-07 08:34:44 浏览: 320
下面是用 Java 读取 nginx.conf 并将内容保留原有格式写入到 nginx2.conf 的例子:
```
import java.io.*;
public class ReadNginxConf {
public static void main(String[] args) {
// 读取文件
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader("nginx.conf"));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 写入文件
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new FileWriter("nginx2.conf"));
} catch (IOException e) {
e.printStackTrace();
}
// 读取并写入
String line;
try {
while ((line = reader.readLine()) != null) {
writer.write(line);
writer.newLine(); // 这一行很重要,保留原有的换行格式
}
} catch (IOException e) {
e.printStackTrace();
}
// 关闭流
try {
reader.close();
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
阅读全文