您的位置:首页 > 编程语言 > Java开发

JAVA流(No.1)

2011-09-01 23:42 176 查看
又一新学期的开始,今天是第一天上课有点不在状态,努力调整好自己的状态好好听课。

今天郭老师讲了java流与文件操作,主要内容为以下几点:

Java的输入/输出类库简介

Java的标准输入/输出流

字节流

文件操作类

字符流的

对象序列化

首先理解的是流的概念,输入/输出流,缓冲流,缓冲流就相当于含在嘴里还没有下咽的水,它有一个缓冲的过程。

¯ 1、字节输入流类InputStream:InputStream类是抽象类,不能直接生成对象,它是所有字节输入流类的父类。该类提供了输入处理的基本方法,它的子类一般都重写这些方法。注意的是,该类中的大多数方法都可能抛出IOException异常,因此调用它们时,应该在try…catch块中,捕获和处理IOException异常。读取数据的方法

int read() throws IOException ;

int read(byte[] b) throws IOException ;

int read(byte[] b,int off,int len) throws IOException ;

注意:read方法若返回-1,则表明当前读取位置已经到达流的末尾。

例如:int n;

While((n=fis.read())!=-1)

{

//数据处理

}

¯ 用法:关闭输入流

public void close() throws IOException;

¯ 获取流中可读的字节数

public int available() throws IOException;

¯ 移动读取指针

public long skip(long n) throws IOException;

¯ 标记流中的位置和重置读取位置

µ public boolean markSupported();

µ public void mark(int readlimit); public void reset();

2、字节输出流 OutputStream

OutputStream类是抽象类,不能直接生成对象,它是所有字节输出流类的父类。该类提供了输出处理的基本方法,它的子类一般都重写这些方法。

注意:该类中的大多数方法都可能抛出IOException异常,因此调用它们时,应放在try…catch块中,捕获和处理IOException异常。

¯ 用法:输出数据的方法

void write(int b) throws IOException ;

void write(byte[] b) throws IOException ;

void write(byte[] b,int off,int len) throws IOException ;

¯ 关闭输出流

public void close() throws IOException;

¯ 清空缓冲区

public void flush() throws IOException;

3、文件字节输入流类FileInputStream

FileInputStream用于顺序访问本地文件。它从父类InputStream中继承read()、close()等方法对本机上的文件进行操作,但不支持mark()方法和reset()方法。

¯ 构造方法

l public FileInputStream(string name) throws FileNotFoundException

l public FileInputStream(File file) throws FileNotFoundException

¯ 读取字节的方法

l public int read() throws IOException

l public int read(byte[] b)throws IOException

l public int read(byte[] b,int off,int len) throws IOException

4、文件字节输出流FileOutputStream类

FileOutputStream类用于向一个文件写数据。它从超类OutputStream中继承write()、close()等方法。

¯ 构造方法

l public FileOutputStream(String name) throws FileNotFoundException

l public FileOutputStream(File file) throws FileNotFoundException

l public FileOutput.Stream (String name,boolean append) throws FileNotFoundException

¯ 写入字节的方法

l public void write(int b) throws IOException

l public void write(byte[] b) throws IOException

l public void write(byte[] b,int off,int len) throws IOException

今天的实训项目:把一串数字进行格式化输出,在做这个项目的过程中不仅把知识掌握了一下而且还得知了一个小小的知识点就是回车的代码:“\r\n”;另外一点就是与今天刚刚见面的陈老师相处的很愉快,他今天的讲话让我又有了更强的自信,我相信我也可以学得很好,我对我的未来的学习生活充满了期待。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: