日期:2014-05-20 浏览次数:20893 次
List list = new ArrayList(); String[] strread =new String[8]; String str =""; str =gpid+","+gpname+","+gpcomment+","+update+","+operator+","+"0"; String s =""; try { File out = new File("D:/redsun/groupinfo.txt"); File in = new File("D:/redsun/groupinfo.txt"); BufferedWriter output = new BufferedWriter(new FileWriter(out));//在这又把.Txt文件都清空了 output.write(str); output.flush(); output.newLine(); output.close(); BufferedReader input = new BufferedReader(new FileReader(in)); while((s = input.readLine())!=null){ strread =s.split(","); if("0".equals(strread[5])){ list.add(strread[1]); } } input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; }
BufferedWriter output = new BufferedWriter(new FileWriter(out,true));//
------解决方案--------------------
改成
output.append(str);
------解决方案--------------------
BufferedReader input = new BufferedReader(new FileReader(in)); //在这里又清空了
------解决方案--------------------
import java.io.*; import java.util.ArrayList; import java.util.List; import java.util.Iterator; public class WriteFile { /** * @param args */ private String gpid="00031"; private String gpname="xmlbook"; private String gpcomment="this is good book for readers"; private String update="2008-03-26"; private String operator = "add + "; public static void main(String[] args) { // TODO Auto-generated method stub List<String> output = new WriteFile().writeEndFile(); Iterator<String> item = output.iterator(); while(item.hasNext()){ System.out.println(item.next()); } } public List<String> writeEndFile(){ ArrayList<String> list = new ArrayList<String>(); String[] strread =new String[8]; String str =""; str =gpid+","+gpname+","+gpcomment+","+update+","+operator+","+"0"; String s =""; try { File out = new File("D:/redsun/groupinfo.txt"); FileOutputStream output = new FileOutputStream(out,true);//在这又把.Txt文件都清空了 output.write(str.getBytes()); output.flush(); output.close(); System.out.println("File writing has done!!"); File in = new File("D:/redsun/groupinfo.txt"); BufferedReader input = new BufferedReader(new FileReader(in)); while((s = input.readLine())!=null){ strread =s.split(","); if("0".equals(strread[strread.length-1])){ list.add(strread[1]); } } input.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return list; } }