IO总结

两个对应指:1.字节流(Byte Stream)和字符流(Char Stream)的对应;2.输入和输出的对应。一个桥梁指:从字节流到字符流的桥梁。对应于输入和输出为InputStreamReader和OutputStreamWriter。
在这里插入图片描述
输入字节流 InputStream

InputStream 是所有的输入字节流的父类,它是一个抽象类。
ByteArrayInputStream、StringBufferInputStream、FileInputStream 是三种基本的介质流,它们分别从Byte 数组、StringBuffer、和本地文件中读取数据。
输出字节流 OutputStream
OutputStream 是所有的输出字节流的父类,它是一个抽象类。
ByteArrayOutputStream、FileOutputStream 是两种基本的介质流,它们分别向Byte 数组、和本地文件中写入数据。
常用的处理流

缓冲流:BufferedInputStrean 、BufferedOutputStream、 BufferedReader、 BufferedWriter 增加缓冲功能,避免频繁读写硬盘。
转换流:InputStreamReader 、OutputStreamReader实现字节流和字符流之间的转换。
数据流: DataInputStream 、DataOutputStream 等-提供将基础数据类型写入到文件中,或者读取出来。

常用实战

第一 读取数据

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class A1 {

public static void main(String[] args) {
    A1 a1 = new A1();

    //电脑d盘中的abc.txt 文档
    String filePath = "D:/abc.txt" ;
    String reslut = a1.readFile( filePath ) ;
    System.out.println( reslut ); 
}


/**
 * 读取指定文件的内容
 * @param filePath : 文件的路径
 * @return  返回的结果
 */
public String readFile( String filePath ){
    FileInputStream fis=null;
    String result = "" ;
    try {
        // 根据path路径实例化一个输入流的对象
        fis  = new FileInputStream( filePath );

        //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
        int size =  fis.available() ;
        //3. 根据输入流中的字节数创建byte数组;
        byte[] array = new byte[size];
        //4.把数据读取到数组中;
        fis.read( array ) ; 

        //5.根据获取到的Byte数组新建一个字符串,然后输出;
        result = new String(array); 

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }finally{
        if ( fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    return result ;
}


}

第二 写入数据

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class A2 {

public static void main(String[] args) {
    A2 a2 = new A2();

    //电脑d盘中的abc.txt 文档
    String filePath = "D:/abc.txt" ;

    //要写入的内容
    String content = "今天是2017/1/9,天气很好" ;
    a2.writeFile( filePath , content  ) ;

}

/**
 * 根据文件路径创建输出流
 * @param filePath : 文件的路径
 * @param content : 需要写入的内容
 */
public void writeFile( String filePath , String content ){
    FileOutputStream fos = null ;
    try {
        //1、根据文件路径创建输出流
        fos  = new FileOutputStream( filePath );

        //2、把string转换为byte数组;
        byte[] array = content.getBytes() ;
        //3、把byte数组输出;
        fos.write( array );

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }finally{
        if ( fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


}

实现文件的复制

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class A3 {

public static void main(String[] args) {
    A3 a2 = new A3();

    //电脑d盘中的cat.png 图片的路径
    String filePath1 = "D:/cat.png" ;

    //电脑e盘中的cat.png 图片的路径
    String filePath2 = "E:/cat.png" ;

    //复制文件
    a2.copyFile( filePath1 , filePath2 );

}

/**
 * 文件复制 
 * @param filePath_old : 需要复制文件的路径
 * @param filePath_new : 复制文件存放的路径
 */
public void copyFile( String filePath_old  , String filePath_new){
    FileInputStream fis=null ;
    FileOutputStream fout = null ;
    try {
        // 根据path路径实例化一个输入流的对象
        fis  = new FileInputStream( filePath_old );

        //2. 返回这个输入流中可以被读的剩下的bytes字节的估计值;
        int size =  fis.available() ;
        //3. 根据输入流中的字节数创建byte数组;
        byte[] array = new byte[size];
        //4.把数据读取到数组中;
        fis.read( array ) ; 

        //5、根据文件路径创建输出流
        fout = new FileOutputStream( filePath_new ) ;
        
        //5、把byte数组输出;
        fout.write( array );

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch (IOException e) {
        e.printStackTrace();
    }finally{
        if ( fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if ( fout != null ) {
            try {
                fout.close();
            } catch (IOException e) {
                e.printStackTrace();
            }   
        }
    }
}
}

读取某个目录下所有文件、文件夹

public static ArrayList<String> getFiles(String path) {
    ArrayList<String> files = new ArrayList<String>();
    File file = new File(path);
    File[] tempList = file.listFiles();

    for (int i = 0; i < tempList.length; i++) {
        if (tempList[i].isFile()) {
//              System.out.println("文     件:" + tempList[i]);
            files.add(tempList[i].toString());
        }
        if (tempList[i].isDirectory()) {
//              System.out.println("文件夹:" + tempList[i]);
        }
    }
    return files;
}

递归读取全部文件

import java.io.File;
import java.util.ArrayList;
import java.util.List;



public class ReadFile {
	private static void test(String fileDir) {
		List<File> fileList = new ArrayList<File>();
		File file = new File(fileDir);
		File[] files = file.listFiles();// 获取目录下的所有文件或文件夹
		if (files == null) {// 如果目录为空,直接退出
			return;
		}
		// 遍历,目录下的所有文件
		for (File f : files) {
			if (f.isFile()) {
				fileList.add(f);
			} else if (f.isDirectory()) {
				System.out.println(f.getAbsolutePath());
				test(f.getAbsolutePath());
			}
		}
		for (File f1 : fileList) {
			System.out.println(f1.getName());
		}
	}
 
	public static void main(String[] args) {
		test("F:/apache-tomcat-7.0.57-windows-x64");
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值