我正在向我的客户端发送流。如何在客户端中逐行将该流读取到客户端的文本区域中?
我用于发送数据的服务器代码:
public class Server extensions javax.swing.JFrame {
public Server()
{
int server=5555;
initComponents();
}
jRadioButton1.setText("a");
jRadioButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jRadioButton1ActionPerformed(evt);
}
});
jRadioButton2.setText("b");
jRadioButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jRadioButton2ActionPerformed(evt);
}
});
jRadioButton3.setText("c");
jRadioButton3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jRadioButton3ActionPerformed(evt);
}
});
jRadioButton4.setText("d");
jRadioButton4.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jRadioButton4ActionPerformed(evt);
}
});
jButton1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
jButton1.setText("SEND");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
Greedy gg=new Greedy();
gg.greedy();
jButton1ActionPerformed(evt);
}
});
jMenu1.setText("OPEN FILE");
jMenu1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
OPEN(evt);
}
});
jMenuItem3.setText("OPEN");
jMenuItem3.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
String cwd=System.getProperty("user.dir");
JFileChooser jfc=new JFileChooser(cwd);
Component frame = null;
if (jfc.showOpenDialog(frame) !=JFileChooser.APPROVE_OPTION)
return;
File f = jfc.getSelectedFile();
try {
content1(f.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
readin(f.toString(), jTextArea1);
}
});
private void readin(String string, JTextArea jTextArea12)
{
try
{
FileReader fr = new FileReader(string);
jTextArea1.read(fr, null);
fr.close();
}
catch (IOException e)
{
System.err.println(e);
}
}
private void EXIT(java.awt.event.ActionEvent evt)
{
System.exit(0);
}
private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void jRadioButton3ActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void jRadioButton4ActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
你好(); }
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
public void run()
{
new Server().setVisible(true);
System.out.println("SERVER IS STARTED.....");
}
});
int port=5000;
while(true)
{
try
{
ServerSocket ser=new ServerSocket(port+10);
System.out.println("CLIENT A IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser=new ServerSocket(port+20);
ser.accept();
System.out.println("CLIENT B IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser=new ServerSocket(port+30);
ser.accept();
System.out.println("CLIENT C IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
try
{
ServerSocket ser=new ServerSocket(port+40);
ser.accept();
System.out.println("CLIENT D IS CONNECTED");
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public void hello()
{
try
{
FileReader buf=new FileReader("e:\buffer.txt");
int port=5010;
try
{
@SuppressWarnings("resource")
ServerSocket ser=new ServerSocket(port);
Socket soc=ser.accept();
BufferedReader toclient=new BufferedReader(buf);
DataOutputStream dos=new DataOutputStream(soc.getOutputStream());
System.out.println(dos.toString());
dos.flush();
dos.close();
}catch(Exception e)
{
}
}
catch(Exception e)
{
System.out.println(e+"BUFFER IS EMPTY");
}
}
public void content1(String s) throws IOException {
try (BufferedReader br = new BufferedReader(new FileReader(s));
PrintWriter pr = new PrintWriter(new File("e:\buffer.txt"))) {
for (String line; (line = br.readLine()) != null;) {
pr.println(line);
}
}
}
}
客户端代码:
/* * 要更改此模板,请选择"工具"|"模板 * 并在编辑器中打开模板。 */
public class NodeA extends javax.swing.JFrame
{
public JLabel sys=new JLabel();
public NodeA()
{
initComponents();
}
@SuppressWarnings("unchecked")
private void initComponents() {
jLabel1.setText("NODE A");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
jButton1.setText("CONNECT");
jButton1.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton1ActionPerformed(evt);
}
});
jButton2.setFont(new java.awt.Font("Tahoma", 1, 14));
jButton2.setForeground(new java.awt.Color(255, 0, 51));
jButton2.setText("OFFLOAD DATA");
jButton2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jButton2ActionPerformed(evt);
}
});
jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "B", "C", "D" }));
jComboBox2.addActionListener(new java.awt.event.ActionListener()
{
public void actionPerformed(java.awt.event.ActionEvent evt)
{
jComboBox2ActionPerformed(evt);
}
});
jLabel2.setText("SELECT NODE TO OFFLOAD");
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
try
{
Socket client=new Socket("localhost",5010);
if(client.isConnected())
{
System.out.println("SERVER ACCEPTS CONNECTION");
}
}
catch(Exception e)
{
System.out.println("SERVER IS BUSY.......");
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt)
{
}
private void jComboBox2ActionPerformed(java.awt.event.ActionEvent evt)
{
}
public void data()
{
DataInputStream is;
BufferedReader bsr=new BufferedReader(new InputStreamReader(System.in));
}
}
我想读取服务器流,并在客户端文本区域中一次显示一行的数据。有人可以帮助我吗?
FileReader b = new FileReader("read.txt");
jTextArea.read(b, "read.txt"); // we're done!
有关详细信息,请参阅文档JTextComponent.(Reader,Object)
。