首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向.txt文件中添加新行

向.txt文件中添加新行
EN

Stack Overflow用户
提问于 2013-07-02 09:58:47
回答 2查看 172关注 0票数 0

我在学中文。我有一个带有光学字符识别器的iPhone应用程序,它可以捕获这种格式的语音列表:(字符TAB发音TAB定义)

yin2hui4淫秽淫秽;淫秽;淫秽 wang3zhan4网站TAB网站 zhuan1xiang4专项TAB .指定用途

但是,我使用的闪存卡应用程序需要这样的格式:(字符新线读音,纽线定义)

淫秽 yin2hui4 淫秽的;淫荡的 网站 wang3zhan4 网站 专项 zhuan1xiang4 attr。指定用途

我只懂一点爪哇。如何将第一种格式转换为第二种格式?

EN

回答 2

Stack Overflow用户

发布于 2013-07-02 10:39:47

显然,我们不想做你的家庭作业。但我们也不想让你搁浅。

我留下了很多东西,下面只是一个看起来像Java的伪代码。你可以从这里开始。

代码语言:javascript
运行
复制
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
票数 2
EN

Stack Overflow用户

发布于 2013-07-02 10:39:13

尽管它看起来像个练习。但理想情况下你可以做到。

  • 获取文件内容(使用commons)
  • 用新行替换TAB并写入文件

示例代码

代码语言:javascript
运行
复制
    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);

        }

}

文件现在看起来就像

代码语言:javascript
运行
复制
    淫秽 
 yin2hui4 
 obscene; salacious; bawdy

    网站 
 wang3zhan4 
 website

    专项 
 zhuan1xiang4 
 attr. earmarked
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17422628

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档