JAVA IO-2 InputStream和OutputStream

来源:互联网 发布:日本男生发型 知乎 编辑:程序博客网 时间:2024/05/18 02:06
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;/********************************************************** * 这篇代码是讲解InputStream和OutputStream也就是字节输入输出流, Reader和Writer字符输入输出流的简单应用 *  * windows ES平台 *******************************************************/// 这里先讲前两种public class IO_Demo1 {* 先把一个容易混淆的点写出来 输入和输出相对应的就是读取和写入,但是这里的读取并不是人读取,而是电脑啊手机啊这些设备读取,* 也就是IN,就像c/c++里面的stdin,所以所谓的输入,一般目的地都是内存,而写入的目的地, 可以是显示器、文件等等,千万不要搞混了。* ************************************************************* inputStream的主要方法 int read(); * int read(byte b[]); * int read(byte b[], * int read(byte[] b, int off, int len); * void close(); long skip(long numBytes);* ************************************** * OutputStream的主要方法 * void write(int n); * void write(byte b[]); * void write(byte b[], int off, int len); * void close(); * ************************************** * Reader的主要方法* int read(); * int read(char b[]); * int read(char b[], int off, int len); * void close(); * long skip(long numBytes); * Writer的主要方法 void* write(int n); * void write(char b[]); * void write(char b[], int off, int len); * void close(); * 基本方法讲完了,我们就开始讲第二点********************** 上面的这4个类,基本上可以算是平时我们所用的输入输出流的基类或者叫做父类,* 我查阅了一些API,像inputStream的构造方法,是一个无参的构造方法,* 也就是说不会对读取的文件类型,文件路径进行定义,只定义了一些基本方法,* 也可以说符合了java的泛型编程的理念。* 接下来就是代码时间了,我会用以上4个类的子类,去完成代码。*/public static void main(String[] args) {File fileIs = new File("D:\\Test\\aaa.txt");// File的作用就是指定路径,aaa.txt是我事先创建好的空文件byte b[] = new byte[5];// 这个是用来存放从文件读取的内容int count = 0;try {// 这就是一个inputStream的子类,FileInputStream构造时就需要传入一个File作为参数FileInputStream fis = new FileInputStream(fileIs);// 开始读,注意!!读到尾部的条件,read的返回值是-1而不是0while ((count = fis.read(b)) != -1) {System.out.println(count);/* * 转换byte成String,用的是String的构造函数 public String(byte bytes[], int * offset, int length) *  * @param bytes The bytes to be decoded into characters *  * @param offset The index of the first byte to decode *  * @param length The number of bytes to decode */System.out.println(new String(b, 0, count));}fis.close();} catch (IOException e) {e.printStackTrace();}// 好了,到这里第一个输入流就讲解完成,高级语言的好处就是会减少好多,自行解析字符串的时间,// 虽然我脑子里还有是好多C的习惯,还是总结一下// 1、创建File,用来设置文件路径// 2、将File传给输入流// 3、循环读取,-1为EOF也就是文件末尾// 4、关闭流// -------------------------------------------------------------// 好了,接下来就开始输出流,我在aaa.txt的目录下建了一个bbb.txt然后把aaa.txt的内容写进去File fileOs = new File("D:\\Test\\bbb.txt");// 我这里再复习下File的一些方法if (fileOs.exists()) {try {FileOutputStream fos = new FileOutputStream(fileOs);fos.write(b);//我在这里打了断点测试,FileOutputStream没有缓存会立即写入文件,当然也有可能是我太菜了。。。。} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();} } else {System.out.println("Cannot find File");}}}
//结果我都进行了验证,没有问题!!//接下来就是第二Part 字节流,其实真的差不多//还是看代码,我直接用注释解释import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class IO_Demo2 {public static void main(String[] args) {//因为与字符流和字节流真的差不多//注释就没有这么详细,基本都是简单例子大家都看得懂。//使用的文件还是那两个文件aaa.txt bbb.txtFile filein = new File("D:\\Test\\aaa.txt");File fileout = new File("D:\\Test\\bbb.txt");char[] buf = new char[5];int count = 0;try {FileReader fr = new FileReader(filein);//FileReader就是字符流FileWriter fw = new FileWriter(fileout);while((count = fr.read(buf)) != -1)//读取{fw.write(buf, 0, count);//写入}//这里就要注意了,虽然说程序结束,流会自动关闭,但是FileWriter和FileReader是有缓存的,//在close的过程中,他会刷新缓存。并且我并没有在这里找到flush这个函数,也有可能是没有。。不确定。。呵呵呵fr.close();fw.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}

//最后,就是缓冲流BufferedReader和BufferedWriter//他的用法也很简单,我先把构造方法写下来,大家一看就明白了//BufferedReader(Reader in);//BufferedWriter(Writer out);//然后还是看代码比较容易理解
import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;public class IO_Demo3 {//我这次测试,一行一行的读。这种方便的读写,就是BufferedReader的优势public static void main(String[] args) {File filein = new File("D:\\Test\\aaa.txt");File fileout = new File("D:\\Test\\bbb.txt");String tmpStr = null;try {FileReader fr = new FileReader(filein);FileWriter fw = new FileWriter(fileout);//注意这是第一层BufferedReader bfr = new BufferedReader(fr);BufferedWriter bfw = new BufferedWriter(fw);//这是第二层//其实buffered系列的构造函数,参数要求传入的是底层的Reader和Writer//这里再次点一下泛型的概念while((tmpStr = bfr.readLine()) != null)//我每次就读一行{bfw.write(tmpStr);bfw.newLine();//换行bfw.flush();//强制刷新}bfr.close();bfw.close();fr.close();fw.close();//从外层到内层一层一层关,养成好习惯} catch (FileNotFoundException e) {// TODO 自动生成的 catch 块e.printStackTrace();} catch (IOException e) {// TODO 自动生成的 catch 块e.printStackTrace();}}}

到这里我就把基本的IOStream写完了,方便自己之后查询。如果有大神看到有错误的地方 欢迎拍砖。

                                             
0 0
原创粉丝点击