且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

格式化输出文件

更新时间:2022-11-30 14:29:37


大家好,


我是java编程的新手。我想编写一个程序,将输入文件转换为输出文件,其中的字段用逗号分隔。以下是样本输入:


12345某些词某些词某些词

21345 somewords somewords somewords


以下是我想要的输出:


12345,某些词,某些词,somewords

21345,某些词,某些词,某些词


请帮我写这个程序。我非常感谢你的帮助。
Hi all,

I am very new to java programming. I want to write a program that turn an input file into the output file with fields separated by comma. Here is the sample input:


12345 somewords somewords somewords
21345 somewords somewords somewords

Below is the output I want to have:

12345,somewords,somewords,"somewords
21345,somewords,somewords,somewords

Please help me write this program. I would very much appreciate your help.



你知道如何使用FileReader读取文件。

还考虑将整个文件读入StringBuider对象然后替换所有文件带逗号的空格。您可能需要在结束时进行一些内务管理。

Do you know how to read a file using FileReader then.
Also consider reading the whole file into a StringBuider object and then replacing all spaces with commas. You may need to do a bit of housekeeping at the end.


您在读取文件时是否可以使用字符串标记符,然后只输出token1,token2,token3,token4?
Could you use string tokenizer when reading in the file, then just output token1,token2,token3,token4?



您是否知道如何使用FileReader读取文件。

还考虑将整个文件读入StringBuider对象并然后用逗号替换所有空格。你可能需要在最后做一些家务。
Do you know how to read a file using FileReader then.
Also consider reading the whole file into a StringBuider object and then replacing all spaces with commas. You may need to do a bit of housekeeping at the end.



嗨管理员,

Hi Admin,

I know how to read the file using FileReader/BufferedReader. But I have the problem with the logic of how to add comma and replace the whitespaces between each fields. Please if you can write a simple code so that I can learn from it. I appreciate it very much.