🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: cn.bbs.comp.program
1 messages
1 total messages Started by ���ܵ�Сʨ Sat, 18 Apr 1998 00:00
���Java I/O���ܵ���������
#1255
Author: ���ܵ�Сʨ
Date: Sat, 18 Apr 1998 00:00
173 lines
10076 bytes
  ����JDK1.0.2�ṩ��Java.io����Ʋ���,Java������/����ѳ�Ϊ
�����ڶ�JavaӦ�õ�ƿ��������Ĺؼ���java.io�еĴ�����඼û
�л�����ʵ����,�л��������ֻ��Buffe redInputStream��Buffe
redOutputStream,���������ṩ�ķ����dz����ޡ�����,���������
����ص�Ӧ����,��Ҫ���ж��ļ������﷨����,����ֻ��DataInputS
tream��֧��readLine ����,�����û�����õĻ�����,readLine����
ʵ���������ַ�"\n"��"\r\n"Ϊһ�еĽ綨��,һ���ַ�һ���ַ��
���������ÿ�ε��ַ��ȡ����ʹ�ļ�������/�����ø���,������
��һ�����ļ�ʱI/OЧ�ʸ��,��û�ṩ������������,����һ����
�����ֽڵ��ļ�����Ҫ��ȡ�������ַ�
    �µ�JDK1.1������һ��Reader���Writer�������Java������/
������ܡ��ڴ�����ļ�ʱ,BufferedReader��֧�ֵ�readLine����
��DataInputStream��֧�ֵ�readLine��������Ҫ��10��20�����ź�
����,JDK1.1�����ܽ�����е��������⡣����,��Ҫ��һ�����ļ���
���﷨����ȴ���뽫������ڴ�������,RandomAccessFile��һ��
�����õ���,��JDK 
    1.1��û��Ϊ�����û�����,��û���ṩ��Ӧ��Reader�ࡣ�����
��I/O����ʱ,JDK1.1���Գ���Ч�ʵ��µ�ȱ����

    ��δ����뻺�����йص�I/O����
    �����ļ�I/OЧ�ʵ��µ�����,������Ҫһ�������û������Rand
omAccessFile�ࡣΪ���ܼ̳д��������еķ���,���Ƕ���һ��Rando
mAccessFile�������,����ΪBraf(Buffered randomaccessfile)��
    public class Braf extends RandomAccessFile{
    }
    Ϊ�����Ч��,���Ƕ���һ��byte�ͻ��������char�ͻ�����
������buf��end��buf��pos��real��pos��������¼�������е���Ч
��ַ:
    byte buffer[];
    int buf��end=0;
    int buf��pos=0;
    long real��pos=0;
    ����һ������˵���������С�IJ���,��дBraf��Ĺ��췽��:
    public Braf(String filename,String mode,int bufsize)thro
ws IOException{
    super(filename,mode);
    invalidate();
    BUF��SIZE=bufsize;
    buffer=new byte[BUF��SIZE];
    }
    ���ظ����read����,ʹ�µ�read�����������ȴӻ������ж�ȡ
���,ֱ���������еĿռ�����,�ŵ��ø����е�read��������Ϊ����
����ռ��þ�ʱ,�µ�read���������fillBuf fer��������仺����
���ڷ���fillBuffer��,���ø��౾���read����������Ȩ��private
 ��invalidate����������ָʾ��������������Ч���ݡ���seek��
�����ļ�ָ���Ƴ�������ʱ,invalidate������ָʾ���Ե÷dz���Ҫ
��
    public final int read() throws IOException{
    if(buf��pos >=buf��end) {
        if(fillBuffer() < 0)
       return -1;
    }
    if(buf��end == 0) {
    return-1;
    }else{
        return buffer[buf��pos++];
    }
    }
    private int fillBuffer() throws IOException{
      int n=super.read(buffer,0,BUF��SIZE);
      if(n>=0){
        real��pos +=n;
        buf��end = n;
        buf��pos = 0;
      }
      return n;
    }
    private void invalidate() throws IOException{
      buf��end = 0;
      buf��pos = 0;
      real��pos=super.getFilePointer();
    }
    ������һ�����в����read�������������㹻�Ļ�����,�µ�rea
d�������򵥵���Syst em.arraycopy,�Ѳ��ֻ�����ֱ�ӿ������û�
�������ڴ���readLine������getNextLine������,read�����õ���
������,����ʹJava��I/O���ܼ������ߡ���read�����Ĵ�������:
    public int read(byte b[],int off,int len)throws IOExcept
ion{
        int leftover=buf��end-buf��pos;
        if(len <= leftover) {
       System.arraycopy(buffer,buf��pos,b,off,len);
       buf��pos +=len;
       return len;
    }
    for(int i=0; i<len; i++){
       int c=this.read();
       if(c !=-1)
    b[off+i��=(byte)c;
      else{
    return i;
      }
     }
     return len;
    }
    Ϊ�˳�����û���������Ǵ����ı���,�����getFilePointer
������seek����Ҳ��Ҫ����,���غ���·����󲿷�ʱ���ڻ������
�в���,ʵ�ָ�Ϊ�򵥡�
      public long getFilePointer() throws IOException{
      long l = real��pos;
      return (l-buf��end+buf��pos);
    }
    public void seek(long pos)throws IOException{
      int n=(int)(real��pos��pos);
      if(n>=0 && n<=buf��end) {
        buf��pos=buf��end-n;
      }else{
        super.seek(pos);
        invalidate();
      }
    }
    ��Ҫ����ָ������,��Ϊ������readLine�����������final,��
�Dz��ܶ�����м򵥵�����,������һ���·���getNextLine����,��
����readLine������getNextLine���������жϻ��������Ƿ���δ��
ȡ������,���û��,��Ҫ��仺��������ڻ���������δ��ȡ����
�ݲ��ҵ����еĽ綨��,�ʹӻ������ж�ȡһ������,������ת����St
ring������,�ͼ򵥵ص���read����һ���ֽ�һ���ֽڵض�ȡ����Ȼ
��һ���ֵĴ����븸���е�readLine����������,����Ϊ�����е�rea
d�������õ��˻�����,��ʹJava��I/O����Ҫ�õöࡣ
    /*
    **return a next line in string
    */
        public final string getNextLine() throws IOException
{
     String str=null;
     if(buf��end-buf��pos<=0) {
        if(fillBuffer()<0){
       throw new IOException("error in filling buffer!");
    }
      }
      int lineend = -1;
      for(int i=buf��pos;i<buf��end; i++){
      if(buffer[i]==��\n��){
       lineend=i;
       break;
       }
       }
       if(lineend < 0){
       StringBuffer input=new StringBuffer(256);
       int c;
       while(((c=read())!=-1)&&(c !=��\n��)){
       input.append((char)c);
       }
       if((c==-1)&&(input.length()==0)) {
        return null;
       }
       return input.toString();
        }
        if(lineend > 0 && buffer[lineend-1]==��\r��)
       str=new String(buffer,0,buf��pos,lineend-buf��pos -1)
;
        else
      str=new String(buffer,0,buf��pos,lineend-buf��pos);
        buf��pos=lineend+1;
        return str;
    }
    ������,��Ҫ�Դ����ļ����������﷨����ʱ,����Braf�����ܱ�
RandomAccessFile�����������25����
    ����������ķ���Ҳ��������Щ�ļ�I/O�����ϼ��еĵط���
    �ر�Synchronization
    �����������۵�����/���������,����JavaЧ�ܵ���һ��������
synchronized����塣ͨ��,һ��synchronized�����Ŀ�֧��Լ��ͨ
��������6������Ӧ�ó����Ƕ��̵߳�,������Ӧ�ó����ijһ����
��ֻ��һ���߳�ִ��,�Ͳ���Ҫ�κβ����IJ��֡���ǰ,Java�л�û��
�ر�synchronization�ķ�����һ���򵥵İ취�ǻ�ȡ���Դ����,��
ȥsynchronized�����,�����һ�����ࡣ����,����BufferedInputS
tream��,����read�����Dz�����,�����б��I/O�����ǽ��������ǵ�
��֮�ϵġ����Լ򵥵ؽ������ΪNewBIS,����,��JavaSoft��JD K
1.1�ṩ��BufferedInputStream.java�п���������Դ����,��NewB
IS.java����ȥsynch ronized�����,���±���NewBIS��

--
�� ��Դ:���ϴ�С�ٺ���Ϣ����վ bbs.nju.edu.cn��[FROM: hsia.nju.edu.cn]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads