Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2298462
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2010-08-06 17:05:29

先看下面一段代码:

view plaincopy to clipboardprint?
//同过判断文件的结尾来读取文件  
import java.io.File;  
import java.io.InputStream;  
import java.io.FileInputStream;  
public class InputStreamDemo02  
{  
    public static void main(String args[]) throws Exception{  
        File f = new File("E:"+File.separator+"java2"+File.separator+"StreamDemo"+File.separator+"test.txt");  
        InputStream in = new FileInputStream(f);  
        byte b[] = new byte[1024];  
        int len = 0;  
        int temp=0;          //所有读取的内容都使用temp接收  
        while((temp=in.read())!=-1){    //当没有读取完时,继续读取  
            b[len]=(byte)temp;  
            len++;  
        }  
        in.close();  
        System.out.println(new String(b,0,len));  
    }  

//同过判断文件的结尾来读取文件
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
public class InputStreamDemo02
{
 public static void main(String args[]) throws Exception{
  File f = new File("E:"+File.separator+"java2"+File.separator+"StreamDemo"+File.separator+"test.txt");
  InputStream in = new FileInputStream(f);
  byte b[] = new byte[1024];
  int len = 0;
  int temp=0;          //所有读取的内容都使用temp接收
  while((temp=in.read())!=-1){    //当没有读取完时,继续读取
   b[len]=(byte)temp;
   len++;
  }
  in.close();
  System.out.println(new String(b,0,len));
 }
}

运行结果为:Hello,java

在此代码中如果我不使用temp进行接收每次读取的内容,而直接操作每次读取的内容会怎么样呢?看下面的代码:

view plaincopy to clipboardprint?
//同过判断文件的结尾来读取文件,不使用temp对读取的每个字节进行接收时,对比InputStreamDemo01.java  
import java.io.File;  
import java.io.InputStream;  
import java.io.FileInputStream;  
public class InputStreamDemo03  
{  
    public static void main(String args[]) throws Exception{  
        File f = new File("E:"+File.separator+"java2"+File.separator+"StreamDemo"+File.separator+"test.txt");  
        InputStream in = new FileInputStream(f);  
        byte b[] = new byte[1024];  
        int len = 0;  
        //int temp=0;  
        while((in.read())!=-1){    //当没有读取完时,继续读取  
            b[len]=(byte)in.read();       
            len++;  
        }  
        System.out.println(new String(b,0,len));  
    }  

//同过判断文件的结尾来读取文件,不使用temp对读取的每个字节进行接收时,对比InputStreamDemo01.java
import java.io.File;
import java.io.InputStream;
import java.io.FileInputStream;
public class InputStreamDemo03
{
 public static void main(String args[]) throws Exception{
  File f = new File("E:"+File.separator+"java2"+File.separator+"StreamDemo"+File.separator+"test.txt");
  InputStream in = new FileInputStream(f);
  byte b[] = new byte[1024];
  int len = 0;
  //int temp=0;
  while((in.read())!=-1){    //当没有读取完时,继续读取
   b[len]=(byte)in.read();    
   len++;
  }
  System.out.println(new String(b,0,len));
 }
}

运行结果为:el,aa

造成这一结果的原因是什么?

观察可以发现,运行结果是跳跃的,每一个内容与上一个内容都间隔了一个字符。再阅读代码发现:in.read()被调用的两次,所以原因也就出来了:原因是有两次调用in.read(),in.read()是读取下一字节,导致b[]中存储的是跳跃的,即每次存入其中的都与上一个内容相差一个字节。问题并不复杂,重要的是自己书写时可能会造成这样的错误,所以一定要使用temp对读取的内容进行接收,同时只操作temp。发现问题和解决问题时,细心耐心很重要。

 

 

阅读(17374) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~