java 将数组列表写入文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28817715/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 14:13:28  来源:igfitidea点击:

writing an arraylist to file

javaarraysarraylistprintwriter

提问by skittels

I'm having some trouble writing an array list to file using printwriter. I've tried another way that worked but it wouldn't print all the things from the array list just one. This is the way I'm trying at the moment and it won't print anything.

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

Can anyone tell me what i should be doing to write all the items in the array to the output file? thank you :)

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

回答by Hyman Hopner

Possibly because you weren't closing your streams, try:

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

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);
           }
       }
   }

回答by Ankush

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();
}

回答by Mak

Here's the code that works. Need to flush/close streams in finally block.

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

    }
}

回答by ask

Some things to consider:

需要考虑的一些事项:

  • Closing your PrintWriter
  • Not making a second FileOutputStreamcalled fosince this is unneeded
  • Making sure you're creating your file output.txt via file.newFile()
  • 关闭你的 PrintWriter
  • FileOutputStream打电话,fo因为这是不需要的
  • 确保您通过以下方式创建文件 output.txt file.newFile()