0

我正在尝试将一个audio file从服务器传输到客户端并将其写入另一个文件。正在从客户端读取文件,但它正在读取文件的server内容而忽略新行。例如,有一个像“我正在做客户端服务器程序”这样的文本,文件的下一行是“in 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();
        }
4

1 回答 1

6

您的描述还不清楚,但我怀疑这是问题所在:

while((str = br.readLine()) !=  null) // reading line-by-line from file
{
    sb.append(str);//append to stringBuffer sb
}

from 的返回值readLine() 包含换行符...并且您没有将一个换行符添加到您的StringBuilder. 因此,如果您收到以下行:

  • “第一的”
  • “第二”
  • “第三”

结果将只是“firstsecondthird”。您只需要自己添加一个适当的换行符:

// Or whatever line break you want
String lineBreak = System.getProperty("line.separator");
while((str = br.readLine()) != null)
{
    sb.append(str);
    sb.append(lineBreak);
}

请注意,如果你真的想像这样传输音频文件,你不应该 - 音频文件是二进制数据,所以你应该从InputStream; 根本不要使用 a Reader,因为它是为文本数据设计的。

于 2013-04-16T06:11:51.623 回答