java实现 给文件中的每行内容加引号和逗号并换行,开始结尾用括号包住 例如("123","123") 要求最后不能有逗号
时间: 2023-07-19 16:22:29 浏览: 51
可以按照以下方式实现:
```java
import java.io.*;
import java.util.*;
public class AddQuotesAndCommas {
public static void main(String[] args) throws IOException {
File inputFile = new File("input.txt");
File outputFile = new File("output.txt");
BufferedReader reader = new BufferedReader(new FileReader(inputFile));
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(outputFile)));
writer.print("(");
String line = reader.readLine();
while (line != null) {
writer.print("\"" + line + "\",");
writer.println();
line = reader.readLine();
}
writer.print(")");
reader.close();
writer.close();
}
}
```
这个程序会读取一个名为 `input.txt` 的文件,将每行内容加上引号和逗号,并换行输出到名为 `output.txt` 的文件中。输出的内容会在开始和结尾加上括号,最后一行不会有逗号。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)