这里我正在附加代码片段,该代码片段应该选择文件并逐行读取该文件并将该数据添加到JTextArea
。
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
File selectedFile;
FileReader reader = null;
BufferedReader in;
String inputLine = "";
double port1 = Math.random();
long portNo = Math.round(Math.random() * 10000);
System.out.println("portNo"+portNo);
int LineNumber = 0;
try {
final JFileChooser fc = new JFileChooser();
DataInputStream inp=null;
ServerSocket ss=new ServerSocket((int) portNo);
Socket socket=new Socket("localhost",(int) portNo);
Socket socket1=ss.accept();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
txtSearch.setText((fc.showOpenDialog(CreateNode.this) == JFileChooser.APPROVE_OPTION) ? fc.getSelectedFile().toString() : txtSearch.getText());
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//gets file from dialog
selectedFile = fc.getSelectedFile();
//makes sure files can be processed before proceeding
if (selectedFile.canRead() && selectedFile.exists()) {
//System.out.println("can read:"+selectedFile.canRead()+"exists:"+selectedFile.exists());
reader = new FileReader(selectedFile);
}
in = new BufferedReader(reader);
//inputLine recieves file text
DataOutputStream dos4=new DataOutputStream(socket.getOutputStream());
while ((inputLine=in.readLine())!=null ) {
//LineNumber isn't needed, but it adds a line count on the left, nice
// inputLine=in.readLine();
System.out.println("inputline"+inputLine+"LineNumber:"+LineNumber);
LineNumber++;
fileData.append(LineNumber + ": " + inputLine + "\n");
//next line in File opened
dos4.writeUTF(LineNumber + ": " + inputLine + "\n");
}
dos4.close();
inp=new DataInputStream(socket1.getInputStream());
String input = (String)inp.readUTF();
//close stream, files stops loading
in.close();
}
catch (Exception e) {
System.out.println("Exception e:"+e);
e.printStackTrace();
}
}
这里没有显示任何错误,它在控制台(sysout
)中显示数据,但它没有附加到文本区域。
答案 0 :(得分:2)
没有@mahender Reddy yasa它没有连接到套接字服务器只是看看这些步骤是不是
1)启动服务器
2)尝试将客户端与服务器连接
答案 1 :(得分:1)
按照
更改代码 File selectedFile;
FileReader reader = null;
BufferedReader in;
try {
final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
txtSearch.setText((fc.showOpenDialog(CreateNode.this) == JFileChooser.APPROVE_OPTION) ? fc.getSelectedFile().toString() : txtSearch.getText());
if (fc.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
//gets file from dialog
selectedFile = fc.getSelectedFile();
//makes sure files can be processed before proceeding
if (selectedFile.canRead() && selectedFile.exists()) {
//System.out.println("can read:"+selectedFile.canRead()+"exists:"+selectedFile.exists());
reader = new FileReader(selectedFile);
}
}
in = new BufferedReader(reader);
//inputLine recieves file text
byte[] buffer;
String inputLine = "";
double port1 = Math.random();
long portNo = Math.round(Math.random() * 10000);
System.out.println("portNo"+portNo);
int LineNumber = 0;
while ((inputLine =in.readLine())!= null) {
//LineNumber isn't needed, but it adds a line count on the left, nice
LineNumber++;
// StringTokenizer tokenizer = new StringTokenizer(inputLine);
ServerSocket ss=new ServerSocket((int) portNo);
Socket socket=new Socket("localhost",(int) portNo);
//displays text file
fileData.append(LineNumber + ": " + inputLine + "\n");
// System.out.println("connected:"+socket.isConnected());
// ss.accept();
DataOutputStream dos4=new DataOutputStream(socket.getOutputStream());
//next line in File opened
dos4.writeUTF(LineNumber + ": " + inputLine + "\n");
dos4.close();
socket.close();
Socket socket1=ss.accept();
DataInputStream inp=new DataInputStream(socket1.getInputStream());
//String msg=inp.readUTF();
//System.out.println("msg:"+msg);
String input = (String)inp.readUTF();
System.out.println("inputline from socket:"+input);
inp.close();
ss.close();
}
//close stream, files stops loading
in.close();
// TODO add your handling code here:
}
catch (Exception e) {
System.out.println("Exception e:"+e);
e.printStackTrace();
}
}