我想将此ArrayList添加到现有的.txt文件中

时间:2017-11-24 04:18:59

标签: java

我想知道如何将这个arraylist添加到现有的.txt文件中。

Person personid = new Person(title,firstName,lastName,address,emailAddress,         homePhone,cellPhone,status,dateOfBirth);

    ArrayList<String> personList = new ArrayList<>();
    personList.add(title); 
    personList.add(firstName); 
    personList.add(lastName);
    personList.add(address);
    personList.add(emailAddress);
    personList.add(cellPhone);  
    personList.add(status);
    personList.add(dateOfBirth);

    try { 
     File file = new File("memberlog.txt");
     /*If file gets created then the createNewFile() 
      * method would return true or if the file is 
      * already present it would return false
      */
         boolean fvar = file.createNewFile();
     if (fvar){
          System.out.println("File has been created successfully");
     }
    else{
           System.out.println("File already present at the specified 
    location");
     }
    } catch (IOException e) {
        System.out.println("Exception Occurred:");
        e.printStackTrace();
    }

5 个答案:

答案 0 :(得分:1)

ArrayList<String> personList = new ArrayList<>();
        personList.add(title);
        personList.add(firstName);
        personList.add(lastName);
        personList.add(address);
        personList.add(emailAddress);
        personList.add(cellPhone);
        personList.add(status);
        personList.add(dateOfBirth);

        try {
            File file = new File("memberlog.txt");

            boolean fvar = file.createNewFile();
            if (fvar) {
                System.out.println("File has been created successfully");
                //Create a file writer
                FileWriter fw = new FileWriter("memberlog.txt");// should have fill path as parameter
                //loop throw your array list
                for (String item : personList) {
                    //write each string to your file line by line
                    fw.write(item);
                }
                //close the file writer
                fw.close();
            } else {
                System.out.println("File already present at the specified location");
            }
        } catch (IOException e) {
            System.out.println("Exception Occurred:");
            e.printStackTrace();
        }

答案 1 :(得分:0)

你可以使用这样的东西

public static void main(String[] args) throws FileNotFoundException {
    ArrayList<String> personList = new ArrayList<>();
    personList.add("hai");
    PrintWriter printWriter = new PrintWriter(new FileOutputStream("D:\\cicd poc\\abc.txt"));
    for (String s : personList) {
        printWriter.println(s);
    }
    printWriter.close();
}

如果要附加到文件,请使用此

public static void main(String[] args) throws IOException {
    ArrayList<String> personList = new ArrayList<>();
    personList.add("appending and writing");
    personList.add("line by line");
    BufferedWriter output = new BufferedWriter(new FileWriter("D:\\cicd poc\\abc.txt", true));
    for (String s : personList) {
        output.append(s + "\n");
    }
    output.close();
}

答案 2 :(得分:0)

尝试使用此代码

  public static void main(String[] args)
    {
    ArrayList<String> personList = new ArrayList<>();
    personList.add("title");
    personList.add("firstName");
    personList.add("lastName");
    personList.add("address");
    personList.add("emailAddress");
    personList.add("cellPhone");
    personList.add("status");
    personList.add("dateOfBirth");


        try(FileOutputStream fos = new FileOutputStream("memberlog.txt"))
        {
              ObjectOutputStream oos = new ObjectOutputStream(fos);

              oos.writeObject(personList);
        }


     catch (IOException e) {
        System.out.println("Exception Occurred:");
        e.printStackTrace();
    }

    }

答案 3 :(得分:0)

首先导入java.io。*;

然后在括号中创建FileWriter对象,检查文件是否存在: -

FileWriter fout=new FileWriter(“memberlog.txt”);

现在使用write()方法使用引用写入文件。

但是既然你已经创建了一个数组列表,其中所有数据都使用for循环来从ArrayList中的每个索引中获取数据。

我正在使用advance for循环 -

for(String s: personList)
{
 fout.write(s);
}

现在在写入所有数据时没有其他问题

fout.flush();和fout.close();最后。

答案 4 :(得分:0)

Apache Commons IO可以帮助您完成此操作。 如果你想逐行写:

source

或者可能最好先将所有人连接到一个大字符串,然后将其写入文件:

import org.apache.commons.io.FileUtils;
...
String separator = System.lineSeparator();
for (String person : personList) {
   FileUtils.writeStringToFile(file, person + separator, true)
}