IO

来源:互联网 发布:淘宝网站源码 编辑:程序博客网 时间:2024/06/01 22:16
<pre class="java" name="code">public class ReadFile
{
    
    private final String pathname;
    
    private final File file;
    
    private FileInputStream fis;
    
    private int filePoint=0;
    
    ReadFile(String pathname)
        throws FileNotFoundException
    {
        this.pathname = pathname;
        this.file = new File(pathname);
        
    }
    
    public void open()
        throws FileNotFoundException
    {
        
        this.fis = new FileInputStream(file);
        
    }
    
    public int read(byte[] b)
        throws IOException
    {
        
        return read(b, 0, b.length);
    }
    
    
    private int getFilePoint( )
    {
        return filePoint;
    }
    
    public int read(byte[] b, int off, int len)
        throws IOException
    {
        
        return  read(b, off, len,1);
        
    }
    
    public int read(byte[] b, int off, int len, int startReadLocation)
        throws IOException
    {
        int i = -1;
        if (parameterIsOk(b, off, len, startReadLocation) == false)
        {throw new IndexOutOfBoundsException();
        }
        else
        {
            
            int c = fis.read();
            if (c == -1)
            {
                return -1;
            }
            
              i = 1;
            try
            {
                for (; i &lt; startReadLocation; i++)
                {
                    c = fis.read();
                    if (c == -1)
                    { 
                        break;
                    }
                    
                }
                i = 0;
                for (; i &lt;  len ; i++)
                {
                    b[off + i] = (byte)c;
                    c = fis.read();
                    if (c == -1)
                    {
                        break;
                    }
                    
                }
                
            }
            catch (IOException e)
            {
            }
            
        }
        filePoint=startReadLocation+len;
        return i;
        
    }
    
    public boolean parameterIsOk(byte[] b, int off, int len, int startReadLocation)
    {
        
        boolean parameterIsOk = false;
        if (fis == null || b == null)
        {
            throw new NullPointerException();
        }
        else if (off &lt; 0 || len &lt;= 0 || len &gt; b.length - off)
        {
            throw new IndexOutOfBoundsException();
        }
        
        else
        {
            parameterIsOk = true;
        }
        return parameterIsOk;
    }
    
    public void close()
        throws IOException
    {
        fis.close();
    }</pre>
<br />
<br />
原创粉丝点击