字节流
基本输入输出方法
System类是Java语言中一个功能强大、非常有用的类,它提供了标准输入/输出及运行时的系统信息
- System类不能创建对象,也就是说,System类的所有属性和方法都是静态的,引用时要以System 作为前缀
- System.in与System.out是System类的两个静态属性,分别对应了系统的标准输入/输出流
System类管理标准输入输出流和错误流
- System.out:把输出送到缺省的显示(通常是显示器),是PrintStream的对象
- System.in:从标准输入获取输入(通常是键盘),是InputStream的对象
- System.err:把错误信息送到缺省的显示,是PrintStream的对象
同时使用System.out和System.err输出信息不能保证显示顺序就是执行顺序,为了避免这种情况在测试 代码执行中经常使用System.err输出
练习题
题目:用户在键盘依次输入若干个数字,每输入一个数字都需要按回车键确认,最后在键盘输入一个非 数字字符串结束整个输入操作过程。程序将计算出这些数的和以及平均值。
用来读写外设字符的,它们都是字节流。如果编程人员愿意,可以用基于字符的流来包装它们
System.out.println("提示信息:");
try {
//从键盘上读取一个字节的数据并返回读取的int类型内容0-255,如果输入中文则会出现乱码,因为一个中文是2个字节构成
//如果用户不输入任何内容,则当前程序会在这里阻塞等待,直到用户输入为止---BIO
int kk=System.in.read();//这个方法上有个IOException异常,属于受检型异常,必须进行处理,处理方法有throws或者try/catch
System.out.println((char)kk);//将读取内容转换为char类型进行显示
} catch (Exception e) {
e.printStackTrace();
}
InputStream基本输入类
InputStream类是基本的输入类。它定义了所有输入流所需的方法
-
public abstract int read() throws IOException读取一个字节并以整数的形式返回,0-255。如果返回-1已到输入流的末尾。
-
public int read(byte b[]) throws IOException读取一系列字节并存储到一个数组,返回实际读取的字节数。如果已经读到输入流的末尾则返回-1
-
public void close() throws IOException 关闭输入流并释放资源
-
public int read(byte b[],int offset,int length) throws IOException功能为从输入流中读数据。这一方法有几种重载形式,可以读一个字节或一组字节。当遇到文件尾时,返回-1。最后一种形式中的offset是指把结果放在b[]中从第offset个字节开始的空间,length为长度
-
public long skip (long n) throws IOEnception 从输入流跳过几个字节。返回值为实际跳过的字节数
OutputStream基本输出类
三个重载形式都是用来向输出流写数据的
- public abstract void write(int b)向输入流写入一个字节数据,该字节为参数的低8位。
- public void write(byte b[],int offset,int length)将一个字节类型的数组中的从指定位置offset开始 的length个字节写入到输出流
- public void close( ) 关闭输出流,释放资源
- public void write(byte b[])将一个字节类型的数组中的数据写入到输出流
- public void flush() 清除缓冲区,将缓冲区内尚未写出的数据全部输出
练习题
需求:使用字节流将一个文件进行拷贝
每次读写一个字节效率太低,所以使用字节数组。同时编码中需要考虑如果文件存在则进行拷贝操作, 否则提示文件不存在
public static void main(String[] args) throws Exception {
try (
InputStream is = new FileInputStream("E:\\java\\eclipse-workspace\\0801\\src\\demo1\\test2.java");
OutputStream os = new FileOutputStream("E:\\java\\eclipse-workspace\\0801\\src\\demo1\\out3.txt");
){
byte[] buffer = new byte[8192];
int len = 0;
while ((len = is.read(buffer)) > 0)
os.write(buffer, 0, len);
}
}
字符流
在顶层有Reader和Writer两个抽象类。Reader和Writer中定义了read()和write()方法,它们被派生流类重载
Reader抽象类的定义
public abstract class Reader implements Readable, Closeable {
//BIO,读取一个有效的字符,返回值为0到65535的整数,如果到达流的末尾则返回-1
public int read() throws IOException
//BIO,读取字符存储到char数组中,返回读取的字符个数,流结束则返回-1
public int read(char cbuf[]) throws IOException
//关闭流,同时释放资源
abstract public void close() throws IOException;
Writer抽象类的定义
public abstract class Writer implements Appendable, Closeable, Flushable {
//写出一个字符到字符流,要写的字符包含在给定整数值的16个低位;16个高位被忽略。
public void write(int c) throws IOException
//将字符数组中的指定部分内容压入到字符流,从off开始共len个字符
abstract public void write(char cbuf[], int off, int len) throws
IOException;
/关闭流,同时释放资源
abstract public void close() throws IOException;
字符流Reader
- int read()读取一个字符并以整数的形式返回0-65535,如果返回-1则已到输入流末尾
- int read(char[] cbuf)读取一系列字符并存储到一个数组中,返回实际读取的字符数,如果读到输入 流末尾则返回-1
- void close()关闭输入流并释放内存资源
- int read(char[] cbuf, int off, int len) 读取len个字符并存储到一个数组中,从off位置开始,返回实 际读取的字符数,如果读取到输入流末尾则返回-1
- long skip(long n)跳过n个字符不读,返回实际跳过的字节数
字符流Writer
- void write(int c) 将字符(int数组的低8位)压入到字符流中
- void write(char[] cbuf, int off, int len)将字符数组中的指定部分内容压入到字符流中,从off开始共len个字符
- void write(String str) 将字符串中的内容压入到字符流中
- void close() 关闭流并释放所占用的资源
- void write(String str, int off, int len) 将字符串中的指定部分内容压入到字符流中,从下标off开始共len个字符
- void flush()刷新缓冲区,将缓冲区中的数据全部送出到目标地,然后清空缓冲区
- void write(char[] cbuf) 将字符数组中的所有数据压入到字符流中
一般来说:一次读写一个字符效率太低,可以引入char[]数组提高执行效率
练习题
练习:编写一个程序实现如下功能,从当前目录下的文件fin.txt中读取80个字节(实际读到的字节数可 能比80少)并将读来的字节写入当前目录下的文件fout.txt
public static void main(String[] args) throws Exception {
try (
InputStream in = new FileInputStream("E:\\java\\eclipse-workspace\\0802\\src\\com\\demo7.java");
OutputStream out = new FileOutputStream("E:\\java\\eclipse-workspace\\0802\\src\\com\\font.txt")) {
for (int i = 0; i < 80; i++) {
int k = in.read();
if (k == -1)
break;
out.write(k);
}
}
}
小结
-
在学些BIO时记忆父类的方法,区分子类的实现不同
- InputStream中的方法 read(byte[]):int; Reader中方法read(char[]):int 如果到达流末尾 则-1
- OutputStream中的方法 write(byte[],0,len):void;Writer中的方法 write(char[],0,len)/write(String)
-
一般在使用中,如果读取数据使用字节流,则写出数据采用的也是字节流;不建议混用,除非引入 桥接流
-
文件流
- FileInputStream(“file-name”) FileInputStream(File) FileNotFoundException
- FileReader(“file-name”) FileReader(File) FileNotFoundException
- FileOutputStream(“file-name”) FileOutputStream(“file-name”,true) 默认文件覆盖,如果 参数true表示追加
- FileWriter(“file-name”) FileWriter(“file-name”,true)
- 一般不使用单字节或者单字符的操作方法,使用数组
-
注意:try(){}是推荐写法,否则应该使用try{}finally{}结构保证流的关闭