黑马程序员——JAVA笔记——IO操作

来源:互联网 发布:淘宝如何退保证金 编辑:程序博客网 时间:2024/05/18 05:59

------Java培训、Android培训、iOS培训、.Net培训、期待与您交流! -------


Java中IO是以流为基础进行输入输出的,所有数据被串行化写入输出流,或者从输入流读入。


Java流的分类:

按流向分为:

输入流:读取数据的流。

输出流:写入数据的流。

按数据传输单位分:

字节流:以字节为单位传输。

字符流:以字符为单位传输。


下面是IO中类的关系图


从图中可以看出

字符流和字节流分别有读和写的两个基类,分别是:

字节流两个基类:InputStream OutputStream

字符流两个基类:Reader Writer


一、字符流

字符流的两个基类是:Reader和Writer。

当处理文件时,常用到的两个类:FileReader和FileWriter。

为了提高流的效率,也可以使用缓冲区:BufferedReader和BufferedWriter。

1、在硬盘上,创建一个文件并写入一些文字数据。

这里就用到了FileWriter类

import java.io.*;class Demo{public static void main(String[] args) throws IOException{//创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件。//而且该文件会被创建到指定目录下。如果该目录下已有同名文件,则会被覆盖。//其实该步就是在明确数据要存放的目的地。FileWriter fw=new FileWriter("demo.txt");//调用write方法,将字符串写入到流中。fw.write("abcde");//刷新流对象中的缓冲中的数据。//将数据刷到目的地中。fw.flush();//关闭流资源,但是关闭之前会刷新一次内部的缓冲中的数据。//将数据刷到目的地中。//和flush的区别:flush刷新后,流可以继续使用,close刷新后会将留关闭。fw.close();fw.write("haha");}}

2、从硬盘上读取一个文本文件。

这里就用到了FileReader,FileReader中提供了两种读取方式

class Demo{public static void main(String[] args)throws IOException{//创建一个文件读取流对象,和指定名称的文件相关联。//要保证该文件是已经存在的,如果不存在,会发生异常FileNotFoundExceptionFileReader fr=new FileReader("Demo.txt");//第一种方式:逐个字符读取//read1(fr);//第二种方式:通过字符数组进行读取read2(fr);fr.close();}public static void read1(FileReader fr) throws IOException{//调用读取流对象的read方法//read();一次读一个字符。而且会自动往下读。int ch;while((ch=fr.read())!=-1){System.out.print((char)ch);}/*while(true){int ch=fr.read();if(ch==-1)break;System.out.println("ch="+(char)ch);}*/}public static void read2(FileReader fr) throws IOException{//定义一个字符数组。用于存储督导的字符。//该read(char[])返回的是督导的字符个数。char[] buf=new char[1024];int num=0;while((num=fr.read(buf))!=-1){System.out.println("num="+num+"----"+new String(buf,0,num));}}}


3、将c盘一个文本文件复制到D盘。
复制的原理:其实就是将C盘下的文件数据存储到D盘的一个文件中。
步骤:
1.在D盘创建一个文件。用于存储C盘文件中的数据
2.定义一个读取流和C盘文件关联。
3.通过不断的读写完成数据存储。
4.关闭资源。
class Demo{public static void main(String[] args){copy_2();}//从C盘读一个字符,就往D盘写一个字符。public static void copy_1()throws IOException{//创建目的地FileWriter fw=new FileWriter("copy1.txt");//与已有文件关联FileReader fr=new FileReader(".classpath");int ch=0;while((ch=fr.read())!=-1){fw.write(ch);}fw.close();fr.close();}public static void copy_2(){FileWriter fw=null;FileReader fr=null;try{fw=new FileWriter("copy_2.txt");fr=new FileReader(".classpath");char[] buf=new char[1024];int len=0;while((len=fr.read(buf))!=-1){fw.write(buf, 0, len);}}catch(IOException e){throw new RuntimeException("读写失败");}finally{if(fw!=null){try{fw.close();}catch(IOException e){System.out.println(e.toString());}}if(fr!=null){try{fr.close();}catch(IOException e){System.out.println(e.toString());}}}}}


看完了传统的FileReader和FileWriter之后,我们会发现流的效率比较低下,于是我们便加入了缓冲区技术。

缓冲区的出现是为了提高流的操作效率而出现的。所以在创建缓冲区之前,必须要先有流对象。

BufferedWriter中提供了一个跨平台的换行符。newLine();

BufferedReader中提供了一个读取一行字符的方法。readLine();

方便于对文本数据的获取。当返回null时,表示读导文件末尾。

readLine方法返回的时候只返回回车符之前的数据内容。并不返回回车符。所以常与newLine方法配合使用。


4、通过缓冲区复制一个.java文件。

class Demo{public static void main(String[] args){BufferedReader bufr=null;BufferedWriter bufw=null;try{bufr=new BufferedReader(new FileReader(".classpath"));bufw=new BufferedWriter(new FileWriter("copy.txt"));String line=null;while((line=bufr.readLine())!=null){bufw.write(line);bufw.newLine();bufw.flush();}}catch(IOException e){throw new RuntimeException("读鞋失败。");}finally{if(bufr!=null){try{bufr.close();}catch(IOException e){throw new RuntimeException("读取关闭失败。");}}if(bufw!=null){try{bufw.close();}catch(IOException e){throw new RuntimeException("写入关闭失败。");}}}}}


二、字节流

字节流的两个基类为InputStream和OutputStream。

当操作文件时,我们常用到FileInputStream和FileOutputStream。

同样的,为了提高流的速率也可以使用BufferedInputStream和BufferedOutputStream。


1、想要操作图片数据。这时就要用到字节流。

class Demo{public static void main(String[] args) throws IOException{readFile_3();}public static void readFile_3()throws IOException{FileInputStream fis=new FileInputStream("fos.txt");//int num=fis.available();//System.out.println(num);byte[] buf=new byte[fis.available()];//定义一个刚刚好的缓冲区。不用在循环了。int n;fis.read(buf);System.out.println(new String(buf));fis.close();}public static void readFile_2()throws IOException{FileInputStream fis=new FileInputStream("fos.txt");byte[] buf=new byte[1024];int len=0;while((len=fis.read(buf))!=-1){System.out.println(new String(buf,0,len));}fis.close();}public static void readFile_1()throws IOException{FileInputStream fis=new FileInputStream("fos.txt");int ch=0;while((ch=fis.read())!=-1){System.out.println((char)ch);}fis.close();}public static void writeFile() throws IOException{FileOutputStream fos=new FileOutputStream("fos.txt");fos.write("abcde".getBytes());fos.close();}}

2、复制一个图片

思路:

1.用字节读取流对象和图片关联。

2.用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。

3.通过循环读写,完成数据的存储。

4.关闭资源。

class Demo{public static void main(String[] args){FileInputStream fis=null;FileOutputStream fos=null;try{fis=new FileInputStream("123.jpg");fos=new FileOutputStream("copy.jpg");int len=0;byte[] buf=new byte[1024];while((len=fis.read(buf))!=-1){fos.write(buf, 0, len);}}catch(IOException e){throw new RuntimeException("copy失败");}finally{if(fis!=null){try{fis.close();}catch(IOException e){throw new RuntimeException("读取关闭失败");}}if(fos!=null){try{fos.close();}catch(IOException e){throw new RuntimeException("写入关闭失败");}}}}}

3、mp3的复制。通过缓冲区。

BufferedOutputStream

BufferedInputStream

public static void copy()throws IOException{BufferedInputStream bufis=new BufferedInputStream(new FileInputStream("1.mp3"));BufferedOutputStream bufos=new BufferedOutputStream(new FileOutputStream("copy.mp3"));int by=0;while((by=bufis.read())!=-1){bufos.write(by);}bufos.close();bufis.close();}

0 0
原创粉丝点击