2010-07-16 134 views
0

我想問的是我可以用文件做些什麼?哪個Stream是文件發送?應該將文件更改爲另一個數據?如何將文件從客戶端發送到服務器到另一個客戶端?

+0

這個問題太詳細了。 – McDowell 2010-07-16 11:56:45

+0

你必須具體說明1)你真的想用這個文件做什麼(讀取/操作到另一個結構)2)你的問題是不明確的:在什麼情況下你的意思是'服務器'?是內聯網還是互聯網?永遠具體,讓你可以得到答案... – Venkat 2010-07-16 12:20:24

回答

0

您可以使用InputStream來讀取文件,並將其數據寫入SocketOutputStream

這可能是這個樣子:

OutputStream out = null; 
FileInputStream in = null; 

try { 
    // Input from file 
    String pathname = "path/to/file.dat"; 
    File file = new File(pathname); 
    in = new FileInputStream(file); 

    // Output to socket 
    String host = "10.0.1.8"; 
    int port = 6077; 
    Socket socket = new Socket(host, port); 
    socket.connect(endpoint); // TODO: define endpoint 
    out = socket.getOutputStream(); 

    // Transfer 
    while (in.available() > 0) { 
     out.write(in.read()); 
    } 

} catch (Exception e) { 
    // TODO: handle exception 

} finally { 
    if (out != null) 
     out.close(); 
    if (in != null) 
     in.close(); 
} 

PS:我不知道這是否實際工作。這是爲了讓你開始...

相關問題