zoukankan      html  css  js  c++  java
  • Java文件与io——字节流

    FileOutputStream用于写入诸如图像数据之类的原始字节的流

    字节输出流:OutputStream 此抽象类表示输出字节流的所有类的超类。(写)

    字节输入流:InputStream(读)

    public class ByteStreamDemo {
    
        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO 自动生成的方法存根
            write();
            //read();
        }
        
        public static void write(){
            File file=new File("c:\a.txt");
            try {
                OutputStream out = new FileOutputStream(file,true);//加true每执行一次就会保留一次
                String info="我爱学编程11111111,/.2";
                out.write(info.getBytes());
                out.close();//关闭释放资源
            } catch (FileNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            
        }
        
        public static void read(){
            File file=new File("c:\a.txt");
            try {
                InputStream in=new FileInputStream(file);
                byte[] bytes=new byte[1024*1024*10];//一次读取10兆
                int len=-1;//Java中规定读到-1就停止
                StringBuffer sb=new StringBuffer();//构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
                while((len=in.read(bytes))!=-1){
                    sb.append(new String(bytes, 0, len));
                }
                in.close();
                System.out.println(sb);
            } catch (FileNotFoundException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
        }
    
    }
  • 相关阅读:
    宝物筛选
    [HAOI2008]糖果传递
    线段树(区间查询,区间修改)——标记永久化版
    图的割边
    图的割点
    P2066 机器分配
    SP1700 TRSTAGE
    P4568 [JLOI2011]飞行路线
    POJ 2533 Longest Ordered Subsequence
    HDU 2512 一卡通大冒险
  • 原文地址:https://www.cnblogs.com/shenhainixin/p/5109335.html
Copyright © 2011-2022 走看看