io流读取文件的几种方式:

io流读取文件的几种方式:大致可以分为两种一个是按照字节读取文件,多数用于读取文本类文件,一种是按照字节读取文件多数用来读取音视频文件,当然也可以用来度写文本类型的文件

下面就是测试完整代码:测试文件需要自己放置和更改

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author dayu
 * @version 创建时间:2018年1月19日 上午10:13:54
 *          测试io读取文件的几种方式
 */
public class ReadFile
{
    public static void main(String[] args)
    {
        //文件位置
        String filepath = "D:\\d.txt";
        
        /** 一次读取所有内容 */
        FileInputStreamReadFile(filepath);
        System.out.println("=====================");
        
        /** 以行为单位读取文件,常用于读面向行的格式化文件 */
        BufferedReaderReadFile(filepath);
        System.out.println("=====================");
        
        /** 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。 */
        ReadFileByByte(filepath);
        System.out.println("\n=====================");
        
        /** 以行为单位读取文件,常用于读面向行的格式化文件 */
        InputSteamReaderReadFile(filepath);
        System.out.println("\n=====================");
        
    }
    
    private static void InputSteamReaderReadFile(String filepath)
    {
        try
        {
            InputStreamReader sr = new InputStreamReader(new FileInputStream(new File(filepath)));
            int temp = 0;
            while ((temp = sr.read()) != -1)
            {
                System.out.print((char)temp);
            }
            sr.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private static void ReadFileByByte(String filepath)
    {
        try
        {
            File file = new File(filepath);
            FileInputStream fis = new FileInputStream(file);
            
            int b = 0;
            while ((b = fis.read()) != -1)
            {
                System.out.print((char)b);
            }
            fis.close();
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private static void BufferedReaderReadFile(String filepath)
    {
        try
        {
            StringBuffer sb = new StringBuffer();
            BufferedReader br = new BufferedReader(new FileReader(new File(filepath)));
            String readLine = "";
            while ((readLine = br.readLine()) != null)
            {
                sb.append(readLine + "\n");
            }
            br.close();
            System.out.print(sb.toString());
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
    }
    
    private static void FileInputStreamReadFile(String filepath)
    {
        try
        {
            File file = new File(filepath);
            FileInputStream fis = new FileInputStream(file);
            
            long filelength = file.length();
            byte[] bb = new byte[(int)filelength];
            
            fis.read(bb);
            fis.close();
            System.out.println(new String(bb));
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

 

posted @ 2018-01-19 10:54  大宇007  阅读(12426)  评论(0编辑  收藏  举报