当前位置: 代码迷 >> Java相关 >> 文件内容 UTF怎么传啊
  详细解决方案

文件内容 UTF怎么传啊

热度:195   发布时间:2007-06-12 10:04:28.0
文件内容 UTF怎么传啊
import java.net.*;
import java.io.*;
class socket
{
public static void main(String[] args)
{
ServerSocket s=null;
try
{
s=new ServerSocket(3456);
}
catch(IOException e)
{

}
while(true)
{
try
{
Socket s1=s.accept();
DataOutputStream dos=new DataOutputStream(s1.getOutputStream());
FileReader input=new FileReader("server.java");
BufferedReader br=new BufferedReader(input);
String str=br.readLine();
while(str!=null)
{
dos.writeUTF(str);
str=br.readLine();
}
dos.close();
s1.close();
}
catch(IOException e)
{

}
}
}
}
服务器端
-----------------------------------

import java.net.*;
import java.io.*;
class testClient
{
public static void main(String[] args)
{
try
{
Socket s=new Socket("127.0.0.1",3456);
DataInputStream dis=new DataInputStream(s.getInputStream());
FileWriter fw=new FileWriter("temp.txt");
PrintWriter bw=new PrintWriter(fw);
String s1=dis.readUTF();
while(s1!=null)
{
System.out.println(s1);
bw.println(s1);
s1=dis.readUTF();
}
bw.close();
dis.close();
s.close();
}
catch(ConnectException con)
{
System.out.println("cant connect");
}catch(IOException e)
{
System.out.println("can,t work");
e.printStackTrace();
}
}
}
客户端
----------------------

怎么什么都没有呢
大家帮忙啊 这里谢过了

搜索更多相关的解决方案: UTF  文件  

----------------解决方案--------------------------------------------------------
输出什么?
----------------解决方案--------------------------------------------------------

什么也没有
while(s1!=null)
{
System.out.println(s1);
bw.println(s1);
s1=dis.readUTF();
}
这里改成输出一个信息的,他输出了
估计是文件访问越界
但是不知道怎么改

[此贴子已经被作者于2007-6-13 7:16:41编辑过]


----------------解决方案--------------------------------------------------------

你在服务器关了socket以后,客户端会收到一个异常,这个时候,你没有关掉文件输出流,所以文件里面会什么都没有
你应该加一个finally块,在里面关闭文件流


----------------解决方案--------------------------------------------------------
  相关解决方案