1 for(int i = 0;i<array.size();i++){ 2 Goods g = array.get(i); 3 File file = new File("d://goodsInfo.txt"); 4 FileOutputStream fos1 = new FileOutputStream(file, true); 5 // 创建FileOutputStream对应的PrintStream,方便操作。PrintStream的写入接口更便利 6 PrintStream out1 = new PrintStream(fos1); 7 String str=""+g.getName()+","+g.getBarcode()+","+g.getPrice()+" "; 8 out1.print(str); 9 out1.close(); 10 11 }
" " 在写入一个对象后换行,
最后结果:
111,1234561234567,122.0
123,1234567890123,33.0
33,1234567891231,66.0
这样就能很轻易的再从文件中读取出来。