利用数据流实现复制文件

package com.mashensoft;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;

/**
 * 读取文本文件,读取图片,读取视频
 * 
 * @author PeicongHe
 *
 */
public class Homework {
    /**
     * 实现复制文件 运用FileInputStream,FileOutputStream对文件进行读取写入 只能一个一个字符复制(速度慢)
     */
    public static void copyFile(String sourceName, String dest) {
        // 1:读取一个文件
        // 2:写入一个文件
        try {
            FileInputStream fis = new FileInputStream(sourceName);// 源文件地址
            FileOutputStream fos = new FileOutputStream(dest);// 目的文件地址
            int a = 0;
            while ((a = fis.read()) != -1) {
                fos.write(a);
            }
            fos.flush();
            fos.close();
            fis.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * test2
     * 实现复制文件(提速) 运用FileInputStream,FileOutputStream对文件进行读取写入 引用数组byte[] 缺点:
     * 当使用数组的时候,如果文件里的字符的长度不是数组的倍数的时候,拿到的数据会重复
     */
    public static void test2() {
        byte[] myArray = new byte[3];
        try {
            InputStream fis = new FileInputStream("src/a.txx");
            while (fis.read(myArray) != -1) {
                // 如果达到文件尾,跳出循环
                for (int i = 0; i < myArray.length; i++) {
                    System.out.print((char) myArray[i]);
                }
            }
            fis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test3
     * 实现复制文件(提速)(改进) 运用FileInputStream,FileOutputStream对文件进行读取写入 引用数组byte[] 缺点:
     * 当文件的内容是中的时候会出现乱码
     */
    public static void test3() {
        byte[] myArray = new byte[3];
        int len;// 长度
        try {
            FileInputStream is = new FileInputStream("src/a.txt");
            while ((len = is.read(myArray)) != -1) {
                // 定义一个新的数组,防止有多余的数据
                for (int i = 0; i < len; i++) {
                    System.out.print((char) myArray[i]);
                }
            }
            is.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * test4
     * 实现复制文件 (速度快) 运用FileInputStream,FileOutputStream对文件进行读取写入 只能一个一个字符复制(速度快)
     * write
     */
    public static void test4() {
        byte[] myArray = new byte[32];
        int len;
        try {
            InputStream is = new FileInputStream("src/BangBang.flac");
            FileOutputStream os = new FileOutputStream("src/BangBangx.flac");
            // 如果到达文件尾,无法满足while的条件,不会执行while语句的内容
            while ((len = is.read(myArray)) != -1) {
                // 定义一个新的数组,防止有多余的数据
                byte descArray[] = new byte[len];
                for (int i = 0; i < len; i++) {
                    descArray[i] = myArray[i];
                }
                os.write(descArray);
            }
            is.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * test5
     * 实现复制文件 (速度快)(jdk)
     */
    public static void test4x(String sourceName, String targetName) {
        byte myArray[] = new byte[1024];
        int len;
        try {
            InputStream fis = new FileInputStream(sourceName);
            FileOutputStream fos = new FileOutputStream(targetName);
            while ((len = fis.read(myArray)) != -1) {
                /**
                 * os.write JDK系统是已经直接封装好的,可以直接用,速度会更快一些 os.write(数字长度,起始,末尾)
                 */
                fos.write(myArray, 0, len);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void test4xx() {
        try {
            FileInputStream fis = new FileInputStream("src/a.txt");
            FileOutputStream fos = new FileOutputStream("src/axx.txt");
            int len;
            byte[] myArray = new byte[1024];
            while ((len = fis.read(myArray)) != -1) {
                fos.write(myArray, 0, len);
            }
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("执行成功");

    }

    /**
     * test6
     * 使用缓冲区,提高速率
     * 用到FileInputStream,FileOutputStream,BufferedInputStream,BufferedOutputStream
     * 用缓冲区的好处
     * 没有缓存区,那么每read一次,就会发送一次IO操作;
有缓存区,第一次read时,会一下读取x个字节放入缓存区,然后后续的read都会从缓存中读取,当read到缓存区末尾时,会再次读取x个字节放入缓存区。
很明显,第二种方式,会减少IO操作,效率更高,缺点就是,内存占用的多。
     */
    public static void test6() {
        InputStream is;
        try {
            is = new FileInputStream("src/BangBang.flac");
            FileOutputStream os = new FileOutputStream("src/BangBangxx.flac");
            BufferedInputStream bis = new BufferedInputStream(is);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            byte myArrat[] = new byte[1024];
            int len;
            while ((len = bis.read(myArrat)) != -1) {
                bos.write(myArrat, 0, len);
            }
            bos.flush();
            bis.close();
            bos.close();
            os.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test8
     * 当我们没有文件的时候,如何使用流
     * 这里用到的是字节数组输出流和字节数组输入流
     * ByteArrayOutputStream类是在创建它的实例时,程序内部创建一个byte型别数组的缓冲区,然后利用ByteArrayOutputStream和ByteArrayInputStream的实例向数组中写入或读出byte型数据。在网络传输中我们往往要传输很多变量,我们可以利用ByteArrayOutputStream把所有的变量收集到一起,然后一次性把数据发送出去。
     */
    public static void test8() {
        String content = "hepeicongshidashuaige";
        ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        int a = 0;
        byte[] myArray = new byte[3];
        try {
            while ((a = is.read(myArray)) != -1) {
                for(int i=0;i<a;i++){
                System.out.print((char)myArray[i]);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * test9
     * 问题:当有中文的时候,可能会出现乱码
     */
    public static void test9() {
        String content = "你";
        byte myArray[] = content.getBytes();
        for(int i = 0;i<myArray.length;i++){
        System.out.println((char)myArray[i]);
        }
    }

    /**
     * test10
     * 问题:当有中文的时候,可能出现乱码,当有了流的时候
     * 出现乱码的原因是:字符的是字节是2个长度,而read(byte[])以一个字节扫描。将字符的两个字节拆分开了,所以出现乱码
     */
    public static void test10() {
        String content = "你";
        ByteArrayInputStream is = new ByteArrayInputStream(content.getBytes());
        byte a = 0;
        while ((a = (byte) is.read()) != -1) {
            System.out.print((char) a);
        }
    }

    /**
     * test11
     * 解决方法:利用数组Array[2],和ByteArrayInputeStream,ByteArrayOutputStream,将字符的数据存储在一起(以二的倍数存储)最后一起输出就可以避免出现乱码
     */
    public static void test11() {
        String content = "你";
        ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
        byte a[] = new byte[2];
        try {
            bis.read(a);
            System.out.println(new String(a));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * text12
     * 同理输出一串中文
     */
    public static void test12() {
        String content = "杨晓怡";
        ByteArrayInputStream bis = new ByteArrayInputStream(content.getBytes());
        byte a[] = new byte[2];
        int len;
        try {
            while((len=bis.read(a))!=-1){
                for(int i=0;i<len-1;i++){
                System.out.println(new String(a));
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void test12x() {
        try {
            FileInputStream is = new FileInputStream("src/a.txt");
            byte myArray[] = new byte[2];
            int a = 0;
            try {
                while ((a = is.read(myArray)) != -1) {
                    System.out.println(new String(myArray));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    /**
     * test13
     * 引用字符流解决中文问题 Reader 抽象类,InputStreamReader实现类
     * 只能输出一个中文(一个字符)
     */
    public static void test13() {
        try {
            Reader reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            int a = reader.read();
                    System.out.print((char)a);

            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test14
     * 引用字符流解决中文的问题 Reader抽象类,InputStreamReader实现类 读取到的数组中,一次可以读取多个字符
     *  
     */
    public static void test14() {
        Reader reader;
        try {
            reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            char myArray[] = new char[3];
            int a = 0;
            while ((a = reader.read(myArray)) != -1) {
                System.out.println(new String(myArray));
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * test15
     * 用字符流写入数据到文件里
     * 这里用到Writer,OutputStreamWriter,FileOutputStream
     */
    public static void test15() {
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream("src/a.txt", true));
            //指向操作对象
            //true表示可以在文件的原有内用上写入内容
            writer.write("jiajia");
            writer.flush();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("写入成功");

    }

    /**
     * test16
     * 复制文件
     * 利用Reader,Writer,InputStreamReader,OutputStreamReader
     */
    public static void test15x() {
        try {
            Reader reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            Writer writer = new OutputStreamWriter(new FileOutputStream("src/a15x.txt"));
            char myArray[] = new char[3];
            int a = 0;//长度
            while ((a = reader.read(myArray)) != -1) {
                System.out.println(new String(myArray));
                writer.write(myArray);
            }
            writer.flush();
            reader.close();
            writer.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("复制成功");
    }
    public static void test15xx(){
        try {
            Reader reader = new InputStreamReader(new FileInputStream("src/a.txt"));
            Writer writer = new OutputStreamWriter(new FileOutputStream("src/axxx.txt"));
            char[] myArray=new char[32];
            int len;
            while((len=reader.read(myArray))!=-1){
                System.out.println(new String(myArray));
                writer.write(myArray);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 利用缓冲,使写入字符流更高效
     * 用到BufferedWriter
     * BufferedWriter(new OutputStreamWriter(new FileOutputStream(where)))
     */
    public static void test17() {
        try {
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/d.txt")));
            bw.write("hello,何沛聪");
            bw.flush();
            bw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    /**
     * test18
     * 为了能够写入数据的时候更加方便,我们要引入printWriter
     * 用到PrintWriter
     */
    public static void test18() {
        try {
            PrintWriter pw = new PrintWriter("src/e.txt");
            pw.print("回车\n");
            pw.print("新行\r");
            pw.print(18);
            pw.flush();
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("执行成功");
    }
    /**
     * 读取文件的时候更加简单 scanner
     */
    public static void test19() {
        Scanner sc;
        try {
            sc = new Scanner(new FileInputStream("src/a15x.txt"));
            while (sc.hasNextLine()) {
                System.out.println(sc.nextLine());
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 复制 利用print - sanner
     */
    public static void test19x() {
        try {
            Scanner sc=new Scanner(new FileInputStream("src/a.txt"));
            PrintWriter pw = new PrintWriter("src/acp.txt");
            while(sc.hasNextLine()){
//              System.out.println(sc.nextLine());
                pw.print(sc.nextLine());
            }
            sc.close();
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        System.out.println("执行成功");
    }
    /**
     * 复制 利用BufferedWriter - Scanner
     */
    public static void test19xx(){
        try {
            Scanner sc=new Scanner(new FileInputStream("src/a.txt"));
            BufferedWriter bfw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/acpx.txt")));
            while(sc.hasNextLine()){
                bfw.write(sc.nextLine());
            }
            //将缓存区的数据强制写入到目标文件
            bfw.flush();
            bfw.close();
            sc.close();
            System.out.println("执行成功");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    public static void main(String[] args) {
        long a = System.currentTimeMillis();
        // copyFile("a.txt","b.txt");
        // test4();
        // test4x("BangBang.flac","BangBangtx.flac");
        test19xx();
        System.out.println("\r<br>执行耗时 : " + (System.currentTimeMillis() - a) / 1000f + " 秒 ");
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
摘要:NTFS是Microsoft公司开发的一种有着良好安全性和稳定性的高性能文件系统,NTFS的文件文件夹中附加多个额外的数据流,但是其访问一直没有很好的解决办法,本文使用VB2003实现NTFS文件附加数据流的读写类,提供.Net框架下NTFS文件附加数据流的完整解决方案。 关键词:VB.Net NTFS 数据流 类 在项目中选择添加引用->浏览->选择“JWBStreamOP.dll”文件->确定,即可成功引用。 4.1 类的声明: Dim myStreamOP As New ClassJWBStreamOP(“NTFS文件完整路径”) 4.2 属性: 该类共有3个只读属性 属性名 返回值类型 备注 FileName String 只读,在成功声明后使用 Ready Boolean 只读,该类可操作时为True Ver String 只读,类版本、版权信息 4.3 方法 该类共有6个方法: 4.3.1 OpenNTFSStream(ByVal sStreamName As String) As System.IO.FileStream 打开指定文件(声明时指定)的指定数据流,返回值为指定数据流的FileStream接口。 参数列表 类型 传递方式 参数说明 sStreamName String Byval 流文件名 4.3.2 GetNTFSStreamSize(ByVal sStreamName As String) As Long 获取指定数据流的大小,返回实际大小,执行失败返回-1 参数列表 类型 传递方式 参数说明 sStreamName String Byval 流文件名 4.3.3 AddNTFSStream(ByVal toHidName As String, ByRef percentDone As Double) As Boolean 添加附加数据流,返回执行结果。 参数列表 类型 传递方式 参数说明 toHidName String ByVal 待添加的文件路径 percentDone Double ByRef 传递一个完成百分比的参数 4.3.4 SaveNTFSStream(ByVal sStreamName As String, ByVal outFileName As String, ByRef percentDone As Double) As Boolean将指定的数据流保存为文件,返回执行结果。 参数列表 类型 传递方式 参数说明 sStreamName String ByVal 流文件名 outFileName String ByVal 保存文件路径 percentDone Double ByRef 传递一个完成百分比的参数 4.3.5 ReadNTFSStreamsName() As String() 获取文件的所有附加数据流名称,返回名称数组。 4.3.6 DeleteNTFSStream(ByVal sStreamName As String) As Boolean 删除指定数据流,返回执行结果。 参数列表 类型 传递方式 参数说明 sStreamName String Byval 流文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值