【问题标题】:writing an arraylist to file将数组列表写入文件
【发布时间】:2015-03-02 19:16:13
【问题描述】:

我在使用 printwriter 将数组列表写入文件时遇到了一些问题。我尝试了另一种可行的方法,但它不会只打印数组列表中的所有内容。这是我目前正在尝试的方式,它不会打印任何内容。

datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");

public void writer() throws FileNotFoundException, IOException{ 


   PrintWriter pw = new PrintWriter(new FileOutputStream(file));
   FileOutputStream fo = new FileOutputStream(file);
   int datList = datArrayList.size();

   for (int i = 0; i < datList; i++){
    pw.write(datArrayList.get(i).toString() + "\n");
  }

谁能告诉我应该怎么做才能将数组中的所有项目写入输出文件?谢谢你:)

【问题讨论】:

  • 要将对象写入文件,您应该考虑序列化而不是文本。
  • 您正在为同一个文件打开两个 FileOutputStreams,并且只使用其中一个。删除你不使用的那个。
  • 您发布的代码不完整。发布MCVE
  • close() 您的流,或使用try-with-resources 语句。
  • datArrayList中有没有对象???

标签: java arrays arraylist printwriter


【解决方案1】:
datArrayList = new ArrayList<theAccounts>();
File file = new File("output.txt");

public void writer() throws FileNotFoundException, IOException { 
   FileOutputStream fo = new FileOutputStream(file);
   PrintWriter pw = new PrintWriter(fo);
   int datList = datArrayList.size();

   for (theAccounts elem : datArrayList){
       pw.println(elem);
   }
   pw.close();
   fo.close();
}

【讨论】:

    【解决方案2】:

    可能是因为您没有关闭流,请尝试:

    datArrayList = new ArrayList<theAccounts>();
    File file = new File("output.txt");
    
       public void writer() throws FileNotFoundException, IOException { 
           try(PrintWriter pw = new PrintWriter(new FileOutputStream(file))){
               int datList = datArrayList.size();
    
               for (theAccounts s : datArrayList){
                   pw.println(s);
               }
           }
       }
    

    【讨论】:

    • fo 未使用,关闭 pw 应在 finally 块中或使用 try-with-resources 完成。
    【解决方案3】:

    这是有效的代码。需要在 finally 块中刷新/关闭流。

    package com.sto.sanbox;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.List;
    
    public class Accounts {
    
        public class Account {
    
            String name;
            String amount;
    
            public Account(String name, String amount) {
                super();
                this.name = name;
                this.amount = amount;
            }
    
            public String getName() {
                return name;
            }
    
            public void setName(String name) {
                this.name = name;
            }
    
            public String getAmount() {
                return amount;
            }
    
            public void setAmount(String amount) {
                this.amount = amount;
            }
    
            public String toString() {
                return this.getName() + ", " + this.getAmount();
            }
    
        }
    
        public  void writer(ArrayList<Account> datArrayList) throws IOException {
    
            PrintWriter pw = null;
            FileOutputStream fo = null;
            File file = null;
            try {
                file = new File("output.txt");
                pw = new PrintWriter(new FileOutputStream(file));
                fo = new FileOutputStream(file);
                int datList = datArrayList.size();
                for (int i = 0; i < datList; i++) {
                    pw.write(datArrayList.get(i).toString() + "\n");
                }
            } finally {
                pw.flush();
                pw.close();
                fo.close();
            }
    
        }
    
        public static void main(String args[]) {
            Accounts Writer = new Accounts();
            ArrayList<Account> datArrayList = new ArrayList<Account>();
            Account account = Writer.new Account(" Name" , " 100000");
            datArrayList.add(account);
    
            try {
                Writer.writer(datArrayList);
            } catch (IOException e) {
                e.printStackTrace();
            }
    
        }
    }
    

    【讨论】:

      【解决方案4】:

      需要考虑的一些事项:

      • 关闭您的PrintWriter
      • 没有第二个FileOutputStream 调用fo,因为这是不需要的
      • 确保您正在通过file.newFile() 创建文件 output.txt

      【讨论】:

        猜你喜欢
        • 2016-09-07
        • 2014-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-28
        • 2016-07-25
        相关资源
        最近更新 更多