package cn.dzr.uploadapp;import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;public class UploadClient
{ public static void main(String[] args) throws IOException
{
/*
   */

System.out.println("客服端开始启动.......");
Socket s = new Socket(InetAddress.getLocalHost(),10005);

File file = new File("D:"+File.separator + "Client/Client.txt");

FileInputStream fs = new FileInputStream(file);
//该流用于从字符串读取数据
BufferedReader bufr = new BufferedReader(new InputStreamReader
(fs));
// System.out.println(file.canRead());
// System.out.println(file.canWrite());
// System.out.println(file.canExecute());
// System.out.println(file.toURI());

//该流用于将数据传输到socket流中.
BufferedOutputStream out = new BufferedOutputStream(new 
DataOutputStream(s.getOutputStream()));

PrintWriter pw = new PrintWriter(s.getOutputStream());

String str = null;

P(System.currentTimeMillis());
while((str =bufr.readLine())!=null)
{
out.write(str.getBytes());
pw.println(str);
}
P(System.currentTimeMillis());

pw.close();
out.close();
bufr.close();
fs.close();

s.close();
}
[code]
while((str =bufr.readLine())!=null)
{
out.write(str.getBytes());
pw.println(str);
}[/code]
1,这一段代码,只能输入一次,这里,out无法写入数据,为什么呢?是因为pw绑定后,原来out绑定就失效了么?????

解决方案 »

  1.   

    肯定是你写的哪里有问题。我写了一个测试代码,测试了完全没有问题。 public static void main(String [] args) {
    BufferedWriter bw = null;
    PrintWriter pw = null;
    OutputStream out = null;
    try {
    out = new FileOutputStream("c:\\a.txt");
    bw = new BufferedWriter(new OutputStreamWriter(out));
    pw = new PrintWriter(out);

    bw.write("bw write\r\n");
    pw.println("pw.println");
    bw.flush();
    pw.flush();
    } catch (Exception e) {
    e.printStackTrace();
    } finally {
    try {
    out.close();
    } catch (Exception e) {
    }
    }
    }
    最终打开文件得到的结果是
    bw write
    pw.println
      

  2.   

    我那个是socket中的流,和这个可能有关系么?我刚刚分别注释掉一个,都能成功运行。
    但注释了pw,out则无法写入数据....
      

  3.   

            while((str =bufr.readLine())!=null)
            {
                out.write(str.getBytes());
                pw.println(str);
            }这程序死循环了!
    另外肯定是可以写的,可能你服务端没的上到而已