Java 向.txt文件添加新行
我在学中文。 我有一个带有光学字符识别器的iPhone应用程序,可以捕获以下格式的人声列表:(字符选项卡发音选项卡定义) 淫秽 TAB yin2hui4 TAB淫秽;淫秽的;淫秽的 网站 TAB Wang3站4 TAB网站 专项 TAB转向4 TAB属性。指定用途 但我使用的flashcard应用程序需要以下格式:(字符换行符发音换行符定义) 淫秽 银辉4 淫秽的;淫秽的;淫秽的 网站 王3站4 网站 专项 转1向4 attr。指定用途Java 向.txt文件添加新行,java,list,Java,List,我在学中文。 我有一个带有光学字符识别器的iPhone应用程序,可以捕获以下格式的人声列表:(字符选项卡发音选项卡定义) 淫秽 TAB yin2hui4 TAB淫秽;淫秽的;淫秽的 网站 TAB Wang3站4 TAB网站 专项 TAB转向4 TAB属性。指定用途 但我使用的flashcard应用程序需要以下格式:(字符换行符发音换行符定义) 淫秽 银辉4 淫秽的;淫秽的;淫秽的 网站 王3站4 网站 专项 转1向4 attr。指定用途 我只懂一点Java。如何将第一种格式转换为第
我只懂一点Java。如何将第一种格式转换为第二种格式?即使它看起来像一个练习。但理想情况下你能做到
- 获取文件内容(使用commons io)
- 用新行替换制表符并写入文件
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Test {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
String path = "C:/test.txt";
// TODO Auto-generated method stub
File file = new File(path);
String string = FileUtils.readFileToString(file);
String finalString = string.replaceAll("\t", "\n");
FileUtils.write(file, finalString);
}
}
文件现在看起来像
淫秽
yin2hui4
obscene; salacious; bawdy
网站
wang3zhan4
website
专项
zhuan1xiang4
attr. earmarked
显然,我们不想做你的家庭作业。但我们也不想让你陷入困境 我留下了许多未解决的问题,下面只是一个看起来像Java的伪代码。你可以从这里开始
FileReader reader = ... // open the file reader using the input file
FileWriter writer = ...// open a file for writing output
while(the stream doesn't end) { // provide the condition, as must be
String line = ... // read a line from the reader
String character = line.substring(0, line.indexOf("\t")),
pronounciation = line.substring(character.length() -1).substring(line.indexOf("\t", character.length()),
definition = line.substring(line.lastIndexOf("\t")); // Obviously, this isn't accurate.... you need to work around this.
writeLineToFile(character)
writeLineToFile(pronounciation)
writeLineToFile(definition)
}
close the reader and writer
Stackoverflow是回答编程问题的地方。这不是学习编程基础的地方,只要用谷歌搜索一下就可以了:java从文件中读取,编写文件,等等,所以我不会选择java来完成这项任务。Perl(或任何其他脚本语言)更适合于此。这些是我需要的关键字(初学者)…谢谢如果文件大小为几GB怎么办?你真的想把整个文件加载到一个
字符串中吗?他没有提到。我不是OP,但我只是建议你编辑你的答案以提供限制。干杯:)谢谢。它实际上并没有说TAB,我只是不知道如何正确地格式化它…非常感谢…我像一个穴居人一样手动执行此操作…这就是我需要的:String secondString=String.replaceAll(“\n”,“\n\n”);。。。String finalString=secondString.replaceAll(“\t”,“\n”);。。。write(文件,finalString);。。。正如你所看到的,我的格式需要工作,很高兴这有帮助。接受为答案