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

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

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

问题

  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.FileWriter;
  7. public class Main {
  8. public static void main( String args[]) {
  9. int a = 32;
  10. int b=12;
  11. int c=33;
  12. List<Integer> myList = new ArrayList();
  13. myList.add(a);
  14. myList.add(b);
  15. myList.add(c);
  16. try{
  17. File myObj = new File("filename.txt");
  18. if(myObj.createNewFile()){
  19. System.out.println("File created " + myObj.getName());
  20. }
  21. else
  22. {
  23. System.out.println("File already exists");
  24. }
  25. }
  26. catch (IOException e)
  27. {
  28. System.out.println("An error has occurred");
  29. e.printStackTrace();
  30. }
  31. try {
  32. FileWriter myWriter = new FileWriter("filename.txt");
  33. for(int i=1;i<10;i++)
  34. {
  35. myWriter.append("This is a new file, nothing sus here."+i + " ");
  36. }
  37. myWriter.close();
  38. System.out.println("Successfully wrote to the file.");
  39. } catch (IOException e) {
  40. System.out.println("An error occurred.");
  41. e.printStackTrace();
  42. }
  43. }
  44. }
英文:

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.

  1. import java.util.List;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.io.File;
  5. import java.io.IOException;
  6. import java.io.FileWriter;
  7. public class Main {
  8. public static void main( String args[]) {
  9. int a = 32;
  10. int b=12;
  11. int c=33;
  12. List&lt;Integer&gt; myList = new ArrayList();
  13. myList.add(a);
  14. myList.add(b);
  15. myList.add(c);
  16. /* for(int s:myList)
  17. {
  18. System.out.println(s);
  19. }
  20. */
  21. //Om ar= new Om(&quot;Alex&quot;,21,185);
  22. //System.out.println(ar);
  23. try{
  24. File myObj = new File(&quot;filename.txt&quot;);
  25. if(myObj.createNewFile()){
  26. System.out.println(&quot;File created &quot; + myObj.getName());
  27. }
  28. else
  29. {
  30. System.out.println(&quot;File already exists&quot;);
  31. }
  32. }
  33. catch (IOException e)
  34. {
  35. System.out.println(&quot;An error has occurred&quot;);
  36. e.printStackTrace();
  37. }
  38. try {
  39. FileWriter myWriter = new FileWriter(&quot;filename.txt&quot;);
  40. for(int i=1;i&lt;10;i++)
  41. {
  42. myWriter.append(&quot;This is a new file, nothing sus here.&quot;+i + &quot; &quot;);
  43. }
  44. myWriter.close();
  45. System.out.println(&quot;Successfully wrote to the file.&quot;);
  46. } catch (IOException e) {
  47. System.out.println(&quot;An error occurred.&quot;);
  48. e.printStackTrace();
  49. }
  50. }
  51. }

答案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:

确定