输入和输出流,见下表: 输入流
输出流
字节输入流 字符输入流 InputStream Reader 字节输出流 OutputStream 字符输出流 Writer InputStream(字节输入流)
InputStream是字节输入流,InputStream是一个抽象类,所有继承了InputStream的类都是字节输入流,主要了解以下子类即可:
<<字节输入流>>IntputStream<<文件输入流>>FileInputStream<<对象输入流>>ObjectInputStream<<过滤输入流>>FilterInputStream<<缓存输入流>>BufferedInputStream
主要方法介绍: void close() 关闭此输入流并释放与该流关联的所有系统资源。 abstract int read() 从输入流读取下一个数据字节。 int read(byte[] b) 从输入流中读取一定数量的字节并将其存储在缓冲区数组 b 中。 int read(byte[] b, int off, int len) 将输入流中最多 len 个数据字节读入字节数组。 具体代码如下:
InputStream in = null;
//字节输入流InputStream 是抽象类,需要实例化他的子类
FileInputStream《文件字节输入流》注意:使用路径要使用\\\\或者/ try {
in = new FileInputStream(\); int c =0;//定义一个变量来接收每次读取的数据
while((c=in.read())!=-1){//按字节读取文件,直到文件末尾返回-1
System.out.print((char)c);//输出对应的字节,将asic码值转
化成对应的char }
} catch (FileNotFoundException e) { }
e.printStackTrace(); e.printStackTrace(); if(in!=null){ }
try {
in.close(); //关闭流 } catch (IOException e) { }
e.printStackTrace();
} catch (IOException e) { }finally{
Reader(字符输入流)
所有继承了Reader都是字符输如流
<<字符输入流>>Reader<<缓存输入流>><<字节转字符输入流>>BufferedReaderInputStreamReader<<字符文件输入流>>FileReader
主要方法介绍
abstract void close() 关闭该流。 int read() 读取单个字符。 int read(char[] cbuf) 将字符读入数组。 abstract int read(char[] cbuf, int off, int len) 将字符读入数组的某一部分。 示例代码: Reader re =null;
try {
re = new FileReader(\);//字符输入流 int a;
while((a=re.read())!=-1){ }
e.printStackTrace(); e.printStackTrace(); if(re!=null){
try {
System.out.print((char)a);
} catch (FileNotFoundException e) { } catch (IOException e) { }finally{
}
}
}
re.close();
e.printStackTrace();
} catch (IOException e) {
OutputStream(字节输出流)
所有继承了OutputStream都是字节输出流
<<字节输出流>>OutputStream<<文件输出流>><<对象输出流>><<过滤输出流>>FileOutputStreamObjectOutputStreamFilerOutputStream<<缓存流>>BufferedOutputStream<<打印流>>PrintStream
主要方法介绍 void close() 关闭此输出流并释放与此流有关的所有系统资源。 void flush() 刷新此输出流并强制写出所有缓冲的输出字节。 void write(byte[] b) 将 b.length 个字节从指定的字节数组写入此输出流。 void write(byte[] b, int off, int len) 将指定字节数组中从偏移量 off 开始的 len 个字节写入此输出流。 abstract void write(int b) 将指定的字节写入此输出流。 示例代码:
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库java基础总结大全(6)在线全文阅读。
相关推荐: