Java IO流
来源:互联网 发布:管家婆软件客户端 编辑:程序博客网 时间:2024/06/06 02:13
IO流 ##IO流主要用来读写文件,而且很难理解.不过早实际的项目操作中也就简单的运用就能满足我们的需求.
先看看IO流的一些类
字节流
字节流主要来对二进制进行操作的,它是以字节的来处理的.(简单的说使用记事本无法打开的,打开或者乱码的)
OutputStreram
:输出流,也就是写入文件中.
简单使用:
向文件中写入”hello World”
//字节输出流.写数据 File file = new File("f:\\demo\\io.txt"); //创建字节流输出对象FileOutputStream fos = new FileOutputStream(file); String content = "hello world"; //写数据 fos.write(content.getBytes()); //关闭资源 fos.close();------------------------------------------------------------------------
inputStream
输入流(从文件中读取数据)
从文件中读取数据:
一次读取一个字符## 标题 ##
//从文件中读取文件中的内容 File file = new File("f:\\demo\\io.txt"); //创建字节流输入对象 FileInputStream fis = new FileInputStream(file); int by = 0;## 标题 ## while ((by = fis.read()) != -1) { System.out.print((char)by); }## 标题 ## //关闭资源 fis.close();
一次读取字符数组
//一次性读取整个数组 byte[] bys = new byte[1024]; int len = 0; while((len = fis.read(bys)) != -1){ System.out.println(new String(bys,0,len)); } //关闭资源 fis.close();
字符缓冲流
定义数组读取的速度相对比读取一个字节的速度快的多,可知存在缓冲区的话速率会更快,这就是字符缓冲流.
写入数据
//字符缓冲输出流,写入文件 File file = new File("f:\\demo\\bos.txt"); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); // 写数据 bos.write("hello".getBytes()); // 释放资源 bos.close();
从文件中读取数据
//从文件中读取文件内容 File file = new File("f:\\demo\\bos.txt"); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); byte[] bys = new byte[1024]; int len = 0; while ((len = bis.read(bys)) != -1) { System.out.print(new String(bys, 0, len)); } // 释放资源 bis.close();
字符流的对比
这里我们做一个实验,判断字节流读取的速率
将IO流.bmp文件进行复制
结果如下:
private static void testCopyFile() throws IOException { /** * 四中复制方式 * <ul> * <li>基本字节流一次读写一个字节:共耗时:8890毫秒</li> * <li>基本字节流一次读写一个字节数组:共耗时:17毫秒</li> * <li>高效字节流一次读写一个字节:共耗时:95毫秒</li> * <li>高效字节流一次读写一个字节数组:共耗时:5毫秒</li> * </ul> */ long start = System.currentTimeMillis(); // method1("f:\\demo\\IO流.bmp", "IO流1.bmp"); // method2("f:\\demo\\IO流.bmp", "IO流2.bmp"); // method3("f:\\demo\\IO流.bmp", "IO流3.bmp"); method4("f:\\demo\\IO流.bmp", "IO流4.bmp"); 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(); }
字符流
字符流 = 字节流 + 编码表
字节输出流(写入文件中)
OutputStreamWriter方法
方法:
OutputStreamWriter(OutputStream out):根据默认编码把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
- public void write(int c);一次写一个字符
- public void write(char[] chuf):写一个字符数组
- public void write(char[] chuf,int off,int len):写一个字符数组的一部分
- public void write(String str):写一个字符串
public void write(String str,int off,int len) :写一个数组的一部分
close()与flush()的区别:
close()关闭流对象关闭后无法继续写入内容,flush()刷新流,还可以写入文件中.
简单看下文件的读写
OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("F://demo//io.txt"),"UTF-8");osw.write("今天是周五");osw.write("今天是周六");osw.flush();osw.close();
InputStreamReader
方法:
- int read():一次读取一个字符;
- int read(char[] chs):一次读取一个字符数组;
InputStreamReader isr = new InputStreamReader(new FileInputStream("F://demo//io.txt"),"utf-8");//一次读取一个字符int ch = 0;while ((ch = isr.read()) != -1) { System.out.print((char)ch);}//关闭流释放资源isr.close();//一次读取一个字符数组char[] chs = new char[1024]; int len = 0; while((len = isr.read(chs))!= -1){ System.out.print(new String(chs,0,len)); }
拷贝文件
代码
/** * 边读编写 */ InputStreamReader isr = new InputStreamReader(new FileInputStream("f://demo/io.txt")); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("f://demo//ioC.txt")); //一次读取字符数组 int len = 0; char[] ch = new char[1024]; while((len = isr.read(ch)) != -1) { String content = new String(ch,0,len); osw.write(content); osw.flush(); } isr.close(); osw.close();
一般拷贝文件都是使用本地默认的编码,可以不指定编码.
下面我们尝试用子类来进行拷贝文件
直接上代码,代码如下
public static void main(String[] args) throws IOException { FileReader fr = new FileReader("f://demo//io.txt"); FileWriter fw = new FileWriter("f://demo//ioC2.txt"); int len = 0; char[] ch = new char[1024]; while((len = fr.read(ch)) != -1) { String content = new String(ch,0,len); fw.write(content); fw.flush(); } fr.close(); fw.close();}
缓冲流
字符流为了增加读写效率增加了缓冲流.
BufferedWriter:字符缓冲输出流
BufferedReader:字符缓冲输入流
BufferedWrite:字节缓冲输出流,将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的 高效写入。
BufferedReader:字符缓冲输入流,从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
备注:缓冲区大小可以指定,一般情况下默认大小就能满足我们的需求
两者的使用如下:
BufferedReader br = new BufferedReader(new FileReader("f://demo//io.txt")); BufferedWriter bw = new BufferedWriter(new FileWriter("f://demo//ioC4.txt")); //一次读取一个字符数组 char[] chs = new char[1024]; int len = 0; while ((len = br.read(chs)) != -1) { bw.write(chs, 0, len); bw.flush(); } //一次读取一行 String con = null; while ((con = br.readLine()) != null) { bw.write(con); bw.flush(); } bw.close(); br.close();
下面有一些示例可以参考下:
拷贝文本文件
/** * 拷贝文件 * 采用字符流 * Created by wsylp on 2017/7/1. */public class CopyFileDemo { public static void main(String[] args) throws IOException { String src = "f://demo//io.txt"; String target1 = "f://demo//ioCopy1.txt"; String target2 = "f://demo//ioCopy2.txt"; String target3 = "f://demo//ioCopy3.txt"; String target4 = "f://demo//ioCopy4.txt"; String target5 = "f://demo//ioCopy5.txt"; //一次读写一个字符 copyFile1(src, target1); //一次读写一个字符数组 copyFile2(src, target2); //字符缓冲流读取一个字符 copyFile3(src, target3); //字符缓冲流读取一个字符数组 copyFile4(src, target4); //字符缓冲流读取一行字符串 copyFile5(src, target5); } /** * 字符缓冲流读取一行字符串 * * @param src 源文件 * @param target 目标文件 */ private static void copyFile5(String src, String target) throws IOException { BufferedReader br = new BufferedReader(new FileReader(src)); BufferedWriter bw = new BufferedWriter(new FileWriter(target)); String line = null; while ((line = br.readLine()) != null) { bw.write(line); bw.flush(); } bw.close(); br.close(); } /** * 字符缓冲流读取一个字符数组 * * @param src 源文件 * @param target 目标文件 */ private static void copyFile4(String src, String target) throws IOException { BufferedReader br = new BufferedReader(new FileReader(src)); BufferedWriter bw = new BufferedWriter(new FileWriter(target)); char[] ch = new char[1024]; int len = 0; while ((len = br.read(ch)) != -1) { bw.write(ch, 0, len); bw.flush(); } bw.close(); br.close(); } /** * 字符缓冲流读取一个字符 * * @param src 源文件 * @param target 目标文件 */ private static void copyFile3(String src, String target) throws IOException { BufferedReader br = new BufferedReader(new FileReader(src)); BufferedWriter bw = new BufferedWriter(new FileWriter(target)); int ch = 0; while ((ch = br.read()) != -1) { bw.write(ch); bw.flush(); } bw.close(); br.close(); } /** * 一次读写一个字符数组 * * @param src 源文件 * @param target 目标文件 */ private static void copyFile2(String src, String target) throws IOException { FileReader fr = new FileReader(src); FileWriter fw = new FileWriter(target); char[] ch = new char[1024]; int len = 0; while ((len = fr.read(ch)) != -1) { fw.write(ch, 0, len); fw.flush(); } fw.close(); fr.close(); } /** * 一次读写一个字符 * * @param src 源文件 * @param target 目标文件 */ private static void copyFile1(String src, String target) throws IOException { FileReader fr = new FileReader(src); FileWriter fw = new FileWriter(target); int len = 0; while ((len = fr.read()) != -1) { fw.write(len); fw.flush(); } fw.close(); fr.close(); }}
拷贝照片
/** * 复制照片文件需要字节流 * * Created by wsylp on 2017/7/1. */public class CopyImgDemo { public static void main(String[] args) throws IOException { String src = "f://demo//io.bmp"; String target1 = "f://demo//io1.bmp"; String target2 = "f://demo//io2.bmp"; String target3 = "f://demo//io3.bmp"; String target4 = "f://demo//io4.bmp"; copyImg1(src,target1); copyImg2(src,target2); copyImg3(src,target3); copyImg4(src,target4); } /** * 缓冲流,一次读取一个字节数组 * @param src 源地址 * @param target 目标地址 */ private static void copyImg4(String src, String target) throws IOException { BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); byte[] by = new byte[1024]; int len = 0; while((len = bis.read(by)) != -1){ bos.write(by,0,len); bos.flush(); } bis.close(); bos.close(); } /** * 缓冲流,一次读取一个字节 * @param src 源地址 * @param target 目标地址 */ private static void copyImg3(String src, String target) throws IOException{ BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src)); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target)); int ch = 0; while((ch = bis.read()) != -1) { bos.write(ch); bos.flush(); } bos.close(); bis.close(); } /** * 一次读取一个字节数组 * @param src 源地址 * @param target 目标地址 */ private static void copyImg2(String src, String target) throws IOException { FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(target); byte[] by = new byte[1024]; int len = 0; while((len = fis.read(by))!= -1) { fos.write(by,0,len); } fos.close(); fis.close(); } /** * 缓冲流,一次读取一个字节 * @param src 源地址 * @param target 目标地址 */ private static void copyImg1(String src, String target) throws IOException{ FileInputStream fis = new FileInputStream(src); FileOutputStream fos = new FileOutputStream(target); int len = 0; while((len = fis.read())!= -1) { fos.write(len); } fos.close(); fis.close(); }}
- JAVA【IO一】IO流
- Java-IO-java的IO流
- java IO 流
- JAVA之IO流
- java IO(字符流)
- java的io流
- JAVA之IO流(
- JAVA 基础IO流
- JAVA 中的IO流
- Java IO 流学习
- JAVA 中的IO流
- Java IO流操作方法。。
- java io流总结
- 转]JAVA IO流
- JAVA IO流
- Java中的IO流
- JAVA IO 流操作
- 详述JAVA IO流
- js:利用for循环,输出1-10所有数的平方和(即1-10的阶乘)
- 《数据结构学习与实验指导》4-2:树种统计
- 二叉树的路径和
- Android使用httpclient访问http
- 树的遍历
- Java IO流
- Mybatis---原生态JDBC编程的问题
- Java+Selenium3方法篇25-isDisplayed方法
- jvm类加载
- Docker exec 命令
- 概率DP学习笔记(一)
- 牛顿迭代法
- 我的学习之路_第十八章_SQL语句
- [LeetCode] 606. Construct String from Binary Tree