Modify contents of text file and write to new file in java

↘锁芯ラ 提交于 2020-01-16 18:35:30

问题


So I've got the basic code for this however due to the while loop I'm using, I can really only write the last line of the text file to the new file. I'm trying to modify the text from testfile.txt and write it to a new file named mdemarco.txt. The modification I'm trying to do is add a line number in front of each line. Does anybody know a way to maybe write the contents of the while loop to a string while it runs and output the resulting string to mdemarco.txt or anything like that?

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

The input file text is:

do
re
me
fa 
so
la
te
do

And the output I'm getting is only

8. do

Can anybody help?


回答1:


The String variable b is overwriten in each iteration of the loop. You want to append to it instead of overwriting (you may also want to add a newline character at the end):

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

Better yet, use a StringBuilder to append the output:

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());



回答2:


Your content on each line of text didn't saved. So only the last line displays on the output file. Please try this:

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:


Try this:

while(file.hasNextLine())

instead of:

while(file.hasNext())

and

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

instead of:

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



回答4:


Change it to b += (n+". "+s);.



来源:https://stackoverflow.com/questions/29456696/modify-contents-of-text-file-and-write-to-new-file-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!