Article View: cn.bbs.comp.lang.java
Article #24079���Java I/O���ܵ��������� (fwd)
From: ��
Date: Sat, 20 Jun 1998 00:00
Date: Sat, 20 Jun 1998 00:00
181 lines
10350 bytes
10350 bytes
������: pipishi.bbs@bbs.nju.edu.cn (���ܵ�Сʨ), ����: Programming
�� ��: ���Java I/O���ܵ���������
����վ: nju_bbs (Sat Apr 18 23:01:40 1998)
ת��վ: lifebbs!cstshnews!ustcnews!nju_bbs
����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��
--
[1;31m ��Ī��������룬��Ī��������֪ ��[0m
�� ��Դ: �й�ƴ�BBSվ [bbs.ustc.edu.cn]
Technical Details
Message-ID:
<980620.ynGLe$PBBS@bbs.ustc.edu.cn>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: Chinese,GB X-Google-Thread: f6cb0,f6d20c6c285526f1,start X-Google-Attributes: gidf6cb0,public From: Qin_Qin@bbs.ustc.edu.cn (��) Subject: ���Java I/O���ܵ��������� (fwd) Date: 1998/06/20 Message-ID: <980620.ynGLe$PBBS@bbs.ustc.edu.cn> X-Deja-AN: 364424075 Organization: �й�ƴ�BBSվ Newsgroups: cn.bbs.comp.lang.java