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;
    }
    }
  • 相关阅读:
    Go 接口
    Go 参数传递
    Go 结构体
    Go 指针
    使用ContentType处理大量的外键关系
    django的render的特殊用法
    restframework中的那些参数你知道吗?
    scrapy框架
    numpy如何使用
    HTML 5 audio标签
  • 原文地址:https://www.cnblogs.com/KevinFeng/p/12955314.html
Copyright © 2011-2022 走看看