黑马程序员_IO流

来源:互联网 发布:五星大饭店知乎 编辑:程序博客网 时间:2024/05/16 10:50

IO流的分类

根据处理数据类型的不同分为:字符流和字节流

根据数据流向不同分为:输入流和输出流

字符流和字节流

字符流的由来: 因为数据编码的不同,而有了对字符进行高效操作的流对象。本质其实就是基于字节流读取时,去查了指定的码表。字节流和字符流的区别:

1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节。

2)处理对象不同:字节流能处理所有类型的数据(如图片、avi等),而字符流只能处理字符类型的数据。

3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的;而字符流在操作的时候下后是会用到缓冲区的,是通过缓冲区来操作文件,我们将在下面验证这一点。

结论:优先选用字节流。首先因为硬盘上的所有文件都是以字节的形式进行传输或者保存的,包括图片等内容。但是字符只是在内存中才会形成的,所以在开发中,字节流使用广泛。

输入流和输出流

对输入流只能进行读操作,对输出流只能进行写操作,程序中需要根据待传输数据的不同特性而使用不同的流。

Java IO流对象

1.输入字节流InputStream

定义和结构说明:

从输入字节流的继承图可以看出:

InputStream 是所有的输入字节流的父类,它是一个抽象类。

字节流读取文件

/**

 * 字节流

 *读文件

 * */

import java.io.*;

class hello{

   public static void main(String[] args) throws IOException {

       String fileName="D:"+File.separator+"hello.txt";

       File f=new File(fileName);

       InputStream in=new FileInputStream(f);

       byte[] b=new byte[1024];

       int count =0;

       int temp=0;

       while((temp=in.read())!=(-1)){

           b[count++]=(byte)temp;

       }

       in.close();

       System.out.println(new String(b));

    }

}

注意:当读到文件末尾的时候会返回-1.正常情况下是不会返回-1的。

 

输出字节流OutputStream

定义和结构说明:

IO 中输出字节流的继承图可见上图,可以看出:

OutputStream 是所有的输出字节流的父类,它是一个抽象类。

/**

 * 字节流

 * 向文件中写入字符串

 * */

import java.io.*;

class hello{

   public static void main(String[] args) throws IOException {

       String fileName="D:"+File.separator+"hello.txt";

       File f=new File(fileName);

       OutputStream out =new FileOutputStream(f);

       String str="Hello World";

       byte[] b=str.getBytes();

       out.write(b);

       out.close();

    }

}

字符输出流Writer

public abstract class Writer

extends Object

implements AppendableCloseableFlushable

写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。

BufferedWriter 是一个装饰器为Writer 提供缓冲功能。

/**

 * 字符流

 * 写入数据

 * */

import java.io.*;

class hello{

   public static void main(String[] args) throws IOException {

       String fileName="D:"+File.separator+"hello.txt";

       File f=new File(fileName);

       Writer out =new FileWriter(f);

       String str="hello";

       out.write(str);

       out.close();

    }

}

 

字符输入流Reader

Reader 是所有的输入字符流的父类,它是一个抽象类。

BufferedReader 很明显就是一个装饰器,它和其子类负责装饰其它Reader 对象。

InputStreamReader 是一个连接字节流和字符流的桥梁,它将字节流转变为字符流

 

/**

 * 字符流

 * 从文件中读出内容

 * */

import java.io.*;

class hello{

   public static void main(String[] args) throws IOException {

       String fileName="D:"+File.separator+"hello.txt";

       File f=new File(fileName);

       char[] ch=new char[100];

       Reader read=new FileReader(f);

       int temp=0;

       int count=0;

       while((temp=read.read())!=(-1)){

           ch[count++]=(char)temp;

       }

       read.close();

       System.out.println("内容为"+new String(ch,0,count));

    }

}

 

字符流与字节流转换

转换流的特点:

1)其是字符流和字节流之间的桥梁

2)可对读取到的字节数据经过指定编码转换成字符

3)可对读取到的字符数据经过指定编码转换成字节

何时使用转换流?

当字节和字符之间有转换动作时;

流操作的数据需要编码或解码时。

具体的对象体现

InputStreamReader:字节到字符的桥梁

OutputStreamWriter:字符到字节的桥梁

这两个流对象是字符体系中的成员,它们有转换作用,本身又是字符流,所以在构造的时候需要传入字节流对象进来。

字节流和字符流转换实例:

/**

 * 将字节输出流转化为字符输出流

 * */

import java.io.*;

class hello{

   public static void main(String[] args) throws IOException {

       String fileName= "d:"+File.separator+"hello.txt";

       File file=new File(fileName);

       Writer out=new OutputStreamWriter(new FileOutputStream(file));

       out.write("hello");

       out.close();

    }

}

/**

 * 将字节输入流变为字符输入流

 * */

import java.io.*;

class hello{

   public static void main(String[] args) throws IOException {

       String fileName= "d:"+File.separator+"hello.txt";

       File file=new File(fileName);

       Reader read=new InputStreamReader(new FileInputStream(file));

       char[] b=new char[100];

       int len=read.read(b);

       System.out.println(new String(b,0,len));

       read.close();

    }

}

 

File

File类是对文件系统中文件以及文件夹进行封装的对象,可以通过对象的思想来操作文件和文件夹。 File类保存文件或目录的各种元数据信息,包括文件名、文件长度、最后修改时间、是否可读、获取当前文件的路径名,判断指定文件是否存在、获得当前目录 中的文件列表,创建、删除文件和目录等方法。 

下列是创建文件夹

import java.io.*;

class hello{

   public static void main(String[] args) {

       File f=new File("D:\\hello.txt");

       try{

           f.createNewFile();

       }catch (Exception e) {

           e.printStackTrace();

       }

    }

}

 

/**

 * 使用listFiles列出指定目录的全部文件

 * listFiles输出的是完整路径

 * */

import java.io.*;

class hello{

   public static void main(String[] args) {

       String fileName="D:"+File.separator;

       File f=new File(fileName);

       File[] str=f.listFiles();

       for (int i = 0; i < str.length; i++) {

           System.out.println(str[i]);

       }

    }}

 

0 0
原创粉丝点击