• IO流参考


    1

    import java.io.File;
    import java.io.FileInputStream;
    
    /**
     * 读取一个字符
     */
    public class MyReadChar {
        public static void main(String[] args) {
            try {
                File f = new File("D:\labs\test.txt");
    
                FileInputStream fis = new FileInputStream(f);
    
                System.out.println((char)fis.read());
            } catch (Exception e) {
    
            }
        }
    }

    2

    import java.io.File;
    import java.io.FileInputStream;
    
    public class MyReadFile {
        public static void main(String[] args) {
            try {
                File f = new File("D:\labs\test.txt");
    
                FileInputStream fis = new FileInputStream(f);
    
                int length = fis.available();
                for (int i = 0; i <length; ++i) {
                    System.out.print((char)fis.read());
                }
            } catch (Exception e) {
    
            }
        }
    }

    3

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class MyCopyFile {
        public static void main(String[] args) {
            try {
                File in = new File("D:\labs\test.txt");
                File out = new File("D:\labs\another_test.txt");
    
                FileInputStream fis = new FileInputStream(in);
                FileOutputStream fos = new FileOutputStream(out);
    
                int length = fis.available();
                for (int i = 0; i < length; ++i) {
                    fos.write(fis.read());
                }
            } catch (Exception e) {
    
            }
        }
    }

    4

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class MyCopyBigFile {
        public static void main(String[] args) {
            try {
                File in = new File("D:\labs\test.txt");
                File out = new File("D:\labs\another_test.txt");
    
                FileInputStream fis = new FileInputStream(in);
                FileOutputStream fos = new FileOutputStream(out);
    
                byte[] tmp = new byte[8192];
                // 处理大部分内容
                int length = fis.available()/8192;
                for (int i = 0; i < length; ++i) {
                    fis.read(tmp);
                    fos.write(tmp);
                }
                int size = fis.read(tmp);
                fos.write(tmp, 0, size);
            } catch (Exception e) {
            }
        }
    }

    5

    import java.io.File;
    import java.io.FileOutputStream;
    
    public class MyKey {
        public static void main(String[] args) {
            try {
                File f = new File("D:\labs\key.key");
    
                FileOutputStream fos = new FileOutputStream(f);
    
                for (int i = 0; i < 128; ++i) {
                    fos.write((int)(Math.random()*128));
                }
            } catch (Exception e) {
    
            }
        }
    }

    6

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    public class MyEncryp {
        public static void main(String[] args) {
            try {
                int key[] = new int[128];
                File keyFile = new File("D:\labs\key.key");
    
                FileInputStream keyFis = new FileInputStream(keyFile);
    
                for (int i = 0; i < 128; ++i) {
                    key[i] = keyFis.read();
                }
    
                File in = new File("D:\labs\test.txt");
                File out = new File("D:\labs\another_test.txt");
    
                FileInputStream fis = new FileInputStream(in);
                FileOutputStream fos = new FileOutputStream(out);
    
                int length = fis.available();
                for (int i = 0; i < length; ++i) {
                    fos.write(fis.read() + key[i%128]);
                }
            } catch (Exception e) {}
        }
    }
  • 相关阅读:
    i春秋暑期训练营丨渗透测试工程师开课啦
    CTF必备技能丨Linux Pwn入门教程——环境配置
    i春秋四周年中奖名单出炉丨确认过眼神,你是中奖人
    i春秋四周年福利趴丨一纸证书教你赢在起跑线
    「你学习,我买单」i春秋四周年精品课程福利专场
    i春秋四周年庆典狂欢丨价值6000元的Web安全课程免费送啦
    倒计时3天!i春秋四周年盛典狂欢,钜惠不停
    「学习攻略」0基础转型白帽黑客第一步是什么?
    奖金高达150万元的NEST即将上线,速来报名
    「白帽挖洞技能」YxCMS 1.4.7 漏洞分析
  • 原文地址:https://www.cnblogs.com/xkxf/p/7105866.html
Copyright © 2020-2023  润新知