我无法在Java中的文本文件中写入多行。

huangapple go评论76阅读模式
英文:

I can't write on multiple lines in a txt file in java

问题

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
public class Main {
    public static void main( String args[]) {
         int a = 32;
         int b=12;
         int c=33;

          List<Integer> myList = new ArrayList();
          myList.add(a);
          myList.add(b);
          myList.add(c);
          
          try{
              File myObj = new File("filename.txt");
              if(myObj.createNewFile()){
                  System.out.println("File created " + myObj.getName());

              }
              else
              {
                  System.out.println("File already exists");
              }

          }
          catch (IOException e)
          {
              System.out.println("An error has occurred");
              e.printStackTrace();
          }
         
          
          try {
            FileWriter myWriter = new FileWriter("filename.txt");
            for(int i=1;i<10;i++)
            {
            myWriter.append("This is a new file, nothing sus here."+i + " ");
            }
            myWriter.close();
            System.out.println("Successfully wrote to the file.");
          } catch (IOException e) {
            System.out.println("An error occurred.");
            e.printStackTrace();
          }
                
    }
    
}
英文:

So I'm trying to write in a text file, nothing too complicated, but for some reason the new text that i want to add doesn't change lines, it keeps going on the same line, and I can't figure out why. The irrelevant parts are being commented so don't worry about them.

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import java.io.File;
import java.io.IOException;
import java.io.FileWriter;
public class Main {
    public static void main( String args[]) {
         int a = 32;
         int b=12;
         int c=33;

          List&lt;Integer&gt; myList = new ArrayList();
          myList.add(a);
          myList.add(b);
          myList.add(c);
         /* for(int s:myList)
          {
              System.out.println(s);
          }
          */
          //Om ar= new Om(&quot;Alex&quot;,21,185);
          //System.out.println(ar);

          try{
              File myObj = new File(&quot;filename.txt&quot;);
              if(myObj.createNewFile()){
                  System.out.println(&quot;File created &quot; + myObj.getName());

              }
              else
              {
                  System.out.println(&quot;File already exists&quot;);
              }

          }
          catch (IOException e)
          {
              System.out.println(&quot;An error has occurred&quot;);
              e.printStackTrace();
          }
         
          
          try {
            FileWriter myWriter = new FileWriter(&quot;filename.txt&quot;);
            for(int i=1;i&lt;10;i++)
            {
            myWriter.append(&quot;This is a new file, nothing sus here.&quot;+i + &quot; &quot;);
            }
            myWriter.close();
            System.out.println(&quot;Successfully wrote to the file.&quot;);
          } catch (IOException e) {
            System.out.println(&quot;An error occurred.&quot;);
            e.printStackTrace();
          }
                
    }
    
}

答案1

得分: 1

  1. 将你的 FileWriter 包装在一个 BufferedWriter 中,以提高向文件写入的效率。
  2. 然后,你可以使用 BufferedWriter 的 newLine() 方法按需要向文件添加换行字符串。newLine() 方法会根据你当前的平台写出适当的换行字符串。
英文:
  1. Wrap your FileWriter in a BufferedWriter to make writing to the file more efficient.
  2. Then you can use the newLine() method of the BufferedWriter to add a newline String to the file as you require. The newLine() method will write out the appropriate string for your current platform.

huangapple
  • 本文由 发表于 2020年9月7日 01:01:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63766719.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定