Ê×Ò³ Ê«´Ê ×Öµä °å±¨ ¾ä×Ó ÃûÑÔ ÓÑ´ð ÀøÖ¾ ѧУ ÍøÕ¾µØͼ
µ±Ç°Î»ÖÃ: Ê×Ò³ > ½Ì³ÌƵµÀ > JAVA > J2SE¿ª·¢ >

ioʲôʱºòÂÒÂëÓйØÎÊÌâ

2012-11-20 
ioʲôʱºòÂÒÂëÎÊÌâÒÔÏÂÈý¶Î³ÌÐò£¬ÎªÊ²Ã´£¨1£©¶ÁÖÐÎIJ»»áÂÒÂ룬£¨2£©»áÂÒÂ룬£¨3£©Ò²»áÂÒÂ룬Ö÷ÒªÊÇÏëÎÊ£¨1£©ÎªÊ²Ã´²»

ioʲôʱºòÂÒÂëÎÊÌâ
ÒÔÏÂÈý¶Î³ÌÐò£¬ÎªÊ²Ã´£¨1£©¶ÁÖÐÎIJ»»áÂÒÂ룬£¨2£©»áÂÒÂ룬£¨3£©Ò²»áÂÒÂ룬Ö÷ÒªÊÇÏëÎÊ£¨1£©ÎªÊ²Ã´²»»á,Ëû²»Ò²ÊÇÿ´Î¶ÁÒ»¸ö×Ö½ÚÂð£¿

£¨1£©
import java.io.File;
 import java.io.InputStream;
 import java.io.FileInputStream;
 public class InputStreamDemo04
 {
 public static void main(String[] args) throws Exception
 {
 InputStream in =null;
 in =new FileInputStream(new File("f:"+File.separator+"text.txt"));
 int temp=0;
 int len=0;
 byte[] b =new byte[1024];
 while((temp=in.read())!=-1)
 {
 b[len]=(byte)temp;
 len++;
 
 }
 in.close();
 System.out.println("³¤¶È£º"+len);
 System.out.println("ÊäÈëµÄÄÚÈÝÊÇ£º"+new String(b,0,len));
 }
 }
===============================================================
£¨2£©
import java.io.File;
 import java.io.InputStream;
 import java.io.FileInputStream;
 public class InputStreamDemo05
 {
 public static void main(String[] args) throws Exception
 {
 InputStream in =null;
 in =new FileInputStream(new File("f:"+File.separator+"text.txt"));
 StringBuffer s =new StringBuffer();
 int temp=0;
 int len=0;
 while((temp=in.read())!=-1)
 {
 char c =(char)temp;
 s.append(c);
 
 }
 in.close();
 System.out.println("ÊäÈëµÄÄÚÈÝÊÇ£º"+s);
 }
 }
==============================================
£¨3£©
import java.io.*;
 public class InputStreamDemo06
 {
 public static void main(String[] args) throws Exception
 {
 File f =new File("f:"+File.separator+"text.txt");
 InputStream in =null;
 in =new FileInputStream(f);
 byte[] b =new byte[4];
 int hasRead=0;
 while((hasRead=in.read(b))>0)
 {
 System.out.println("³¤¶ÈÊÇ£º"+hasRead);
 System.out.println(new String(b,0,hasRead));
 }
 }
 }

[½â¾ö°ì·¨]
£¨1£© byte[] b =new byte[1024];
£¨2£©byte[] b =new byte[4];
Ðֵܣ¬£¨1£©ºÍ£¨2£©Æäʵ¶¼»áÓÐÂÒÂëµÄ£º
Ò»¸öºº×ÖÊÇ2¸ö×Ö½Ú£¬£¨2£©Ã¿´Î¿ÉÒÔ¶Á³öµÄÊÇ2¸öÍêÕûµÄºº×Ó£¬
Èç¹ûÄãÒª¶Á³öµÄÄÚÈÝÈ«ÊǺº×Ó×Ö·ûÒ²ÊǺº×Ó¸ñʽµÄ£¬ÄÇô²»»áÓÐÂÒÂë³öÏÖ
Èç¹ûÄãËù¶ÁµÄÄÚÈÝÖÐÓкº×ÖºÍżÊý¸ö×Öĸ,²¢ÇÒ×Öĸ¶¼ÔÚÒ»Æð£¬ÄÇôҲ²»»áÓÐÂÒÂë
ÕâÑù˵ÆðÀ´£¬£¨1£©¿Ï¶¨»áÓÐÂÒÂëµÄ£¬ÆäÒ»´Î¿ÉÒÔÍêÕûµÄ¶Á³ö512¸öºº×Ö£¬ÒªÊÇÄã¶ÁÊÇÄÚÈÝÊÇ£º
aºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Öºº×Ö...£¨Ê¡ÂԺźóÃæÈ«ÊǺº×Ö£¬²¢ÇÒÊÇÔÚÒ»ÐУ¬ÄÇô¿Ï¶¨»áÓÐÂÒÂëµÄ£©



[½â¾ö°ì·¨]
Îı¾ÎļþÇëʹÓà Reader¡£

Ò»¸öºº×Ö¶àÉÙ×Ö½ÚºÍÄãÑ¡ÔñÁËʲô±àÂëÓйء£
[½â¾ö°ì·¨]
Ò»¸öºº×Ö¿ÉÄÜÊÇ2,3,»òÕß4¸ö×Ö½Ú£¬ÕâºÍ±àÂë¸ñʽÓйء£


̽ÌÖ
£¨1£© byte[] b =new byte[1024];
£¨2£©byte[] b =new byte[4];
Ðֵܣ¬£¨1£©ºÍ£¨2£©Æäʵ¶¼»áÓÐÂÒÂëµÄ£º
Ò»¸öºº×ÖÊÇ2¸ö×Ö½Ú£¬£¨2£©Ã¿´Î¿ÉÒÔ¶Á³öµÄÊÇ2¸öÍêÕûµÄºº×Ó£¬
Èç¹ûÄãÒª¶Á³öµÄÄÚÈÝÈ«ÊǺº×Ó×Ö·ûÒ²ÊǺº×Ó¸ñʽµÄ£¬ÄÇô²»»áÓÐÂÒÂë³öÏÖ
Èç¹ûÄãËù¶ÁµÄÄÚÈÝÖÐÓкº×ÖºÍżÊý¸ö×Öĸ,²¢ÇÒ×Öĸ¶¼ÔÚÒ»Æð£¬ÄÇôҲ²»»áÓÐÂÒÂë
ÕâÑù˵ÆðÀ´£¬£¨1£©¿Ï¶¨»áÓÐÂÒ¡­¡­

ÈȵãÅÅÐÐ