zoukankan      html  css  js  c++  java
  • 缓冲流,转换流

    使用字节缓冲流和数组来复制一个东西

    import java.io.*;
    
    public class test1 {
        public static void main(String[] args) throws Exception {
    
            long start = System.currentTimeMillis();
            String path1 = "/Users/mima000000/Desktop/1.jpeg";
            String path2 = "/Users/mima000000/Desktop/2.jpeg";
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(path1));
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(path2));
            int len;
            byte[] bytes = new byte[1024];
            while ((len=bufferedInputStream.read(bytes))!=-1){
                bufferedOutputStream.write(bytes,0,len);
            }
            bufferedOutputStream.close();
            bufferedInputStream.close();
    
        }
    }
    
    

    转换流

    InputStreamReader指定GBK来读取

    import com.sun.xml.internal.fastinfoset.util.ValueArrayResourceException;
    import sun.nio.cs.ext.GBK;
    
    import java.io.*;
    
    public class test1 {
        public static void main(String[] args) throws Exception {
            String path = "/Users/mima000000/Desktop/1.txt";
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path), "GBK");
            int read;
            while ((read=inputStreamReader.read())!=-1){
                System.out.println((char)read);
            }
            inputStreamReader.close();
    
        }
    }
    
    

    转换文件的编码,读取一个gbk 文件,保存为utf-8

    import com.sun.xml.internal.fastinfoset.util.ValueArrayResourceException;
    import sun.nio.cs.ext.GBK;
    
    import java.io.*;
    
    public class test1 {
        public static void main(String[] args) throws Exception {
            String path = "/Users/mima000000/Desktop/1.txt";
            String path2 = "/Users/mima000000/Desktop/2.txt";
            InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream(path), "GBK");
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream(path2) ,"UTF-8");
            int len;
            char[] temp = new char[1024];
            while ((len=inputStreamReader.read(temp))!=-1){
                outputStreamWriter.write(temp,0,len);
            }
            outputStreamWriter.close();
            inputStreamReader.close();
    
        }
    }
    
    
  • 相关阅读:
    记录一下自己的洛谷的题解
    初学java 学生管理系统——v0002版本
    初学java 学生管理系统——v0001版本
    Redis守护进程作用+数据类型
    java实现发送短信验证码
    Kali入侵入门版笔记!!!
    2020实现ssh公网外联和外网远程穿透以及内网穿透防火墙
    监控键盘和鼠标记录内容和截屏,更新版本2.0,增加了Linux服务端!!!
    Git管理软件开发项目入门版
    2020年Windows下开机自动执行最强
  • 原文地址:https://www.cnblogs.com/hellosiyu/p/12499958.html
Copyright © 2011-2022 走看看