【问题标题】:Modify contents of text file and write to new file in java修改文本文件的内容并在java中写入新文件
【发布时间】:2015-04-05 11:16:17
【问题描述】:

所以我已经有了基本的代码,但是由于我正在使用 while 循环,我真的只能将文本文件的最后一行写入新文件。我正在尝试从testfile.txt 修改文本并将其写入一个名为mdemarco.txt 的新文件。我正在尝试做的修改是在每行前面添加一个行号。有没有人知道一种方法可以在运行时将 while 循环的内容写入字符串并将结果字符串输出到 mdemarco.txt 或类似的东西?

public class Writefile
{
public static void main(String[] args) throws IOException
{
  try
  {
     Scanner file = new Scanner(new File("testfile.txt"));
     File output = new File("mdemarco.txt");
     String s = "";
     String b = "";
     int n = 0;
     while(file.hasNext())
     {
        s = file.nextLine();
        n++;
        System.out.println(n+". "+s);
        b = (n+". "+s);
     }//end while
     PrintWriter printer = new PrintWriter(output);
     printer.println(b);
     printer.close();
  }//end try
     catch(FileNotFoundException fnfe)
  {
     System.out.println("Was not able to locate testfile.txt.");
  }
}//end main
}//end class

输入文件文本为:

do
re
me
fa 
so
la
te
do

而我得到的输出只有

8. do

有人可以帮忙吗?

【问题讨论】:

    标签: java text-files read-write


    【解决方案1】:

    String 变量 b 在循环的每次迭代中都会被覆盖。您想追加而不是覆盖(您可能还想在末尾添加换行符):

    b += (n + ". " + s + System.getProperty("line.separator"));
    

    更好的是,使用StringBuilder 附加输出:

    StringBuilder b = new StringBuilder();
    int n = 0;
    while (file.hasNext()) {
        s = file.nextLine();
        n++;
        System.out.println(n + ". " + s);
        b.append(n).append(". ").append(s).append(System.getProperty("line.separator"));
    }// end while
    PrintWriter printer = new PrintWriter(output);
    printer.println(b.toString());
    

    【讨论】:

    • 非常感谢!在写字板中工作正常!
    • 碰巧你知道为什么它没有在记事本中以新行打印吗?
    • @MichaelDeMarco 您需要使用\r\n 作为新的行组合。对于独立于平台的选择,请使用System.getProperty("line.separator")
    • 非常感谢您的帮助!
    【解决方案2】:

    您在每行文本中的内容未保存。所以只有最后一行显示在输出文件上。请试试这个:

    public static void main(String[] args) throws IOException {
        try {
            Scanner file = new Scanner(new File("src/testfile.txt"));
            File output = new File("src/mdemarco.txt");
            String s = "";
            String b = "";
            int n = 0;
            while (file.hasNext()) {
                s = file.nextLine();
                n++;
                System.out.println(n + ". " + s);
    
                //save your content here
                b = b + "\n" + (n + ". " + s);
                //end save your content
    
            }// end while
            PrintWriter printer = new PrintWriter(output);
            printer.println(b);
            printer.close();
        }// end try
        catch (FileNotFoundException fnfe) {
            System.out.println("Was not able to locate testfile.txt.");
        }
    }// end m
    

    【讨论】:

      【解决方案3】:

      试试这个:

      while(file.hasNextLine())
      

      代替:

      while(file.hasNext())
      

      b += (n+". "+s + "\n");
      

      代替:

      b = (n+". "+s);
      

      【讨论】:

      • 非常感谢!直到我查了一下才知道 Next() 和 NextLine() 之间的区别。
      【解决方案4】:

      将其更改为b += (n+". "+s);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-04-05
        • 1970-01-01
        • 2012-01-23
        • 1970-01-01
        • 1970-01-01
        • 2011-05-20
        • 2012-11-24
        相关资源
        最近更新 更多