zoukankan      html  css  js  c++  java
  • 工具类篇——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();
        }
    }
  • 相关阅读:
    底图与蒙版的过渡效果transition
    消除blur属性的边框
    jquery ajax实例教程和一些高级用法
    常用js正则表达式大全
    HTML5 + JS 调取摄像头拍照下载
    JS运动框架
    JS 实现AJAX封装(只限于异步)
    事件委托详解
    JS 实现随机验证码功能
    利用JS调取电脑摄像头,实现拍照功能
  • 原文地址:https://www.cnblogs.com/zzb-yp/p/10020403.html
Copyright © 2011-2022 走看看