工具类篇——I/O读取文件

通过I/O流读取、输出的方式将本地文件复制到指定路径下。

1、以字节为单位读取本地文件,常用于读取二进制文件,如:图片、音频等,代码如下:

package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class ReadByByte {

    public static void main(String[] args) throws Exception {
        
        readByByte();
    }
    //读取路径 d:/testimage.jpg 图片并复制到路径 d:/write.jpg 
    public static void readByByte() throws Exception {
        
        File readImageFile = new File("d:/testimage.jpg");
        
        File writeImageFile = new File("d:/write.jpg");
        
        FileInputStream fis = new FileInputStream(readImageFile);
        
        FileOutputStream fos = new FileOutputStream(writeImageFile);
        
        byte[] readOneByte = new byte[1024*3];
        
        while(fis.read(readOneByte) != -1) {
        
            fos.write(readOneByte);
        }
        
        fos.close();
        
        fis.close();
    }
}

2、以字符为单位读取本地文件,常用于读取文本文档,代码如下:

package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class ReadByChar {

    public static void main(String[] args) throws Exception {
        
        readByChar();
    }
    
    public static void readByChar() throws Exception {
        
        File readFile = new File("d:/testtxt.txt");
        
        File writeFile = new File("d:/write.txt");
        
        FileInputStream fis = new FileInputStream(readFile);
        
        FileOutputStream fos = new FileOutputStream(writeFile);
        
        InputStreamReader isr = new InputStreamReader(fis);
        
        int readChar = isr.read();
        
        while(readChar != -1) {
            
            fos.write(readChar);
            
            readChar = isr.read();
        }
        
        fos.close();
        
        fis.close();
    }
}

3、以字符行为单位读取本地文件,常用于读取文本文档,代码如下:

package util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;

public class ReadByStr {

    public static void main(String[] args) throws Exception {
        
        readByStr();
    }
    
    public static void readByStr() throws Exception {
        

        File readFile = new File("d:/testtxt.txt");
        
        File writeFile = new File("d:/write.txt");
        
        FileInputStream fis = new FileInputStream(readFile);
        
        FileOutputStream fos = new FileOutputStream(writeFile);
        
        InputStreamReader isr = new InputStreamReader(fis);
        
        BufferedReader br = new BufferedReader(isr);
        
        String readStr = br.readLine();
        
        while(readStr != null) {
            
            fos.write(readStr.getBytes());
            
            readStr = br.readLine();
        }
        
        fos.close();
        
        fis.close();
    }
}

 

posted @ 2018-11-26 15:09  不浪小生  阅读(359)  评论(0编辑  收藏  举报