Java tcp サーバーのユーザーが目的の接続 IP をテキスト ファイルに配置できるようにしようとしていますが、作成したテスト ファイルをプログラムに読み取らせると、見つからないと表示されます。プロジェクトがファイルを見つけられるように、ファイルの場所を教えてもらえますか? それが役立つ場合、私はネットビーンズを使用しています。
これが私のコードです:
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);
}
}