package test;java
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.List;get
public class test {
public static void main(String[] args) {
//建立集合
List<String> list=new ArrayList<String>();
list.add("name");
list.add("臧三");
list.add("name");
list.add("李四");
list.add("name");
list.add("李四");
list.add("name");
list.add("李四");
try {
//將集合數據寫入磁盤
FileWriter fw=new FileWriter("D:\\name.txt");
for(String s:list){
fw.write(s);
fw.write("\r\n");
}
fw.close();
//讀取磁盤上文件數據
BufferedReader br = new BufferedReader(new FileReader("D:\\name.txt"));
String line;
//將讀取到的數據寫入到list集合中
List<String> list1=new ArrayList<String>();
while((line=br.readLine())!=null){
list1.add(line);
}
//循環輸出list集合
for(int i=0;i<list1.size();i++){
System.out.println(list1.get(i));
}
br.close();
} catch (Exception e) {
}
}
}it