zoukankan      html  css  js  c++  java
  • 字节流和字符流小练习

    1 FileOutputStream

    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    public class test {
        public static void main(String[] args) throws Exception {
            FileOutputStream fos = new FileOutputStream("/Users/mima000000/Desktop/1.txt",false);  //true表示追加
    //        每次写出一个字节数据
            fos.write(97); //a
            fos.write(98);  //b
    
            //换行
            fos.write("
    ".getBytes());
    
    
    //        写出字节数组
            byte[] bytes = " 不想加班".getBytes();
            fos.write(bytes);
            fos.close();
        }
    }
    
    

    2 FileInputStream

    2-1 每次读取1个字节

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    public class test {
        public static void main(String[] args) throws Exception {
            FileInputStream fis = new FileInputStream("/Users/mima000000/Desktop/1.txt");
            //每次读取一个字节数据
            int b;
            while ((b=fis.read())!=-1){
                System.out.println((char)b);
            }
            fis.close();
        }
    }
    

    2-2 使用有效字节数组读取

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    
    public class test {
        public static void main(String[] args) throws Exception {
            FileInputStream fis = new FileInputStream("/Users/mima000000/Desktop/1.txt");
            //每次读取一个字节数据
            int b;
    //        使用字节数组读取,每次最多读取2个字节
            byte[] bytes = new byte[2];
            while ((b= fis.read(bytes))!=-1){
                System.out.println(new String(bytes,0,b));
            }
            fis.close();
        }
    }
    
    

    3 FileReader

    3-1 每次读取一个字符数据

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    
    public class test {
        public static void main(String[] args) throws Exception {
            FileReader fileReader = new FileReader("/Users/mima000000/Desktop/1.txt");
            int b;
            while ((b=fileReader.read())!=-1){
                System.out.println((char)b);
            }
    
        }
    }
    

    3-2 使用字符数组读取

    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.FileReader;
    
    public class test {
        public static void main(String[] args) throws Exception {
            FileReader fileReader = new FileReader("/Users/mima000000/Desktop/1.txt");
            int b;
            char[] temp = new char[2];
            while ((b=fileReader.read(temp))!=-1){
                String res = new String(temp, 0, b);
                System.out.println(res);
            }
    
        }
    }
    
    

    4 FileWriter

    import java.io.*;
    
    public class test {
        public static void main(String[] args) throws Exception {
            FileWriter fileWriter = new FileWriter("/Users/mima000000/Desktop/1.txt");
    //        写字符串
            fileWriter.write("你好");
            //写字符数组
            char[] temp = {'e','t'};
            fileWriter.write(temp);
            //写单个字符
            fileWriter.write('g');
    
            //刷新缓冲
            fileWriter.flush();
            //关闭
             fileWriter.close();
    
    
    
        }
    }
    
    
  • 相关阅读:
    【BZOJ5302】[HAOI2018]奇怪的背包(动态规划,容斥原理)
    【BZOJ5303】[HAOI2018]反色游戏(Tarjan,线性基)
    【BZOJ5304】[HAOI2018]字串覆盖(后缀数组,主席树,倍增)
    【BZOJ5305】[HAOI2018]苹果树(组合计数)
    【BZOJ5300】[CQOI2018]九连环 (高精度,FFT)
    【BZOJ5292】[BJOI2018]治疗之雨(高斯消元)
    【BZOJ5298】[CQOI2018]交错序列(动态规划,矩阵快速幂)
    【BZOJ5289】[HNOI2018]排列(贪心)
    Codeforces Round #539 Div1 题解
    【BZOJ5288】[HNOI2018]游戏(拓扑排序)
  • 原文地址:https://www.cnblogs.com/hellosiyu/p/12489784.html
Copyright © 2011-2022 走看看