我正在尝试将audio file
从服务器转移到客户端并将其写入另一个文件。 server
正在从客户端读取文件,但它正在连续忽略新行读取文件的内容。例如,有一个文本,如“我正在做客户端服务器程序”,在文件的下一行文本是“在Java中”。服务器读作“我正在做客户端服务器编程Java
”。
我的问题是如何逐行阅读内容..
我的代码如下......
*
服务器
**try
{
` try
{
System.out.println("Im server waiting for client request");
ss=new ServerSocket(2005);
Socket s=ss.accept();
InputStream is = s.getInputStream( );//Input Stream
BufferedReader fileRead =new BufferedReader(new InputStreamReader(is));
String fname = fileRead.readLine( );
OutputStream os = s.getOutputStream( );//socket output stream
PrintWriter pw = new PrintWriter(os, true);
BufferedReader br = new BufferedReader(new FileReader(fname) );//read File
String str;//initiating string and StringBuffer
StringBuilder sb=new StringBuilder();
while((str = br.readLine()) != null) // reading line-by-line from file
{
sb.append(str);//append to stringBuffer sb
}
System.out.println("The string is in server" +sb);
pw.println(sb);
pw.flush();
pw.close();
is.close();
fileRead.close();
os.close();
pw.close();
br.close();
}
catch(Exception e)
{
e.getMessage();
e.getStackTrace();
e.getSuppressed();
}`
The Client side::
try {
System.out.println("Im client ..sending request");
Socket s=new Socket("127.0.0.1",2005);//Initiate Socket
System.out.println("Enter filename");
BufferedReader bw=new BufferedReader(new InputStreamReader(System.in));//Take fileName from keyBoard
String filename=bw.readLine();//Read filename into a string
OutputStream dout=new DataOutputStream(s.getOutputStream());//output Stream
PrintWriter pw=new PrintWriter(dout,true);
pw.println(filename);//write filename to socket
InputStream is = s.getInputStream( );//Input Stream
BufferedReader in =new BufferedReader(new InputStreamReader(is));
String clstr;
StringBuilder sd=new StringBuilder();
while((clstr=in.readLine())!=null)
{
sd.append(clstr);
//clstr=in.readLine();//Read String From socket into "clstr" string
}
System.out.println("The string in client is" +sd);
File f=new File("A://music/kin.txt");//Open a file
if(!f.exists())
f.createNewFile();//if not existed create it
try
{
//PrintWriter bf=new PrintWriter(new FileWriter(f)); //Write to a file
BufferedWriter bf=new BufferedWriter(new FileWriter(f));
//bf.append(clstr);
//bf.write(sd);
bf.append(sd);
bf.flush();
bf.close();
}
catch(Exception e)
{
e.getMessage();
e.getSuppressed();
e.getStackTrace();
}
System.out.println("The Content written is" +sd);
bw.close();
pw.close();
dout.close();
in.close();
is.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 0 :(得分:6)
您的描述还不清楚,但我怀疑这是问题所在:
while((str = br.readLine()) != null) // reading line-by-line from file
{
sb.append(str);//append to stringBuffer sb
}
来自readLine()
的返回值不包含换行符......并且您不会在StringBuilder
中添加一个换行符。因此,如果您收到以下行:
结果将是“第一次第二次”。您只需要自己添加适当的换行符:
// Or whatever line break you want
String lineBreak = System.getProperty("line.separator");
while((str = br.readLine()) != null)
{
sb.append(str);
sb.append(lineBreak);
}
请注意,如果您确实 尝试传输此类音频文件,则不应该 - 音频文件是二进制数据,因此您应该只读取{ {1}};不要使用InputStream
,因为它是为 text 数据而设计的。