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();
        }
    }
  • 相关阅读:
    Web上传大文件的解决方案
    JS上传大文件的解决方案
    网页上传大文件的解决方案
    B/S上传大文件的解决方案
    Unity UGUI——提供可视功能的UI组件(Text)
    Java设计模式透析之 —— 策略(Strategy)
    【边做项目边学Android】小白会遇到的问题--Appcompat_V7问题
    高度平衡树 -- AVL 树
    成长这事儿,不可不说-------Day36
    Cocos2D-X2.2.3学习笔记5(UI系统)
  • 原文地址:https://www.cnblogs.com/zzb-yp/p/10020403.html
Copyright © 2011-2022 走看看