将一个ArrayList数组通过BufferedWriter储存到“English.txt”文件中
public class ArrayListToTxtDemo {
public static void main(String[] args) throws IOException {
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("Hello");
arrayList.add("I'm fine");
arrayList.add("Thank you");
arrayList.add("And you?");
BufferedWriter bw = new BufferedWriter(new FileWriter("com.idea_primary\\English.txt"));
for (String s : arrayList){
bw.write(s);
bw.newLine();
bw.flush();
}
bw.close();
}
读取“English.txt”文件
public class TxTArrayListDemo {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("com.idea_primary\\English.txt"));
ArrayList<String> arrayList = new ArrayList<>();
String line;
while ((line = br.readLine())!=null){
arrayList.add(line);
}
br.close();
for (String s : arrayList){
System.out.println(s);
}
}
}
控制台输出:
Hello
I’m fine
Thank you
And you?