2

我试图允许我的 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);

    }
}
4

1 回答 1

3

您的资源文件应该在project classpathorJVM classpath中。

由于您是从 netbeans 运行它,因此请将其放在root项目的文件夹下。那应该行得通。您可以右键单击该项目并检查classpath 属性并查看folders/paths设置为classpath.

当您独立运行程序时,没有 netbeans,作为可执行文件,您的资源文件应该位于从中读取类文件的类文件夹下(作为相对路径)或在根文件夹中,具有根文件夹的绝对路径.

您也可以提及绝对系统路径(c:\test\..),但不推荐。

于 2013-06-27T00:58:58.140 回答