写一个arraylist到文件

时间:2015-03-02 19:16:14

标签: java arrays arraylist printwriter

我在使用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");
  }

有人能告诉我在将数组中的所有项目写入输出文件时应该做些什么吗?谢谢你:))

4 个答案:

答案 0 :(得分: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);
           }
       }
   }

答案 1 :(得分:2)

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 :(得分:1)

这是有效的代码。需要在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();
        }

    }
}

答案 3 :(得分:0)

需要考虑的一些事项:

  • 关闭PrintWriter
  • 由于这是不需要的,因此不会将第二个FileOutputStream称为fo
  • 确保您通过file.newFile()
  • 创建文件output.txt
相关问题