我试图允许我的 java tcp 服务器上的用户将所需的连接 IP 放在文本文件中,但是当我要求程序读取我编写的测试文件时,它说找不到。有人可以告诉我将文件放在哪里以便项目可以找到它吗?如果有帮助,我正在使用 netbeans。
这是我的代码:
package chat;
import java.net.*;
import java.io.*;
import java.awt.*;
public class Chat extends Frame {
private Socket socket = null;
private DataInputStream console = null;
private DataInputStream fileStream = null;
private DataOutputStream streamOut = null;
private ChatClientThread client = null;
private TextArea display = new TextArea();
private TextField input = new TextField();
private Button send = new Button("Send"),
connect = new Button("Connect"),
quit = new Button("Bye"),
exit = new Button("Exit");
private String serverName;
private int serverPort;
public Chat() {
Panel keys = new Panel();
keys.setLayout(new GridLayout(1, 2));
keys.add(quit);
keys.add(connect);
Panel south = new Panel();
south.setLayout(new BorderLayout());
south.add("West", keys);
south.add("Center", input);
south.add("East", send);
Label title = new Label("Chat Room", Label.CENTER);
title.setFont(new Font("Helvetica", Font.BOLD, 14));
setLayout(new BorderLayout());
Panel Stuff = new Panel();
Stuff.add(title);
Stuff.add(exit);
add("North", Stuff);
add("Center", display);
add("South", south);
quit.disable();
send.disable();
display.setFocusable(false);
getParameters();
}
public boolean action(Event e, Object o) {
if (e.target == quit) {
input.setText(".bye");
send();
quit.disable();
send.disable();
connect.enable();
exit.enable();
} else if (e.target == connect) {
connect(serverName, serverPort);
} else if (e.target == send) {
send();
input.requestFocus();
} else if (e.target == exit) {
this.dispose();
}
return true;
}
public void connect(String serverName, int serverPort) {
println("Establishing connection. Please wait ...");
try {
socket = new Socket(serverName, serverPort);
println("Connected: " + socket);
open();
send.enable();
connect.disable();
quit.enable();
exit.disable();
} catch (UnknownHostException uhe) {
println("Host unknown: " + uhe.getMessage());
} catch (IOException ioe) {
println("Unexpected exception: " + ioe.getMessage());
}
}
private void send() {
try {
streamOut.writeUTF(input.getText());
streamOut.flush();
input.setText("");
} catch (IOException ioe) {
println("Sending error: " + ioe.getMessage());
close();
}
}
public void handle(String msg) {
if (msg.equals(".bye")) {
println("Good bye.");
close();
} else {
println(msg);
}
}
public void open() {
try {
streamOut = new DataOutputStream(socket.getOutputStream());
client = new ChatClientThread(this, socket);
} catch (IOException ioe) {
println("Error opening output stream: " + ioe);
}
}
public void close() {
try {
if (streamOut != null) {
streamOut.close();
}
if (socket != null) {
socket.close();
}
} catch (IOException ioe) {
println("Error closing ...");
}
client.close();
client.stop();
}
private void println(String msg) {
display.appendText(msg + "\n");
}
//This is the problem area.
public void getParameters() {
try
{
FileInputStream dataIn = new FileInputStream("IP.txt");
int k;
try {
while((k = dataIn.read()) != -1) {
serverName += (char)k;
}
dataIn.close();
}
catch(IOException ioe) {
println("Reading problem.");
}
println("Press 'connect' to connect to " + serverName);
}
catch (IOException ioe)
{
println("Problem finding IP address file.");
}
serverPort = 3000;
}
public static void main(String[] args) {
Chat client = new Chat();
client.setVisible(true);
client.setSize(400, 300);
}
}