Java API —— IO流( FileInputStream FileOutputStream BufferedInputStream BufferedOutputStream )...
生活随笔
收集整理的这篇文章主要介绍了
Java API —— IO流( FileInputStream FileOutputStream BufferedInputStream BufferedOutputStream )...
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
1、IO流概述 · IO流用来处理设备之间的数据传输 · 上传文件和下载文件 · Java对数据的操作是通过流的方式 · Java用于操作流的对象都在IO包中 2、IO流分类 1)按照数据流向 输入流 读入数据 输出流 写出数据 2) 按照数据类型 字节流: 字节输入流 读取数据 InputStream 字节输出流 写出数据 OutputStream 字符流: 字符输入流 读取数据 Reader 字符输出流 写出数据 Writer 3)什么情况下使用哪种流呢? 如果数据所在的文件通过windows自带的记事本打开并能读懂里面的内容,就用字符流。其他用字节流。 如果你什么都不知道,就用字节流 3、IO流常用基类 1) 字节流的抽象基类: InputStream ,OutputStream。 2) 字符流的抽象基类: Reader , Writer。 注:由这四个类派生出来的子类名称都是以其父类名作为子类名的后缀。 如:InputStream的子类FileInputStream。 如:Reader的子类FileReader。 4、字节流写数据 1)OutputStream -> FileOutputStream 分析发现其实更适合用字符流,但是由于字节流先出现,所以,我们考虑先使用字节流后面再讲会什么出现字符流。 2) FileOutputStream的构造方法 FileOutputStream(File file) FileOutputStream(String name) 3)字节流写数据的方式 · public void write(int b) · public void write(byte[] b) · public void write(byte[] b,int off,int len) 例子1: package iodemos;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/*** Created by gao on 15-12-27.*/
/*
* 需求:我要往一个文本文件中输入一句话:"hello,io"
*
* 分析:
* A:这个操作最好是采用字符流来做,但是呢,字符流是在字节流之后才出现的,所以,今天我先讲解字节流如何操作。
* B:由于我是要往文件中写一句话,所以我们要采用字节输出流。
*
* 通过上面的分析后我们知道要使用:OutputStream
* 但是通过查看API,我们发现该流对象是一个抽象类,不能实例化。
* 所以,我们要找一个具体的子类。
* 而我们要找的子类是什么名字的呢?这个时候,很简单,我们回想一下,我们是不是要往文件中写东西。
* 文件是哪个单词:File
* 然后用的是字节输出流,联起来就是:FileOutputStream
* 注意:每种基类的子类都是以父类名作为后缀名。
* XxxOutputStream
* XxxInputStream
* XxxReader
* XxxWriter
* 查看FileOutputStream的构造方法:
* FileOutputStream(File file)
* FileOutputStream(String name)
*
* 字节输出流操作步骤:
* A:创建字节输出流对象
* B:写数据
* C:释放资源
*/
public class IODemo01 {public static void main(String[] args) throws IOException {// 创建字节输出流对象// FileOutputStream(File file)
// File file = new File("fos.txt");
// FileOutputStream fos = new FileOutputStream(file);// FileOutputStream(String name)FileOutputStream fos = new FileOutputStream("fos.txt");/** 创建字节输出流对象了做了几件事情:* A:调用系统功能去创建文件* B:创建fos对象* C:把fos对象指向这个文件*///写数据fos.write("hello,IO".getBytes());fos.write("java".getBytes());//释放资源//关闭此文件输出流并释放与此流有关的所有系统资源。
fos.close();/** 为什么一定要close()呢?* A:让流对象变成垃圾,这样就可以被垃圾回收器回收了* B:通知系统去释放跟该文件相关的资源*///java.io.IOException: Stream Closed//fos.write("java".getBytes());
}
}
例子2:
package iodemos; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 字节输出流操作步骤:* A:创建字节输出流对象* B:调用write()方法* C:释放资源** public void write(int b):写一个字节* public void write(byte[] b):写一个字节数组* public void write(byte[] b,int off,int len):写一个字节数组的一部分*/ public class IODemo02 {public static void main(String[] args) throws IOException {// 创建字节输出流对象// OutputStream os = new FileOutputStream("fos2.txt"); // 多态FileOutputStream fos = new FileOutputStream("fos2.txt");// 调用write()方法//fos.write(97); //97 -- 底层二进制数据 -- 通过记事本打开 -- 找97对应的字符值 -- a// fos.write(57);// fos.write(55);//public void write(byte[] b):写一个字节数组byte[] bys = {97,98,99,100,101};fos.write(bys);//public void write(byte[] b,int off,int len):写一个字节数组的一部分fos.write(bys,1,3);//释放资源 fos.close();} }输出到文件fos2.txt:
abcdebcd 例子3: package iodemos; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 如何实现数据的换行?* 为什么现在没有换行呢?因为你值写了字节数据,并没有写入换行符号。* 如何实现呢?写入换行符号即可。* 有些文本文件打开是可以的,通过windows自带的那个不行,为什么呢?* 因为不同的系统针对不同的换行符号识别是不一样的?* windows:\r\n* linux:\n* Mac:\r* 而一些常见的个高级记事本,是可以识别任意换行符号的。** 如何实现数据的追加写入?* 用构造方法带第二个参数是true的情况即可*/ public class IODemo03 {public static void main(String[] args) throws IOException {// 创建字节输出流对象//FileOutputStream fos = new FileOutputStream("fos3.txt");// 创建一个向具有指定 name 的文件中写入数据的输出文件流。// 如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。FileOutputStream fos = new FileOutputStream("fos3.txt",true);// 写数据for (int x = 0; x < 10; x++) {fos.write(("hello"+x).getBytes());fos.write("\r\n".getBytes());}// 释放资源 fos.close();} }4)加入异常处理的IO流操作 package iodemos; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 加入异常处理的字节输出流操作*/ public class IODemo04 {public static void main(String[] args) {// 分开做异常处理 // FileOutputStream fos = null; // try { // fos = new FileOutputStream("fos04.txt"); // } catch (FileNotFoundException e) { // e.printStackTrace(); // } // try { // fos.write("java".getBytes()); // } catch (IOException e) { // e.printStackTrace(); // } // try { // fos.close(); // } catch (IOException e) { // e.printStackTrace(); // }// 一起做异常处理 // try { // FileOutputStream fos = new FileOutputStream("fos04.txt"); // fos.write("java".getBytes()); // fos.close(); // }catch (FileNotFoundException e){ // e.printStackTrace(); // }catch (IOException e){ // e.printStackTrace(); // }// 改进版// 为了在finally里面能够看到该对象就必须定义到外面,为了访问不出问题,还必须给初始化值FileOutputStream fos = null;try{fos = new FileOutputStream("fos04.txt");fos.write("java".getBytes());}catch (FileNotFoundException e){e.printStackTrace();}catch (IOException e){e.printStackTrace();}finally {// 如果fos不是null,才需要close()if(fos != null){// 为了保证close()一定会执行,就放到这里了try {fos.close();} catch (IOException e) {e.printStackTrace();}}}} }
5)字节流读取数据 · InputStream FileInputStream · FileInputStream的构造方法 FileInputStream(File file) FileInputStream(String name) · FileInputStream的成员方法 public int read() public int read(byte[] b) 例子1: package inputstream; import java.io.FileInputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 字节输入流操作步骤:* A:创建字节输入流对象* B:调用read()方法读取数据,并把数据显示在控制台* C:释放资源** 读取数据的方式:* A:int read():一次读取一个字节* B:int read(byte[] b):一次读取一个字节数组*/ public class FileInputStreamDemo01 {public static void main(String[] args) throws IOException {// FileInputStream(String name)FileInputStream fis = new FileInputStream("fis.txt");// 调用read()方法读取数据,并把数据显示在控制台// 用循环改进 // int by = fis.read(); // while (by != -1) { // System.out.print((char) by); // by = fis.read(); // }// 最终版代码int b = 0;// 读取,赋值,判断while ((b = fis.read()) != -1) {System.out.print((char) b);}//释放资源 fis.close();} }
例子2:计算机如何识别两个字节拼接成一个中文
package iodemos; import java.util.Arrays; /** 计算机是如何识别什么时候该把两个字节转换为一个中文呢?* 在计算机中中文的存储分两个字节:* 第一个字节肯定是负数。* 第二个字节常见的是负数,可能有正数。但是没影响。*/ public class IODemo06 {public static void main(String[] args) {//String s = "abcde";//[97, 98, 99, 100, 101]String s = "我爱你中国";//[-50, -46, -80, -82, -60, -29, -42, -48, -71, -6]byte[] bys = s.getBytes();System.out.println(Arrays.toString(bys));} }例子3:一次读取一个字节数组
read(byte[] b) 和 new String(b,0,len)一起用 package iodemos; import java.io.FileInputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ public class IODemo10 {public static void main(String[] args) throws IOException {// 创建字节输入流对象FileInputStream fis = new FileInputStream("fis2.txt");// 数组的长度一般是1024或者1024的整数倍byte[] bys = new byte[1024];int len = 0;while ((len = fis.read(bys)) != -1) {System.out.print(new String(bys, 0, len)); //千万要带上len的使用 }// 释放资源 fis.close();} }6)字节流复制数据练习 · 把当前项目目录下的a.txt内容复制到当前项目目录下的b.txt中 package iodemos; import inputstream.FileInputStreamDemo01; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 复制文本文件。** 数据源:从哪里来* a.txt -- 读取数据 -- FileInputStream** 目的地:到哪里去* b.txt -- 写数据 -- FileOutputStream** java.io.FileNotFoundException: a.txt (系统找不到指定的文件。)** 这一次复制中文没有出现任何问题,为什么呢?* 上一次我们出现问题的原因在于我们每次获取到一个字节数据,就把该字节数据转换为了字符数据,然后输出到控制台。* 而这一次呢?确实通过IO流读取数据,写到文本文件,你读取一个字节,我就写入一个字节,你没有做任何的转换。* 它会自己做转换。*/ public class IODemo05 {public static void main(String[] args) throws IOException {// 封装数据源FileInputStream fis = new FileInputStream("a.txt");// 封装目的地FileOutputStream fos = new FileOutputStream("b.txt");int by = 0;while ((by = fis.read()) != -1) {fos.write(by);}// 释放资源(先关谁都行) fis.close();fos.close();} }
· 把c:\a\a.txt内容复制到c:\b\b.txt中
方式一: package iodemos; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 需求:把c盘下的a.txt的内容复制到c盘下的b.txt中** 数据源:* c:\a\a.txt -- 读取数据-- FileInputStream* 目的地:* c:\b\b.txt -- 写出数据 -- FileOutputStream*/ public class IODemo07 {public static void main(String[] args) throws IOException {// 封装数据源FileInputStream fis = new FileInputStream("c:\\a\\a.txt");// 封装目的地FileOutputStream fos = new FileOutputStream("c:\\b\\b.txt");// 复制数据int by = 0;while ((by = fis.read()) != -1){fos.write(by);}// 释放资源 fos.close();fis.close();} }方式二:
package iodemos; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ public class IODemo11 {public static void main(String[] args) throws IOException {// 封装数据源FileInputStream fis = new FileInputStream("c:\\a\\a.txt");// 封装目的地FileOutputStream fos = new FileOutputStream("c:\\b\\b.txt");// 复制数据byte[] bys = new byte[1024];int len = 0;while ((len = fis.read(bys)) != -1) {fos.write(bys, 0, len);}// 释放资源 fos.close();fis.close();} }· 把c:\aa.jpg内容复制到当前项目目录下的mn.jpg中 package iodemos; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 需求:把c:\\aa.jpg内容复制到当前项目目录下的mn.jpg中** 数据源:* c:\\aa.jpg --读取数据--FileInputStream* 目的地:* mn.jpg--写出数据--FileOutputStream*/ public class IODemo08 {public static void main(String[] args) throws IOException {// 封装数据源FileInputStream fis = new FileInputStream("c:\\aa.jpg");// 封装目的地FileOutputStream fos = new FileOutputStream("mn.jpg");// 复制数据int by = 0;while ((by = fis.read()) != -1){fos.write(by);}// 释放资源 fos.close();fis.close();} } · 把c:\abc.mp4复制到当前项目目录下的copy.mp4中(效率低) 方式一: package iodemos; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 需求:把c:\\abc.mp4复制到当前项目目录下的copy.mp4中** 数据源:* c:\\abc.mp4--读取数据--FileInputStream* 目的地:* copy.mp4--写出数据--FileOutputStream*/ public class IODemo09 {public static void main(String[] args) throws IOException {// 封装数据源FileInputStream fis = new FileInputStream("c:\\abc.mp4");// 封装目的地FileOutputStream fos = new FileOutputStream("copy.mp4");// 复制数据int by = 0;while ((by = fis.read()) != -1) {fos.write(by);}// 释放资源 fos.close();fis.close();} }
方式二:
package iodemos; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ public class IODemo12 {public static void main(String[] args) throws IOException {// 封装数据源FileInputStream fis = new FileInputStream("c:\\abc.mp4");// 封装目的地FileOutputStream fos = new FileOutputStream("copy.mp4");// 复制数据byte[] bys = new byte[1024];int len = 0;while ((len = fis.read(bys)) != -1) {fos.write(bys, 0, len);}// 释放资源 fos.close();fis.close();} }
5、字节缓冲流
1)字节流一次读写一个数组的速度明显比一次读写一个字节的速度快很多,这是加入了数组这样的缓冲区效果,java本身在设计的时候,也考虑到了这样的设计思想(装饰设计模式后面讲解),所以提供了字节缓冲区流 2)字节缓冲输出流:BufferedOutputStream 3)字节缓冲输入流:BufferedInputStream 例子1: package iodemos; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 通过定义数组的方式确实比以前一次读取一个字节的方式快很多,所以,看来有一个缓冲区还是非常好的。* 既然是这样的话,那么,java开始在设计的时候,它也考虑到了这个问题,就专门提供了带缓冲区的字节类。* 这种类被称为:缓冲区类(高效类)* 写数据:BufferedOutputStream* 读数据:BufferedInputStream** 构造方法可以指定缓冲区的大小,但是我们一般用不上,因为默认缓冲区大小就足够了。** 为什么不传递一个具体的文件或者文件路径,而是传递一个OutputStream对象呢?* 原因很简单,字节缓冲区流仅仅提供缓冲区,为高效而设计的。但是呢,真正的读写操作还得靠基本的流对象实现。*/ public class IODemo13 {public static void main(String[] args) throws IOException {// BufferedOutputStream(OutputStream out) // FileOutputStream fos = new FileOutputStream("bos.txt"); // BufferedOutputStream bos = new BufferedOutputStream(fos);// 简单写法BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt"));// 写数据bos.write("hello".getBytes());// 释放资源 bos.close();} }例子2:
package iodemos; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; /*** Created by gao on 15-12-27.*/ /** 注意:虽然我们有两种方式可以读取,但是,请注意,这两种方式针对同一个对象在一个代码中只能使用一个。*/ public class IODemo14 {public static void main(String[] args) throws IOException {// BufferedInputStream(InputStream in)BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt"));// 读取数据 // int by = 0; // while ((by = bis.read()) != -1) { // System.out.print((char) by); // }int len = 0;byte[] bys = new byte[1024];while ((len = bis.read(bys)) != -1) {System.out.print(new String(bys, 0, len));}// 释放资源 bis.close();} }例子3:四种方式复制mp4
package cn.itcast_06; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** 需求:把e:\\哥有老婆.mp4复制到当前项目目录下的copy.mp4中* * 字节流四种方式复制文件:* 基本字节流一次读写一个字节: 共耗时:117235毫秒* 基本字节流一次读写一个字节数组: 共耗时:156毫秒* 高效字节流一次读写一个字节: 共耗时:1141毫秒* 高效字节流一次读写一个字节数组: 共耗时:47毫秒*/ public class CopyMp4Demo {public static void main(String[] args) throws IOException {long start = System.currentTimeMillis();// method1("e:\\哥有老婆.mp4", "copy1.mp4");// method2("e:\\哥有老婆.mp4", "copy2.mp4");// method3("e:\\哥有老婆.mp4", "copy3.mp4");method4("e:\\哥有老婆.mp4", "copy4.mp4");long end = System.currentTimeMillis();System.out.println("共耗时:" + (end - start) + "毫秒");}// 高效字节流一次读写一个字节数组:public static void method4(String srcString, String destString)throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));byte[] bys = new byte[1024];int len = 0;while ((len = bis.read(bys)) != -1) {bos.write(bys, 0, len);}bos.close();bis.close();}// 高效字节流一次读写一个字节:public static void method3(String srcString, String destString)throws IOException {BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcString));BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(destString));int by = 0;while ((by = bis.read()) != -1) {bos.write(by);}bos.close();bis.close();}// 基本字节流一次读写一个字节数组public static void method2(String srcString, String destString)throws IOException {FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString);byte[] bys = new byte[1024];int len = 0;while ((len = fis.read(bys)) != -1) {fos.write(bys, 0, len);}fos.close();fis.close();}// 基本字节流一次读写一个字节public static void method1(String srcString, String destString)throws IOException {FileInputStream fis = new FileInputStream(srcString);FileOutputStream fos = new FileOutputStream(destString);int by = 0;while ((by = fis.read()) != -1) {fos.write(by);}fos.close();fis.close();} }
转载于:https://www.cnblogs.com/yangyquin/p/5080549.html
总结
以上是生活随笔为你收集整理的Java API —— IO流( FileInputStream FileOutputStream BufferedInputStream BufferedOutputStream )...的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: uniDBGrid导入数据库(转红鱼儿)
- 下一篇: AOS V0.8 发布,JavaEE 应