Java IO(2)

参考:http://www.cnblogs.com/zhaoyanjun/p/6292384.html

FileInputStream类的使用:读取文件内容

package com.app;

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 ;
    }
}

FileOutputStream 类的使用:将内容写入文件

package com.app;
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();
                }
            }
        }
    }
}

注意:
在实际的项目中,所有的IO操作都应该放到子线程中操作,避免堵住主线程。
FileInputStream在读取文件内容的时候,我们传入文件的路径("D:/abc.txt"), 如果这个路径下的文件不存在,那么在执行readFile()方法时会报FileNotFoundException异常。
FileOutputStream在写入文件的时候,我们传入文件的路径("D:/abc.txt"), 如果这个路径下的文件不存在,那么在执行writeFile()方法时, 会默认给我们创建一个新的文件。还有重要的一点,不会报异常。

综合练习,实现复制文件,从D盘复制到E盘

package com.app;
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();
                }   
            }
        }
    }
}

你可能感兴趣的:(Java IO(2))