Modify contents of text file and write to new file

2019-09-07 11:12发布

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?

4条回答
啃猪蹄的小仙女
2楼-- · 2019-09-07 11:49

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

查看更多
We Are One
3楼-- · 2019-09-07 11:53

Try this:

while(file.hasNextLine())

instead of:

while(file.hasNext())

and

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

instead of:

b = (n+". "+s);
查看更多
放我归山
4楼-- · 2019-09-07 11:56

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
查看更多
Evening l夕情丶
5楼-- · 2019-09-07 12:07

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());
查看更多
登录 后发表回答