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:
- Your class need to implement CharSequence to be eligible to be passed to Files.write.
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
How to Set Hard Limit on a Jcomponent When Setmaximumsize() and Setprefferedsize() Don't Work
Moving Items Around in an Arraylist
Creating Classes Dynamically with Java
Using Locales with Java's Tolowercase() and Touppercase()
Jaxb Marshalling Unmarshalling with Cdata
Multiple Clients Access the Server Concurrently
Casting a Class to an Unrelated Interface
Difference Between Oracle Jdbc Driver Classes
What Package Naming Convention Do You Use for Personal/Hobby Projects in Java
Cannot Find Main Class in File Compiled with Ant
Change a Method at Runtime via a Hot Swap Mechanism
How to Create a Class Literal of a Known Type: Class<List<String>>
Mocking Files in Java - Mock Contents - Mockito
Use of Java [Interfaces/Abstract Classes]
Java Keylistener VS Keybinding