会计考友 发表于 2012-8-4 12:37:27

Java学习知识点要求(16)

Java学习知识点要求(16)
24、基于字符文件读写(FileReader、FileWriter)、基于字节文件读写(InputStream、FileInputStream、FileOutputStream、DataOutputStream、DataInputStream)、随机文件读写(RandomAccessFile)。
(1)字节输入流类-InputStream字节输入流类的作用就是从外部设备获取字节数据到计算机内存中。
抽象类InputStream中的方法
①int read()从输入流的当前位置读取输入流的下一个字节,返回读入的字节数据;如果已经读取到输入流的末端,返回-1。
②int read(byte b[ ] )将输入流读到字节数组中,返回读入的字节数
③int read(byte b[ ],int off,int len )从输入流中读取len个字节到字节数组b[]中,读到的第一个字节放在off处,返回读入的字节数;如果off+len大于b.length,或者off和len中有一个是负数,那么回抛出IndexOutOfBoundsException。
④long skip(long n )从输入流中跳过n个字节,返回跳过的字节数;执行skip()方法后,接着使用read()方法将会读取第n+1个字节。
⑤abstract void close()关闭输入流,释放资源。对流的读取结束后应显示地调用该方法。
⑥mark()方法:给流的当前位置做一个标记
⑦reset()方法:使流回到上次使用mark()方法后的标记处。
(2)字节输出流类-OutputStream 字节输出流类的作用就是将暂存在计算机内存中的数据输出到外部设备。
抽象类OutputStream的方法
①void write(int b )将整数b的低8位写入输出流
②void write(byte b[ ] )将字节数组写入输出
③void write(byte b[ ],int off,int len )从字节数组b的off处开始向输出流写入len个字节
④abstract void flush()强制将输出流保存在缓冲区中的数据写入目标数据源
⑤abstract void close()先调用flush()方法,然后关闭输出流,释放资源
使用输入输出时,可能会出现异常,通常要使用Java的异常处理机制。
try{
         //与流操作相关的代码
}catch(IOException e){
   //处理异常的代码
}finally{
   xxx.close();
}
(3)对文件进行读写
方式一:使用基于字节的流类进行操作,即使用FileInputStream和FileOutputStream;
方式二:使用基于字符的流类进行操作,即使用FileReader和FileWriter(推荐方式)
①FileInputStream类的对象可以从文件中读取字节。
构造方法:FileInputStream(String filename)throws FileNotFoundException
FileInputStream(File file)throws FileNotFoundException
例:FileInputStream in1=new FileInputStream(”c:\hello.java”);
例:File f=new File(“myfile.dat”);FileInputStream in2=new FileInputStream(f);
FileInputStream类的方法
read():从此输入流中读取一个数据字节。
read(byte[] b):从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。
read(byte[] b, int off, int len):从此输入流中将最多 len 个字节的数据读入一个字节数组中。
close():关闭此文件输入流并释放与此流有关的所有系统资源。
②FileOutputStream类
构造方法:
FileOutputStream(String filename)throws FileNotFoundException
FileOutputStream(File file)throws FileNotFoundException
FileOutputStream(String filename,boolean append)throws FileNotFoundException
常用方法:
write(byte[] b):将 b.length 个字节从指定字节数组写入此文件输出流中。
write(byte[] b, int off, int len):将指定字节数组中从偏移量 off 开始的 len 个字节写入此文件输出流。
write(int b):将指定字节写入此文件输出流。
(3)数据字节输入流DataInputStream类和数据字节输出流DataOutputStream类提供直接读或写基本数据类型数据的方法。
①DataInputStream类以与机器无关的方式从一个输入流中读取Java的基本数据类型的数据,所读数据应是由DataOutputStream对象写入。
DataInputStream类的定义如下:
public class DataInputStream extends FilterInputStream implements DataInput{//构造方法
public DataInputStream(InputStream in)//成员方法
public final int skipBytes(int n) throws IOException
public final boolean readBoolean() throws EOFException,IOException
public final byte readBytes() throws EOFException,IOException
public final char readChar() throws EOFException,IOException
public final int readInt() throws EOFException,IOException
public final double readDouble() throws EOFException,IOException
public final String readUTF() throws EOFException,IOException
}
②DataOutputStream定义形式:
public class DataOutputStream extends FilterOutputStream implements DataOutput
DataOutputStream类提供的构造方法如下:
public DataOutputStream(OutputStream out):参数out为底层输出流,通常与具体的设备相关联。
常用方法:
close()
writeByte()
writeInt()
writeUTF()//采用UTF-8编码的字符串。这使字符串格式独立于平台。
writeFloat()
(4)Reader类的方法
字符输入流类定义的方法:
abstract void close()关闭输入流,如果试图继续读取,将产生一个IOException
int read()从输入流读取一个字符。如果到达文件结尾,则返回-1
int read(char buf[ ])从输入流中将指定个数的字符读入到数组buf中,并返回读取成功的实际字符数目。如果到达文件结尾,则返回-1
abstract int read(char buf[ ],int off,int len)从输入流中将len个字符从buf位置开始读入到数组buf中,并返回读取成功的实际字符数目。如果到达文件结尾,则返回-1
boolean ready()判断流是否做好读的准备
long skip(long n)跳过输入中的n个字符,返回实际跳过的字符数目
FileReader类
在Java API中给出FileReader类的定义如下:
public class FileReader extends InputStreamReader{
public FileReader(String fileName)throws FileNotFoundException
         public FileReader(File file)throws FileNotFoundException
}
(5)Writer类
字符输出流类定义的方法:
abstract void close()关闭输出流,如果试图继续写入,将产生一个IOException
abstract void flush()强制输出流中的字符输出到指定的输出装置
void write(int ch)写入一个字符到流中
void write(char buf[ ])将一个完整的字符数组写入到输出流中
abstract void write(char buf[ ],int off,int len)从数组buf的buf位置开始,写入len个字符到输出流中
void write(string str)写入一个字符串到输出流中
void write(string str,int off,int len)写入一个字符串到输出流中,off为字符串的起始位置,len为字符串的长度,即写入的字符数
FileWriter类
Java API给出的FileWriter类的定义如下:
public class FileWriter extends OutputStreamWriter{//部分构造方法
public FileWriter(File file)throws IOException
public FileWriter(File file,Boolean append)throws IOException
public FileWriter(String fileName)throws IOException
public FileWriter(String filename,Boolean append)throws IOException
(6)RandomAccessFile类
①RandomAccessFile类的两个特点:
对一个文件可以同时进行既读又写的操作。
可以在文件中的指定位置读取数据或写入数据。
②RandomAccessFile类的声明为
public class RandomAccessFile extends Object implements DataInput, DataOutput
③RandomAccessFile类有两个构造方法,可以用来创建RandomAccessFile类对象:RandomAccessFile(File file, String mode)
RandomAccessFile(String name, String mode)
第一个参数是File对象(或字符串),指定了文件名。第二个参数mode指明了访问模式。
④要想实现对文件内容进行随机的读/写,文件指针是重要的,Java中提供了如下的方法对文件指针进行操作:
public long getFilePointer( )//获得当前的文件指针。
public void seek( long pos )//移动文件指针到指定的位置。
public int skipBytes( int n )//使文件指针向前移动指定的n个字节。并返回指针实际移动的字节数。
⑤读写数据的方法:
public int read()//读取一个数据字节,以整数形式返回此字节。
public long length()//返回此文件的长度。
public final String readUTF()//从文件中读取一个字符串。
public final String readLine()//从此文件读取文本的下一行。
public final boolean readBoolean()//从此文件读取一个 boolean。
public final void writeUTF(String str)//使用 modified UTF-8 编码以与机器无关的方式将一个字符串写入该文件。
public void write(int b)//从当前文件指针开始写入指定的字节
public final void writeChar(int v)//按双字节值从文件指针的当前位置开始将 char 写入文件。
public final void writeChars(String s)//按字符序列将一个字符串写入该文件。
public final void writeBoolean(Boolean v)//按单字节值将 boolean 写入该文件。
页: [1]
查看完整版本: Java学习知识点要求(16)