2013-06-27 39 views
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); 

    } 
} 
+0

IP.txt將在%APPLICATION%/ IP.txt中。所以無論你在何時從執行運行時啓動「java」。多年來我沒有使用Netbeans,但我想這是在你的項目的「根」文件夾中。 –

+0

你可以嘗試刪除不相關的gui代碼,將你的問題集中在你的實際問題上。 – assylias

回答

3

資源文件應在project classpathJVM classpath

既然你是從netbeans運行它,把它放在你項目的root文件夾下。這應該工作。您可以右鍵單擊該項目並檢查classpath屬性並查看folders/paths設置爲classpath

當你獨立運行程序,而無需NetBeans中,作爲一個可執行文件,你的資源文件應該在哪裏被讀取類文件的類文件夾下(作爲相對路徑),或在根文件夾與您的根文件夾中的絕對路徑

您還可以提及絕對系統路徑(c:\test\..)但不推薦。