try
{
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
}catch (IOException e) {}
在构造FileWriter对象时添加一个参数就可以了!
你试试看!
{
BufferedWriter out = new BufferedWriter(new FileWriter("filename", true));
out.write("aString");
out.close();
}catch (IOException e) {}
在构造FileWriter对象时添加一个参数就可以了!
你试试看!
不知我的观点对不对?你的思路是怎样实现的!
这个方法是可行的。
两个线程互斥,我以前写过一段程序就是这样做的,完全可以
对同一个文件进行输入输出处理! 可否 share 代码?
The Thread Test Example
**************/
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class ThreadTest extends JFrame
{
private boolean available = false;
private static BufferedReader in = null;
private static BufferedWriter out = null;
private char str[] = new char[1];
private int finish = 1;
JButton read = null,
write = null;
JTextArea readArea = null,
writeArea = null;
JPanel buttonPane = null,
textPane =null;
static WriteThread wt = null;
static ReadThread rt = null;
boolean readstart = false , writestart = false ; class readListener implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()=="read")
{
readArea.setText("");available = true; readstart = true ;rt.start();
}
else
{
writeArea.setText("");available = false ;writestart = true ;wt.start();
}
}
}
public ThreadTest()
{
super("线程测试示例");
Container pane = getContentPane();
pane.setLayout(new BorderLayout(0,6));
buttonPane = new JPanel();
read = new JButton("按下开始读文件");
read.setFont(new Font("宋体", Font.PLAIN, 20));
read.setActionCommand("read");
read.addActionListener(new readListener());
write = new JButton("按下开始写文件");
write.setFont(new Font("宋体", Font.PLAIN, 20));
write.setActionCommand("write");
write.addActionListener(new readListener());
buttonPane.setLayout(new GridLayout(1,2,10,0));
buttonPane.add(read);
buttonPane.add(write);
pane.add(buttonPane,BorderLayout.NORTH);
textPane = new JPanel();
readArea = new JTextArea(20,20);
readArea.setEditable(true);
readArea.setText("");
readArea.setLineWrap(true);
readArea.setWrapStyleWord(true);
writeArea = new JTextArea(20,20);
writeArea.setEditable(true);
writeArea.setText("");
writeArea.setLineWrap(true);
writeArea.setWrapStyleWord(true);
textPane.setLayout(new GridLayout(1,2,10,0));
textPane.add(new JScrollPane(readArea));
textPane.add(new JScrollPane(writeArea));
pane.add(textPane,BorderLayout.SOUTH);
setSize(400,200);
}
public synchronized int readOperation()
{
int flag = 0;
while (available == false )
{
try {
read.setText("读线程堵塞");
wait();
} catch (InterruptedException e) { }
}
try{
if ( (in.read(str))!=(-1) )
{
read.setText("读线程开始");
readArea.append(new String(str));
if (finish == 1 && writestart == true)
{available = false;}
notifyAll();
}
else
{
flag = (-1);
available = false;
readstart = false ;
finish = 0;
in.close();
read.setText("读线程结束");
notifyAll();
}
}catch(Exception e){System.out.println(" read error");}
return flag;
}
public synchronized void writeOperation(String st,int over)
{
if (over ==0)
{
try{out.flush();}catch(Exception e){}
available = true;
writestart = false ;
finish = 0;
try{ out.close(); }catch(Exception e){}
write.setText("写线程结束");
notifyAll();
return;
}
while (available == true )
{
try {
write.setText("写线程阻塞");
wait();
} catch (InterruptedException e) { }
}
try {
write.setText("写线程开始");
out.flush();
out.write(st);
}catch(Exception e){System.out.println(" write error");}
writeArea.append(st);
if (finish == 1 && readstart == true)
available = true;
notifyAll();
}
public static void main(String[] args)
{
ThreadTest tt = new ThreadTest();
try {
in = new BufferedReader(new FileReader("a.txt"));
out = new BufferedWriter (new FileWriter("a.txt",true));
}catch(Exception e){ System.out.println(" initial error");} wt = new WriteThread(tt);
rt = new ReadThread(tt);
tt.pack();
tt.setLocation(200,70);
tt.setDefaultCloseOperation(EXIT_ON_CLOSE);
tt.setVisible(true);
}
}/**************
The ReadThread Test Example
**************/
import java.io.*;public class ReadThread extends Thread
{
private ThreadTest fileOperation;
public ReadThread(ThreadTest fp)
{
fileOperation = fp;
}
public void run() {
while ( (fileOperation.readOperation())!=(-1) )
{
try {
sleep((int)(Math.random() * 100));
}catch (InterruptedException e) { }
}
}
}/**************
The WriteThread Test Example
**************/
import java.io.*;public class WriteThread extends Thread
{
private ThreadTest fileOperation;
private BufferedReader rf = null;
char ss[] = new char[1];
public WriteThread(ThreadTest fp)
{
fileOperation = fp;
try {
rf = new BufferedReader(new FileReader("outputtext.txt"));
}catch(Exception e){}
}
public void run()
{
try{
while( (rf.read(ss))!=(-1) )
{
fileOperation.writeOperation((new String(ss)),1);
try {
sleep((int)(Math.random() * 100));
}catch (InterruptedException e) { }
}
fileOperation.writeOperation((new String(ss)),0);
}catch(Exception e){}
}
}
回家先,各位不用送了。:)