Java IO学习总结

来源:互联网 发布:淘宝卖的德国护肤品 编辑:程序博客网 时间:2024/05/20 09:43

最近没事,就把以前学过的一些基础东西回顾回顾总结总结。这两天又看了看IO,也大概有了一个知识框架,下图就是IO这块的知识体系。

所谓IO(Input Output):是用来处理设备之间数据的传输,java对数据的操作都是通过流的方式,java中用来操作流的对象都在java.io包中。 java中IO流按照流向分为:输入流和输出流;按照操作的数据类型分为:字节流和字符流。由于数据在计算机中的最终是通过0101这样二进制数据进行保存和运算的,所以字节流可以处理任意数据类型,比如音视频文件、图片、文本等数据;而字符流是专门为了方便操作文本数据而生的,它结合了字符编码,在创建字符流的时候,java会默认使用平台的字符编码,当然我们也可以指定字符编码。




1.字节流

1).字节流抽象基类    InputStream   , OutputStream2).字节流中 FileInputStream  , FileOuputStream基本操作    在操作图片,音视频数据的时候就需要使用到字节流    //使用FileOutputStream往文件中写入数据    public static void fileOutputStreamDemo(){FileOutputStream fos = null;try{//1.创建字节输入流对象,并于文件fos.txt关联,fos = new FileOutputStream("fos.txt");//2.往字节流中输入字节数据fos.write("FileOutputStream Demo".getBytes());  //将字节数据写入到文件中fos.flush();   //该方法是一个空实现;因为不存在缓冲,直接操作的字节数据}catch(IOException e){e.printStackTrace();}finally{//3.关闭资源close(fos);}}//使用FileInputStream从文件中读取数据public static void fileInputStreamDemo(){FileInputStream fis = null;try{fis = new FileInputStream("fos.txt");int len = 0;byte[] buf = new byte[1024];while((len=fis.read(buf)) != -1){System.out.println(new String(buf,0,len));}}catch(IOException e){e.printStackTrace();}finally{close(fis);}}//使用字节流拷贝文件public static void copyFile(){FileInputStream fis = null;FileOutputStream fos = null;try{fis = new FileInputStream("fos.txt");fos = new FileOutputStream("fos_copy.txt");int len = 0;byte[] buf = new byte[1024];while((len=fis.read(buf)) != -1){fos.write(buf,0,len);}}catch(IOException e){e.printStackTrace();}finally{close(fis);close(fos);}}//使用带有缓冲区的BufferedInputStream ,BufferedOutputStream拷贝文件public static void copyFileWithBuffer(){BufferedInputStream bis = null;BufferedOutputStream bos = null;try{bis = new BufferedInputStream(new FileInputStream("fos.txt"));bos = new BufferedOutputStream(new FileOutputStream("fos_copy.txt"));int by = 0;while((len=bis.read(by)) != -1){bos.write(by);}}catch(IOException e){e.printStackTrace();}finally{close(bis);close(bos);}}

2.字符流

1).字符流抽象基类    Reader   ,   Writer 2).字符流中FileReader , FileWriter基本操作  //使用FileWriter创建文件并写入数据 , 注意FileWriter使用了默认编码,即系统本身编码 public static void fileWriterDemo(){FileWriter fw = null;try{fw = new FileWriter("demo.txt");     //在指定目录下创建demo.txt文件,无论有没有都会创建该文件,所以如果文件存在,就会覆盖掉原文件;//fw = new FileWriter("demo.txt" , true);  // 在指定目录下创建文件,true表示不覆盖文件,并在已有文件的末尾进行续写;fw.write("FileWriter Demo");   // 将字符串写入到流中fw.flush();  // 刷新流对象,将流中缓冲的数据写入到文件中}catch(IOException e){e.printStackTrace();}finally{try{// 关闭资源,在关闭之前会调用flush()方法将内部缓存中的数据写入到文件中去// 调用close()方法之后,不能在往流里面继续写入数据;if(fw != null)fw.close();    }catch(IOException e){throw new RuntimeException("关闭资源失败");}}}//使用FileReader读取文件并打印到控制台上public static void fileReaderDemo(){FileReader fr = null;try{// 创建文件读取流对象,和指定名称的文件关联,如果文件不存在,则抛出FileNotFoundExceptionfr = new FileReader("demo.txt"); // 使用read()方法读取数据;如果读到流末尾时候返回-1;int len = 0;char[] buf = new char[1024];while((len=fr.read(buf)) != -1){   // 将读入的数据存入到字符数组中,返回读取数据的长度,当到达文件末尾时返回-1System.out.println(new String(buf , 0 ,len));}}catch(Exception e){e.printStackTrace();}finally{try{if(fr != null)fr.close();}catch(IOException e){throw new RuntimeException("resource closed fail");}}}// 完整的copy文件public static void copyFileDemo(){FileReader fr = null;FileWriter fw = null;try{fr = new FileReader("demo.txt");      // 创建输入流对象,与文件demo.txt关联fw = new FileWriter("demo_copy.txt"); // 创建输出流对象,int len = 0;char[] buf = new char[1024];while((len=fr.read(buf)) != -1){    // 读取数据到字符数组缓冲区中,返回值为读取字符的长度,当返回-1时,到达文件末尾fw.write(buf, 0, len);       // 将字符数组中的数据写入到输出流中fw.flush();                // 通过flush()将流中的数据写入到文件中;}}catch(IOException e){throw new RuntimeException("copy failed");}finally{close(fr);close(fw);}}3).字符流缓冲区来提高读写效率(BufferedWriter ,BufferedReader 的使用)//BufferedReader 读取数据public static void bufferReaderDemo(){FileReader fr = null;BufferedReader br = null;try{//1.创建一个字符读取流对象fr = new FileReader("buf.txt");//2.创建BufferedReader对象br = new BufferedReader(fr);//3.读取数据String line = null;while((line=br.readLine()) != null){  //当读取到文件末尾时,readLine方法返回nullSystem.out.println(line);}}catch(IOException e){e.printStackTrace();}finally{close(fr);close(br);}}//BufferedWriter 写入数据public static void bufferWriterDemo(){FileWriter fw = null;BufferedWriter bw = null;try{//1. 创建一个字符写入流对象fw = new FileWriter("buf.txt");//2.为了提高字符写入效率,加入缓冲技术,bw = new BufferedWriter(fw);//3.写入数据bw.write("bufferedWriter Demo....");bw.newLine();  //换行//4.刷新数据bw.flush();}catch(Exception e){e.printStackTrace();}finally{close(fw);}}//使用BufferedReader,BufferedWriter复制文件public static void copyFileByBuffer(){BufferedReader br = null;BufferedWriter bw = null;try{br = new BufferedReader(new FileReader("buf.txt"));bw = new BufferedWriter(new FileWriter("buf_copy.txt"));String line = null;while((line=br.readLine()) != null){   // readLine()方法不包含换行符bw.write(line);bw.newLine();bw.flush();}}catch(IOException e){e.printStackTrace();}finally{close(br);close(bw);
使用的的工具类

/** * 关闭资源 * @param obj */public static void close(Closeable obj){try{if(obj !=null)obj.close();}catch(IOException e){e.printStackTrace();}}  }}
1 0
原创粉丝点击