zoukankan      html  css  js  c++  java
  • 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("
    =====================");
            
            /** 以行为单位读取文件,常用于读面向行的格式化文件 */
            InputSteamReaderReadFile(filepath);
            System.out.println("
    =====================");
            
        }
        
        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 + "
    ");
                }
                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();
            }
        }
    }
  • 相关阅读:
    Css3 常见鼠标滑过效果集合
    HTML5 Media事件
    HTML 5 Audio/Video DOM buffered 属性
    Cocos2d-x 3.X 事件分发机制
    在 WPF 程序中使用 MVVM 模式
    Windows Phone 版 Cocos2d-x 程序的结构
    转载:Cocos2D-x 游戏接入 Windows 设备所需做的六件事
    使用 Cocos2d-x 3.1.1 创建 Windows Phone 8 游戏开发环境
    转载:Windows Phone 8.1 投影我的屏幕使用教程
    NHibernate 中使用 nvarchar(max) 类型
  • 原文地址:https://www.cnblogs.com/dayu007/p/8315821.html
Copyright © 2011-2022 走看看