我试图允许我的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);
}
}
答案 0 :(得分:3)
您的资源文件应位于project classpath
或JVM classpath
。
由于您是从netbeans运行它,因此将其放在项目的root
文件夹下。这应该工作。您可以右键单击该项目并查看classpath
属性,然后查看folders/paths
设置为classpath
的内容。
当您独立运行程序而没有netbeans作为可执行文件时,您的资源文件应位于正在读取类文件的类文件夹下(作为 相对路径 )或在根文件夹中,根文件夹中包含 绝对路径 。
您还可以提及绝对系统路径 (c:\test\..)
,但不建议这样做。