How to Write an Arraylist of Strings into a Text File

How to write an ArrayList of Strings into a text file?

import java.io.FileWriter;
...
FileWriter writer = new FileWriter("output.txt");
for(String str: arr) {
writer.write(str + System.lineSeparator());
}
writer.close();

How to write an ArrayList into a text file in Android App

You can use google gson.

    class Four {
String first,second;
int third,fourth;
public Four()
{

}
}
//save_array(your_context,"MyData.txt",your_array)
void save_array(Context contxt,String file_name,ArrayList<Four> testes)
{
Gson gson = new Gson();
String json = gson.toJson(testes);
String root = contxt.getExternalFilesDir(null).getAbsolutePath() + "/arrays";

File file = new File(root);
file.mkdirs();
try {
File out_file = new File(file, file_name);
if(out_file.exists()){
out_file.delete();
}
FileWriter writer = new FileWriter(out_file,true);
writer.append(json);
writer.flush();
writer.close();
}catch (Exception ex)
{

}
}
ArrayList<Four> saved_array(Context contxt, String file_name) throws IOException {
String path = contxt.getExternalFilesDir(null).getAbsolutePath() + "/arrays/"+file_name;

Gson gson = new Gson();
BufferedReader br = new BufferedReader(new FileReader(path));
return gson.fromJson(br, new TypeToken<List<Four>>(){}.getType());

}

you can call as
Remember to request for storage permissions

Insert line by line data in txt file with arraylist

Use :

outStream.write(contactsinformations.get(i) + "\n");

Here \n signifies the new line.

Writing into text file from ArrayList that has multiple values per ArrayList object (java)

BufferedWriter has a newLine() method, you may use that if you declare output as a BufferedWriter .

private static void saveFile(ArrayList<Vehicle> vehs){
File fileName = new File("VehicleList.txt");

try {
FileWriter fw = new FileWriter(fileName);
BufferedWriter output = new BufferedWriter(fw);

for (int i = 0; i < vehs.size(); i++){
output.write(vehs.get(i).toString());
output.newLine();
}

output.close();

} catch (Exception e) {
JOptionPane.showMessageDialog(null, "I cannot create that file");
}
}

Also for the javadoc of the method :

Writes a line separator. The line separator string is defined by the
system property line.separator, and is not necessarily a single
newline ('\n') character.

Saving ArrayList to Text File

There are two changes:

  1. Your class need to implement CharSequence to be eligible to be passed to Files.write.
  2. You need to over-ride toString method to specify how your contents look like when saved.
    I can see output after above two changes.

        class Animals implements CharSequence {

    public int id;
    public String type, vertebrate, aclass;

    public Animals(int id,String type,String vertebrate,String aclass) {
    this.id = id;
    this.type = type;
    this.vertebrate = vertebrate;
    this.aclass = aclass;
    }

    public int getID() {
    return id;
    }

    public String getType() {
    return type;
    }

    public String getVert() {
    return vertebrate;
    }

    public String getaclass() {
    return aclass;
    }

    @Override
    public int length() {
    return toString().length();
    }

    @Override
    public char charAt(int index) {
    return toString().charAt(index);
    }

    @Override
    public CharSequence subSequence(int start, int end) {
    return toString().subSequence(start, end);
    }

    /* (non-Javadoc)
    * @see java.lang.Object#toString()
    */
    @Override
    public String toString() {
    return "Animals [id=" + id + ", type=" + type + ", vertebrate=" + vertebrate + ", aclass=" + aclass + "]";
    }

    }


Related Topics



Leave a reply



Submit