◇ 从流中读取数据: int read( ); //读取一个字节,返回值为所读的字节 int read( byte b[ ] ); //读取多个字节,放置到字节数组b中,通常 //读取的字节数量为b的长度,返回值为实际 //读取的字节的数量 int read( byte b[ ], int off, int len ); //读取len个字节,放置 //到以下标off开始字节 //数组b中,返回值为实 //际读取的字节的数量 int available( ); //返回值为流中尚未读取的字节的数量 long skip( long n ); //读指针跳过n个字节不读,返回值为实际 //跳过的字节数量
public File(String path);/*如果path是实际存在的路径,则该File对象 /*表示的是目录;如果path是文件名,则该File对象表示的是文件。*/ public File(String path,String name);//path是路径名,name是文件名 public File(File dir,String name);//dir是路径名,name是文件名
FileInputStream fis; try{ fis = new FileInputStream( "text" ); System.out.print( "content of text is : "); int b; while( (b=fis.read())!=-1 ) //顺序读取文件text里的内容并赋值 给整型变量b,直到文件结束为止。 { System.out.print( (char)b ); } }catch( FileNotFoundException e ){ System.out.println( e ); }catch( IOException e ){ System.out.println( e ); }
4.5.3 随机访问文件
对于InputStream 和OutputStream 来说,它们的实例都是顺序访问流,也就是说,只能对文件进行顺序地读/写。随机访问文件则允许对文件内容进行随机读/写。在java中,类RandomAccessFile 提供了随机访问文件的方法。类RandomAccessFile的声明为: public class RandomAccessFile extends Object implements DataInput, DataOutput
◇ 读取字符 public int read() throws IOException; //读取一个字符,返回值为读取的字符 public int read(char cbuf[]) throws IOException; /*读取一系列字符到数组cbuf[]中,返回值为实际读取的字符的数量*/ public abstract int read(char cbuf[],int off,int len) throws IOException; /*读取len个字符,从数组cbuf[]的下标off处开始存放,返回值为实际读取的字符数量,该方法必须由子类实现*/
◇ 标记流 public boolean markSupported(); //判断当前流是否支持做标记 public void mark(int readAheadLimit) throws IOException; //给当前流作标记,最多支持readAheadLimit个字符的回溯。 public void reset() throws IOException; //将当前流重置到做标记处
◇ 关闭流 public abstract void close() throws IOException;
2. Writer类是处理所有字符流输出类的父类。
◇ 向输出流写入字符 public void write(int c) throws IOException; //将整型值c的低16位写入输出流 public void write(char cbuf[]) throws IOException; //将字符数组cbuf[]写入输出流 public abstract void write(char cbuf[],int off,int len) throws IOException; //将字符数组cbuf[]中的从索引为off的位置处开始的len个字符写入输出流 public void write(String str) throws IOException; //将字符串str中的字符写入输出流 public void write(String str,int off,int len) throws IOException; //将字符串str 中从索引off开始处的len个字符写入输出流
◇ flush( ) 刷空输出流,并输出所有被缓存的字节。
◇ 关闭流 public abstract void close() throws IOException;
◇ 生成流对象 public InputStreamReader(InputStream in); /*in是字节流,而InputStreamReader是字符流,但是其来源是字节流in, 因此InputStreamReader就可以把字节流in转换成字符流处理。/*
public InputStreamReader(InputStream in,String enc) throws UnsupportedEncodingException; /*enc是编码方式,就是从字节流到字符流进行转换时所采用的编码方式, 例如 ISO8859-1,UTF-8,UTF-16等等*/
public OutputStreamWriter(OutputStream out); /*out是字节流,而OutputStreamReader是字符流 */
public OutputStreamWriter(OutputStream out,String enc) throws UnsupportedEncodingException; //enc是编码方式
InputStreamReader和OutputStreamWriter的方法:
◇ 读入和写出字符 基本同Reader和Writer。
◇ 获取当前编码方式 public String getEncoding();
◇ 关闭流 public void close() throws IOException;
4.7.3 BufferedReader和BufferedWriter
◇ 生成流对象
public BufferedReader(Reader in); //使用缺省的缓冲区大小 public BufferedReader(Reader in, int sz); //sz为缓冲区的大小 public BufferedWriter(Writer out); public BufferedWriter(Writer out, int sz);
◇ 读入/写出字符
除了Reader和Writer中提供的基本的读写方法外,增加对整行字符的处理。 public String readLine() throws IOException; //读一行字符 public void newLine() throws IOException; //写一行字符 【例4-4】
import java.io.*; public class NumberInput{ public static void main(String args[]){ try{ InputStreamReader ir; BufferedReader in; ir=new InputStreamReader(System.in); //从键盘接收了一个字符串的输入,并创建了一个字符输入流的对象 in=new BufferedReader(ir); String s=in.readLine(); //从输入流in中读入一行,并将读取的值赋值给字符串变量s System.out.println("Input value is: "+s); int i = Integer.parseInt(s);//转换成int型 i*=2; System.out.println("Input value changed after doubled: "+i); }catch(IOException e) {System.out.println(e);} } }
运行结果
D:\>java NumberInput 123 Input value is 123 Input value changed after doubled: 246