目录
程序在运行期间,可能需要从外部的存储媒介或其他程序中读入所需要的数据,这就需要使用输入流。输入流的指向称为它的源,程序通过输入流读取源中的数据。
输入流示意图:
另一方面,程序在处理数据后,可能需要将处理的结果写入到永久的存储媒介中或传送给其他的应用程序,这就需要使用输出流。输出流的指向称为它的目的地,程序通过输出流把数据传送到目的地。虽然I/O流经常与磁盘文件存取有关,但是源和目的地也可以是键盘、内存或显示器窗口。
输出流示意图:
java.io包(I/O 流库)提供大量的流类,所有输入流都是抽象类InputStream (字节输入流)或抽象类Reader (字符输入流)的子类,而所有输出流都是抽象类OutputStream (字节输出流)或抽象类Writer (字符输出流)的子类。
Java所有的I/O机制都是基于数据流进行输入输出,这些数据流表示了字符或者字节数据的流动序列。
通过流来读写文件
流是一组有序的数据序列
以先进先出方式发送信息的通道
Java流的分类
Java程序想要访问文件属性需要使用java.io.File 类
操作:
1.创建File类对象指向需要访问的文件
2.调用File类对象访问文件属性
File类的常用方法
方法名称 | 说明 |
boolean exists( ) | 判断文件或目录是否存在 |
boolean isFile( ) | 判断是否是文件 |
boolean isDirectory( ) | 判断是否是目录 |
String getPath( ) | 返回此对象表示的文件的相对路径名 |
String getAbsolutePath( ) | 返回此对象表示的文件的绝对路径名 |
String getName( ) | 返回此对象表示的文件或目录的名称 |
boolean delete( ) | 删除此对象指定的文件或目录 |
boolean createNewFile( ) | 创建名称的空文件,不创建文件夹 |
long length() | 返回文件的长度,单位为字节, 如果文件不存在,则返回 0L |
- package demo01;
- import java.io.File;
- import java.io.IOException;
-
-
- public class FileDemo01 {
-
- public static void main(String[] args) {
- //获取一个File类对象,这个对象指向计算机F盘中的demo.txt文件
- File file1 = new File("F:\\demo.txt");
- File file2 = new File("F:/test");
-
- //boolean exists():判断File类对象指向的文件或者目录是否存在,如果存在返回true,否则返回false
- System.out.println(file1.exists());
- System.out.println(file1.exists());
-
- //boolean isFile():判断File类对象指向的是不是一个文件,如果是返回true,否则返回false
- System.out.println(file1.isFile());
- System.out.println(file2.isFile());
-
- //boolean isDirectory():判断File类对象指向的是不是一个目录,如果是返回true,否则返回false
- System.out.println(file1.isDirectory());
- System.out.println(file2.isDirectory());
-
- File file3 = new File("F:/a.txt");
- File file4 = new File("F:/a/b/c/d.txt");
- //boolean createNewFile():创建名称的空文件,不创建文件夹,也就是说File类指向的文件,其所在的文件夹应该存在
-
-
-
- // file3.createNewFile();
- //
- // file4.createNewFile();
-
- System.out.println("文件创建成功");
- //String getPath():返回此对象表示的文件的相对路径名
- //String getAbsolutePath():返回此对象表示的文件的绝对路径名
- //String getName():返回此对象表示的文件或目录的名称
-
- File file5 = new File("qqqq.txt");
-
- try {
- file5.createNewFile();
- System.out.println("file5创建成功");
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- System.out.println(file5.getPath());//qqqq.txt
- System.out.println(file5.getAbsolutePath());//E:\MyEclipseWorkspaces02\Day026输入输出流\qqqq.txt
- //boolean delete():删除File类对象指向的文件或目录
- // file1.delete();
-
- file2.delete();
- //long length():返回File类对象指向的文件的长度,单位为字节,如果文件不存在,返回0L
-
- System.out.println(file1.length());//0
-
- File file6 = new File("F:/a");
- File file7 = new File("F:/aa/bb/cc/dd");
- //mkdir():创建此抽象路径名
- file6.mkdir();
- // file7.mkdir();
-
- //mkdirs():创建此抽象路径名指定的目录,包括所有必需但不存在的父目录
- file7.mkdirs();
-
-
- }
-
- }
抽象类,直接子类比较经常使用的是FileInputStream类
此抽象类是表示字节输入流的所有类的超类。
如果对文件读取需求比较简单,那么可以使用FileInputStream 类(文件字节输入流),该类是InputStream类的子类(以字节为单位读取文件),该类的实例方法都是从InputStream类继承来的。
使用FileInputStream 读文本文件
1.导包
2.创建对象指向文件
3.使用方法读取文件
4.关闭流
- package demo01;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.IOException;
-
- public class FileInputStreamDemo01 {
-
- public static void main(String[] args) throws IOException {
- //字节输入流InputStream基类 为抽象类 FileInputStream类是其子类
- // 创建File类对象
- File file = new File("F:/a.txt");
- //创建FileInputStream类对象
- FileInputStream fis = new FileInputStream(file);
-
- //读取数据
- // int num = fis.read();
- // System.out.println((char)num);
- // int num2 = fis.read();
- // System.out.println((char)num2);
- // int num3 = fis.read();
- // System.out.println((char)num3);
- //
- // while(fis.read()!=-1){
- // int num = fis.read();
- // System.out.println((char)num);
- //
- // }
-
- int num;
- while((num=fis.read())!=-1){
- System.out.println((char)num);
- }
-
- System.out.println("数据读取完毕");
-
- //数据读取完毕之后,关闭流
- fis.close();
-
-
-
- }
-
- }
- package demo01;
-
- import java.io.FileInputStream;
- import java.io.IOException;
-
- public class FileInputStreamDemo02 {
-
- public static void main(String[] args) throws IOException {
- // 创建FileInputStream类对象
- FileInputStream fis = new FileInputStream("F:/a.txt");
- //读取数据
- byte[] bytes = new byte[1024];
- //从流中读取数据,将读取的数据存储在你声明的数组中,该方法返回的结果表示从流中读取到的字节数目
- int num = fis.read(bytes);
- System.out.println(num);
-
- //遍历数组,获取读取到的数据
- for (int i = 0; i < num; i++) {
- System.out.print((char)bytes[i]);
-
- }
- fis.close();
- }
-
- }
此抽象类是表示输出字节流的所有类的超类。输出流接受输出字节并将这些字节发送到某个接收器。
如果对文件写入需求比较简单,那么可以使用FileOutputStream类(文件字节输出流),它是OutputStream 类的子类(以字节为单位向文件写入内容),该类的实例方法都是从OutputStream类继承来的。
使用FileOutputStream 写文本文件
1.导包
2.创建对象指向文件
3.使用方法写入文件
4.关闭流
- package demo02;
-
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- public class FileOutputStreamDEmo01 {
-
- public static void main(String[] args) {
- //字节输出流OutputStream基类 是抽象类 其子类是FileOutputSream类
- //创建File类对象
- File file = new File("F:/a.txt");
-
- // 创建FileOutputStream类对象
- FileOutputStream fos =null;
- try {
- //构造方法FileOutputStream(File file)和FileOutputStream(String path):通过这两个构造方法创建的输出流对象在对外数据输出的时候,会覆盖文件中原来的数据
- // FileOutputStream fos = new FileOutputStream(file);
- //FileOutputStream(File file,boolean append)和FileOutputStream(String path,boolean append):使用这两个构造方法创建输出流对象的时候,将第二个参数赋值为true,则在对外进行输出数据的时候,不会覆盖文件中原来的数据
-
- fos =new FileOutputStream(file,true);
- //调用写的方法,将数据写入到文件中
- fos.write(65);
- System.out.println("数据写入完毕");
-
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- //关闭流
- try{
- if(fos!=null){
- fos.close();
- }
- }catch(IOException e){
- e.printStackTrace();
- }
- }
-
- }
-
- }
用于读取字符流的抽象类。子类必须实现的方法只有 read(char[], int, int) 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。
InputStreamReader 是字节流通向字符流的桥梁:它使用指定的
读取字节并将其解码为字符。它使用的字符集可以由名称指定或显式给定,或者可以接受平台默认的字符集。charset
每次调用 InputStreamReader 中的一个 read() 方法都会导致从底层输入流读取一个或多个字节。要启用从字节到字符的有效转换,可以提前从底层流读取更多的字节,使其超过满足当前读取操作所需的字节。
- package demo04;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
-
- public class InputStreamReaderDemo01 {
-
- public static void main(String[] args) {
- // 创建File类对象
- File file = new File("F:/a.txt");
-
- // 创建InputStream类的引用(InputStream是一个抽象类,无法实例化,可以创建其子类对象)
- InputStream is = null;
- // 创建InputStreamReader类对象
- InputStreamReader isr = null;
- try {
- // 向上转型:父类引用指向子类的实例
- is = new FileInputStream(file);
- // 创建InputStreamReader类对象
- isr = new InputStreamReader(is);
-
- //读取数据
- int num;
- while((num=isr.read())!=-1){
- System.out.println((char)num);
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try{
- isr.close();
- is.close();
-
- }catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- }
InputStreamReader类 的直接子类
用来读取字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是适当的。要自己指定这些值,可以先在 FileInputStream 上构造一个 InputStreamReader。
- package demo05;
-
- import java.io.FileNotFoundException;
- import java.io.FileReader;
- import java.io.IOException;
-
- public class FileReaderDemo01 {
-
- public static void main(String[] args) {
- // 创建FileReader类对象,FileReader类对象只能按照本地平台的编码格式读取文件,如果文件编码和本地平台编码格式不一样,会出现乱码的现象
- FileReader fr =null;
- try {
- fr = new FileReader("F:/a.txt");
- char[] chs = new char[1024];
- //读取数据
- int num = fr.read(chs);
- for (int i = 0; i < num; i++) {
- System.out.print(chs[i]);
-
- }
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
-
- }
-
- }
BufferedReader类是Reader类的子类
从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取。
BufferedReader类带有缓冲区 按行读取内容的readLine()方法
BufferedReader常用的构造方法
BufferedReader(Reader in)
子类BufferedReader特有的方法
readLine()
- package demo06;
-
- import java.io.BufferedReader;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.Reader;
-
- public class BufferedReaderDemo01 {
-
- public static void main(String[] args) {
- //创建InputStream类对象,但是InputStream类是一个抽象类,不能创建对象,可以将其引用指向子类
- InputStream is =null;
- //创建Reader类对象,但是Reader类是一个抽象类,不能创建对象,可以将其引用指向子类(包含孙子类)的实例
- Reader reader =null;
- //创建BufferedReader类对象
- BufferedReader br = null;
- try {
- is = new FileInputStream("F:/demo.txt");
- reader = new InputStreamReader(is);
- br = new BufferedReader(reader);
-
- //读取文件
- // String str = br.readLine();
- // System.out.println(str);
- // String str2 = br.readLine();
- // System.out.println(str2);
- String str;
- while((str=br.readLine())!=null){
- System.out.println(str);
- }
-
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try{
- br.close();
- reader.close();
- is.close();
- }catch(IOException e){
- e.printStackTrace();
- }
- }
-
-
- }
-
- }
写入字符流的抽象类。子类必须实现的方法仅有 write(char[], int, int)、flush() 和 close()。但是,多数子类将重写此处定义的一些方法,以提供更高的效率和/或其他功能。
OutputStreamWriter 是字符流通向字节流的桥梁:可使用指定的
将要写入流中的字符编码成字节。它使用的字符集可以由名称指定或显式给定,否则将接受平台默认的字符集。charset
- package demo07;
-
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
-
- public class OutputStreamWriterDemo01 {
-
- public static void main(String[] args) {
- // 创建OutputStream类对象,该类是一个抽象类,不能直接创建对象,可以创建一个引用指向其子类对象
- OutputStream os = null;
- // 创建OutputStreamWriter类对象
- OutputStreamWriter osw = null;
- try {
- os = new FileOutputStream("F:/demo.txt", true);
- osw = new OutputStreamWriter(os);
-
- osw.write("asdfghjkl");
- System.out.println("数据写入完毕");
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } finally {
- try {
- osw.close();
- os.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- }
用来写入字符文件的便捷类。此类的构造方法假定默认字符编码和默认字节缓冲区大小都是可接受的。要自己指定这些值,可以先在 FileOutputStream 上构造一个 OutputStreamWriter。
- package demo08;
-
- import java.io.FileWriter;
- import java.io.IOException;
-
- public class FileWriterDemo01 {
-
- public static void main(String[] args) {
- //创建FileWriter类对象
- FileWriter fw = null;
- try {
- fw = new FileWriter("F:/demo.txt", false);
- fw.write("hello java");
- System.out.println("文件写入完毕");
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- fw.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
- }
-
- }
BufferedWriter类是Writer类的子类
将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。
BufferedWriter类带有缓冲区
BufferedWriter常用的构造方法
BufferedWriter(Writer out)
- package demo08;
-
- import java.io.BufferedWriter;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.io.Writer;
-
- public class BufferedWriterDemo01 {
-
- public static void main(String[] args) {
- //创建Writer类对象,但是Writer类是一个抽象类,不能直接创建对象,但是可以创建引用指向其子类对象
- Writer writer = null;
- //创建BufferedWriter类对象
- BufferedWriter bw =null;
- try {
- writer = new FileWriter("F:/demo.txt", true);
- bw = new BufferedWriter(writer);
- //先向文件中插入一个换行
- bw.newLine();
- bw.write("hello html");
- bw.newLine();
- bw.write("hello css");
- System.out.println("数据写入完毕");
- } catch (IOException e) {
- e.printStackTrace();
- }finally{
- try {
- bw.close();
- writer.close();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- }
-
- }
BufferedReader和BufferedWriter 类创建的对象称为缓冲输入、输出流,二者增强了读写文件的能力。
BufferedReader 流和BufferedWriter流,二者的源和目的地必须是字符输入流和字符输出流。
因此,如果把文件字符输入流作为BufferedReader流的源,把文件字符输出流作为BufferedWriter 流的目的地,那么,BufferedReader 和BufferedWriter 类创建的流将比字符输入流和字符输出流有更强的读写能力,比如,BufferedReader流就可以按行读取文件。