java—如何将arraylist逐行追加到txt文件中?

myzjeezk  于 2021-06-30  发布在  Java
关注(0)|答案(1)|浏览(534)

我正在尝试将arraylist的一项添加到txt文件中,但我需要逐行添加。txt文件包含名称,我正在尝试添加它们的用户名,所以我需要逐行添加每个用户。这是原始的txt文件:

  1. Smith, Will
  2. Lothbrok, Ragnar
  3. Skywalker, Anakin
  4. Ronaldo, Cristiano
  5. Messi, Lionel

这是我使用的方法:

  1. public static void addUsers(int maxLines, List<Users> users) throws IOException {
  2. File f = new File("Users.txt");
  3. FileOutputStream fos = new FileOutputStream(f, true);
  4. BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
  5. //maxLines is just a count of the lines from the text file so i can put the limit of this loop.
  6. for (int i = 0; i < maxLines; i++) {
  7. bw.write(" > " + users.get(i).getUsername() );
  8. bw.newLine();
  9. }
  10. bw.close();
  11. }

我得到的结果是:

  1. Smith, Will
  2. Lothbrok, Ragnar
  3. Skywalker, Anakin
  4. Ronaldo, Cristiano
  5. Messi, Lionel
  6. > Will.Smith3
  7. > Ragnar.Lothbrok74
  8. > Anakin.Skywalker30
  9. > Cristiano.Ronaldo32
  10. > Lionel.Messi2

但我需要这样:

  1. Smith, Will > Will.Smith3
  2. Lothbrok, Ragnar > Ragnar.Lothbrok74
  3. Skywalker, Anakin > Anakin.Skywalker30
  4. Ronaldo, Cristiano > Cristiano.Ronaldo32
  5. Messi, Lionel > Lionel.Messi2

我一直在尝试不同的方法,比如在bufferedwriter方法中添加append not write,但仍然得到相同的结果。我怎样才能做得更好?

gopyfrb3

gopyfrb31#

应该使用try with resources自动关闭资源。
将文件中的行以及相应的用户名存储到 List<String> 读取文件时。读取完成后,将此列表的内容写入文件。
演示:

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileReader;
  5. import java.io.FileWriter;
  6. import java.io.IOException;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. class User {
  10. private String username;
  11. public User(String username) {
  12. this.username = username;
  13. }
  14. public String getUsername() {
  15. return username;
  16. }
  17. }
  18. public class Main {
  19. public static void main(String[] args) {
  20. // Test
  21. List<User> users = List.of(new User("Will.Smith3"), new User("Ragnar.Lothbrok74"),
  22. new User("Anakin.Skywalker30"), new User("Cristiano.Ronaldo32"), new User("Lionel.Messi2"));
  23. try {
  24. addUsers(5, users);
  25. } catch (IOException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. static void addUsers(int maxLines, List<User> users) throws IOException {
  30. // List to store lines from the file plus corresponding username
  31. List<String> list = new ArrayList<>();
  32. try (BufferedReader reader = new BufferedReader(new FileReader(new File("Users.txt")))) {
  33. String currentLine;
  34. int line = 0;
  35. while ((currentLine = reader.readLine()) != null && line < users.size()) {
  36. list.add(line, currentLine + " > " + users.get(line).getUsername() + System.lineSeparator());
  37. line++;
  38. }
  39. }
  40. // Write the content of the list into the file
  41. try (BufferedWriter writer = new BufferedWriter(new FileWriter(new File("Users.txt")))) {
  42. for (String s : list) {
  43. writer.write(s);
  44. }
  45. }
  46. }
  47. }
展开查看全部

相关问题