2013-06-27 2 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 classpath 또는 JVM classpath에 있어야합니다 :

여기 내 코드입니다.

netbeans에서 실행 중이므로 프로젝트의 root 폴더에 넣으십시오. 그게 효과가있다. 프로젝트를 마우스 오른쪽 버튼으로 클릭하고 속성을 확인하고 folders/pathsclasspath으로 설정된 것을 확인하십시오. 당신은 실행 파일, 리소스 파일이 클래스 파일을 읽어되고있는 클래스 폴더 아래에 있어야한다, 넷빈즈없이 독립적으로 프로그램을 실행

(A 상대 경로로) 또는에서 루트 폴더에서 절대 경로이있는 루트 폴더.

절대 시스템 경로(c:\test\..)을 언급 할 수도 있지만 권장하지는 않습니다.