zoukankan      html  css  js  c++  java
  • FileInputStream和FileOutputStream的使用实例

    package io;
    
    import org.junit.Test;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class StreamDemoTest {
        private String path0 = System.getProperty("user.dir") + "/src/main/resources/copy0.txt";
        private String path = System.getProperty("user.dir") + "/src/main/resources/d.txt";
        private File file = null;
        private FileInputStream fis = null;
        private FileOutputStream fos = null;
    
        @Test
        public void fun() throws Exception {
            FileInputStream fis = new FileInputStream(new File(this.getClass().getResource("/a.txt").getPath()));
            int i = 0;
            int i2 = 0;
            int i3 = 0;
            while ((i = fis.read()) != -1) {
                /*System.out.println((char)i);*/
                if (!(i >= 0 && i < 128)) {
                    i2 = fis.read();
                    i3 = fis.read();
                    //UTF-8编码的情况,一个汉字占用3个字节
                    System.out.println(new String(new byte[]{(byte) i, (byte) i2, (byte) i3}));
                    //GBK编码的情况,一个汉字占用2个字节
                    /*System.out.println(new String(new byte[]{(byte) i, (byte) i2}));*/
                    continue;
                }
                System.out.println((char) i);
            }
            fis.close();
        }
    
        @Test
        public void fun2() throws Exception {
            File file = new File(path);
            if (!file.exists()) {
                file.createNewFile();
                fileOutputStreamFun();
            } else {
                fileOutputStreamFun();
            }
        }
    
        public void fileOutputStreamFun() {
            try {
                fos = new FileOutputStream(file);
                String a = "虎哥明天买车";
                fos.write(a.getBytes());
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        @Test
        public void copy0() throws Exception {
            fis = new FileInputStream(new File("C:\Users\15082157\Desktop\自我介绍.txt"));
            fos = new FileOutputStream(path0);
            File file = new File(path0);
            if (!file.exists()) {
                file.createNewFile();
                fileOutputStreamCopy1();
            } else {
                fileOutputStreamCopy1();
            }
        }
    
        @Test
        public void copy() throws Exception {
            String path1 = System.getProperty("user.dir") + "/src/main/resources/copy.mp4";
            fis = new FileInputStream(new File("C:\Users\15082157\Desktop\3-4视频19秒.mp4"));
            fos = new FileOutputStream(path1);
            File file = new File(path1);
            if (!file.exists()) {
                file.createNewFile();
                /*fileOutputStreamCopy();*/
                fileOutputStreamCopy1();
            } else {
                /*fileOutputStreamCopy();*/
                fileOutputStreamCopy1();
            }
        }
    
        public void fileOutputStreamCopy() {
            int i = 0;
            int i2 = 0;
            int i3 = 0;
            byte hanZi[];
            try {
                long beginTime = getCurrentTime1();
                while ((i = fis.read()) != -1) {
                    if (!(i >= 0 && i < 128)) {
                        i2 = fis.read();
                        i3 = fis.read();
                        hanZi = new byte[]{(byte) i, (byte) i2, (byte) i3};
                        fos.write(hanZi);
                        continue;
                    }
                    fos.write(i);
                }
                fos.flush();
                long endTime = getCurrentTime1();
                float time = (endTime - beginTime) / 1000F;
                System.out.println("copy时间为" + time + "s");
                fis.close();
                fos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void fileOutputStreamCopy1(){
            int len =0;
            byte[] bytes = new byte[1024]; // 创建一个长度是1024的byte类型的数组,用于存储读取到的数据
            try{
                long beginTime = getCurrentTime1();
                while ((len=fis.read(bytes))!=-1){
                    fos.write(bytes);
                }
                fos.flush();
                long endTime = getCurrentTime1();
                float time = (endTime - beginTime) / 1000F;
                System.out.println("copy时间为" + time + "s");
                fis.close();
                fos.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    
    
        public long getCurrentTime1() {
            long currentTime = System.currentTimeMillis();
            System.out.println(currentTime);
            return currentTime;
        }
    }
    package io;

    import org.junit.Test;

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

    public class StreamDemoTest {
    private String path0 = System.getProperty("user.dir") + "/src/main/resources/copy0.txt";
    private String path = System.getProperty("user.dir") + "/src/main/resources/d.txt";
    private File file = null;
    private FileInputStream fis = null;
    private FileOutputStream fos = null;

    @Test
    public void fun() throws Exception {
    FileInputStream fis = new FileInputStream(new File(this.getClass().getResource("/a.txt").getPath()));
    int i = 0;
    int i2 = 0;
    int i3 = 0;
    while ((i = fis.read()) != -1) {
    /*System.out.println((char)i);*/
    if (!(i >= 0 && i < 128)) {
    i2 = fis.read();
    i3 = fis.read();
    //UTF-8编码的情况,一个汉字占用3个字节
    System.out.println(new String(new byte[]{(byte) i, (byte) i2, (byte) i3}));
    //GBK编码的情况,一个汉字占用2个字节
    /*System.out.println(new String(new byte[]{(byte) i, (byte) i2}));*/
    continue;
    }
    System.out.println((char) i);
    }
    fis.close();
    }

    @Test
    public void fun2() throws Exception {
    File file = new File(path);
    if (!file.exists()) {
    file.createNewFile();
    fileOutputStreamFun();
    } else {
    fileOutputStreamFun();
    }
    }

    public void fileOutputStreamFun() {
    try {
    fos = new FileOutputStream(file);
    String a = "虎哥明天买车";
    fos.write(a.getBytes());
    fos.flush();
    fos.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    @Test
    public void copy0() throws Exception {
    fis = new FileInputStream(new File("C:\Users\15082157\Desktop\自我介绍.txt"));
    fos = new FileOutputStream(path0);
    File file = new File(path0);
    if (!file.exists()) {
    file.createNewFile();
    fileOutputStreamCopy1();
    } else {
    fileOutputStreamCopy1();
    }
    }

    @Test
    public void copy() throws Exception {
    String path1 = System.getProperty("user.dir") + "/src/main/resources/copy.mp4";
    fis = new FileInputStream(new File("C:\Users\15082157\Desktop\3-4视频19.mp4"));
    fos = new FileOutputStream(path1);
    File file = new File(path1);
    if (!file.exists()) {
    file.createNewFile();
    /*fileOutputStreamCopy();*/
    fileOutputStreamCopy1();
    } else {
    /*fileOutputStreamCopy();*/
    fileOutputStreamCopy1();
    }
    }

    public void fileOutputStreamCopy() {
    int i = 0;
    int i2 = 0;
    int i3 = 0;
    byte hanZi[];
    try {
    long beginTime = getCurrentTime1();
    while ((i = fis.read()) != -1) {
    if (!(i >= 0 && i < 128)) {
    i2 = fis.read();
    i3 = fis.read();
    hanZi = new byte[]{(byte) i, (byte) i2, (byte) i3};
    fos.write(hanZi);
    continue;
    }
    fos.write(i);
    }
    fos.flush();
    long endTime = getCurrentTime1();
    float time = (endTime - beginTime) / 1000F;
    System.out.println("copy时间为" + time + "s");
    fis.close();
    fos.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }

    public void fileOutputStreamCopy1(){
    int len =0;
    byte[] bytes = new byte[1024]; // 创建一个长度是1024byte类型的数组,用于存储读取到的数据
    try{
    long beginTime = getCurrentTime1();
    while ((len=fis.read(bytes))!=-1){
    fos.write(bytes);
    }
    fos.flush();
    long endTime = getCurrentTime1();
    float time = (endTime - beginTime) / 1000F;
    System.out.println("copy时间为" + time + "s");
    fis.close();
    fos.close();
    }catch (Exception e){
    e.printStackTrace();
    }
    }


    public long getCurrentTime1() {
    long currentTime = System.currentTimeMillis();
    System.out.println(currentTime);
    return currentTime;
    }
    }
  • 相关阅读:
    记第一场省选
    POJ 2083 Fractal 分形
    CodeForces 605A Sorting Railway Cars 思维
    FZU 1896 神奇的魔法数 dp
    FZU 1893 内存管理 模拟
    FZU 1894 志愿者选拔 单调队列
    FZU 1920 Left Mouse Button 简单搜索
    FZU 2086 餐厅点餐
    poj 2299 Ultra-QuickSort 逆序对模版题
    COMP9313 week4a MapReduce
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955314.html
Copyright © 2011-2022 走看看