本帖最后由 杨兴庭 于 2013-7-25 22:56 编辑
下面代码中readLine()已经写到最后一行后,再继续readLine()读的值应该是null,为什么line的值不是null呢,而且还要定义结束标记?
- import java.io.*;
- import java.net.*;
- class TextClient2
- {
- public static void main(String[] args) throws Exception
- {
- Socket s = new Socket("127.0.0.1",10086);
- BufferedReader bufr = new BufferedReader(new FileReader("xx.java"));
- PrintWriter out = new PrintWriter(s.getOutputStream(),true);
- String line = null;
- while((line=bufr.readLine())!=null)
- {
- out.println(line);
- }
- //这里line已经写到最后一行了,最后一行不已经是null了吗,也就是line=null,为什么还要定义一个结束标记呢?
- out.println("over");
- BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
- String str = bufin.readLine();
- System.out.println(str);
- //bufin.close();为什么这里不关闭bufin缓冲区呢?
- bufr.close();
- s.close();
- }
- }
- class TextServer2
- {
- public static void main(String[] args) throws Exception
- {
- ServerSocket ss = new ServerSocket(10086);
- Socket s = ss.accept();
- String ip = s.getInetAddress().getHostAddress();
- System.out.println(ip+"....connection");
- BufferedReader bufin = new BufferedReader(new InputStreamReader(s.getInputStream()));
- PrintWriter out = new PrintWriter(new FileWriter("server.txt"),true);
- String line = null;
- while ((line=bufin.readLine())!=null)
- {
- if("over".equals(line))
- break;
- out.println(line);
- }
- PrintWriter pw = new PrintWriter(s.getOutputStream(),true);
- pw.println("上传完成");
-
- //bufin.close();这里为什么不关闭bufin缓冲区呢?
- out.close();
- s.close();
- ss.close();
- }
- }
复制代码 |