import java.io.*;
import java.util.*;
public class Demo {
public static void main(String[] args) throws Exception {
List list = new ArrayList();
for(int i = 0; i < 10; i++){
list.add("zhangsan:"+i);
}
FileOutputStream fos = new FileOutputStream("D:\\a.txt");
for(Iterator it = list.iterator(); it.hasNext();){ //便利arrylist
String str = (String)it.next(); //将list中的元素转为str遍历给String
fos.write(str.getBytes()); //字节流转为byte数组写入
fos.write("\r\n".getBytes()); //代表windows系统的换行。
}
fos.close();
}
}
//小例子,仅供参考,另外异常我直接throws了,需要自己捕捉一下吧。
ListoutList = new ArrayList ();
OutputStream fOut = null;
try {
fOut = new FileOutputStream(new File("D:/test.txt"));
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(String Str:outList){
try {
fOut.write((Str+"\n").getBytes());
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(fOut != null){
try {
fOut.close();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}