我想找一个流对象对文本文件既能读又能写,开始我用了RandomAccessFile,但它只成功地读写了一次,就把文本文件改成二进制的了,并且无法继续按要求读写了。这段函数如下:
private class OrderAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
String name=orderName.getText();
String card=orderCardId.getText();
String num=orderAmount.getText();
String fid=orderFlightId.getText();
String re=orderRe.getText();
StringBuffer s = new StringBuffer();
TicketOrderInformation toi= new TicketOrderInformation(name,card,Integer.parseInt(num),fid,re);
Node toiNnde = new Node(toi,null);
s.append(toi.getOrderFormId()+" "+name+" "+card+" "+num+" "+fid+" "+re+"\r\n");
DataOutputStream outs = new DataOutputStream(new FileOutputStream("TicketOrderInformation.txt",true));
outs.write(s.toString().getBytes());
outs.close();
orderName.setText(null);
orderCardId.setText(null);
orderAmount.setText(null);
orderFlightId.setText(null);
orderRe.setText(null);
String[] str=new String[8]; //用来装一条信息
String temp=null;
RandomAccessFile buffer=new RandomAccessFile ("AirlineInformation.txt","rw");
while((temp=buffer.readLine())!=null)
{
str =temp.split(" ");
if(str[0].equals(fid))
{
Integer left=Integer.parseInt(str[7]);
left-=1;System.out.println(left);
buffer.writeChars(str[0]+str[1]+str[2]+str[3]+str[4]+str[5]+str[6]+left.toString()+"\r\n");
buffer.close();
}
}
}
catch(Exception e)
{
}
}
}我的文件格式TicketOrderInformation.txt:每行6个字符串,用空格隔开,代表一条订票信息
TicketOrderInformation.txt:每行8个字符串,用空格隔开,代表一条航班信息
private class OrderAction implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
try
{
String name=orderName.getText();
String card=orderCardId.getText();
String num=orderAmount.getText();
String fid=orderFlightId.getText();
String re=orderRe.getText();
StringBuffer s = new StringBuffer();
TicketOrderInformation toi= new TicketOrderInformation(name,card,Integer.parseInt(num),fid,re);
Node toiNnde = new Node(toi,null);
s.append(toi.getOrderFormId()+" "+name+" "+card+" "+num+" "+fid+" "+re+"\r\n");
DataOutputStream outs = new DataOutputStream(new FileOutputStream("TicketOrderInformation.txt",true));
outs.write(s.toString().getBytes());
outs.close();
orderName.setText(null);
orderCardId.setText(null);
orderAmount.setText(null);
orderFlightId.setText(null);
orderRe.setText(null);
String[] str=new String[8]; //用来装一条信息
String temp=null;
RandomAccessFile buffer=new RandomAccessFile ("AirlineInformation.txt","rw");
while((temp=buffer.readLine())!=null)
{
str =temp.split(" ");
if(str[0].equals(fid))
{
Integer left=Integer.parseInt(str[7]);
left-=1;System.out.println(left);
buffer.writeChars(str[0]+str[1]+str[2]+str[3]+str[4]+str[5]+str[6]+left.toString()+"\r\n");
buffer.close();
}
}
}
catch(Exception e)
{
}
}
}我的文件格式TicketOrderInformation.txt:每行6个字符串,用空格隔开,代表一条订票信息
TicketOrderInformation.txt:每行8个字符串,用空格隔开,代表一条航班信息
你用什么写进去的,原则上就要用什么读。否则就要自己进行转换。
用BufferedFileWriter写可能比较方便
try {
BufferedWriter out = new BufferedWriter(new FileWriter("outfilename"));
out.write("aString");
out.close();
} catch (IOException e) {
}
DataUnputStream