zoukankan      html  css  js  c++  java
  • 字节流

    字节输出流OutputStream
    FileOutputStream类
    构造方法
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo01 {
        public static void main(String[] args) throws IOException {
            method1();
            method2();
        }
        public static void method1() throws IOException{
            //创建字节输出流对象
            //如果该文件有则覆盖,如果没有则创建
            FileOutputStream fos=new FileOutputStream("E:\java\demo.txt");
            byte[] bytes={97,98,99,100};
            fos.write(100);
            fos.write(bytes, 1, 1);//数组存
            fos.write("你好么".getBytes());//直接扔
            //释放资源
            fos.close();
        }
        //续写
        public static void method2() throws IOException{
            FileOutputStream fos=new FileOutputStream("E:\java\b.txt",true);
            fos.write("
    你好么".getBytes());
            //释放资源
            fos.close();
        }
    }
    IO异常的处理
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo02 {
        public static void main(String[] args) {
            //创建字节输出流对象
            FileOutputStream fos=null;
            try{
                fos=new FileOutputStream("E:\java");
                fos.write("你好".getBytes());
            }catch(IOException e){
                e.printStackTrace();
                //抛出运行异常终止程序
                throw new RuntimeException();
            }finally{
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    字节输入流InputStream
    FileInputStream类
    构造方法
    方法
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class Demo03 {
        public static void main(String[] args) throws IOException {
            method1();
            method2();
            method3();
            method4();
            method5();
        }
        public static void method1() throws IOException{
            //明确数据源
            FileInputStream fos=new FileInputStream("E:\java\demo.txt");
            //从文件中读取一个字节
            int len1=fos.read();
            System.out.println((char)len1);
            len1=fos.read();
            System.out.println((char)len1);
            len1=fos.read();
            System.out.println(len1);
            fos.close();
        }
        //从文件读取内容
        public static void method2() throws IOException{
            FileInputStream fos=new FileInputStream("E:\java\demo.txt");
            int len=0;
            while((len=fos.read())!=-1){
                System.out.println((char)len);
            }
            fos.close();
        }
    读取数据read(byte[])方法
    //数组读取
        public static void method3() throws IOException{
            FileInputStream fos=new FileInputStream("E:\java\demo.txt");
            byte[] bytes=new byte[2];
            int len=fos.read(bytes);
            System.out.println(new String(bytes));
            System.out.println(len);
            len=fos.read(bytes);
            System.out.println(new String(bytes));
            System.out.println(len);
            fos.close();
        }
        public static void method4() throws IOException{
            FileInputStream fos=new FileInputStream("E:\java\demo.txt");
            byte[] bytes=new byte[2];
            int len=0;
            while((len=fos.read(bytes))!=-1){
                System.out.println(new String(bytes,0,len));//不要后边多出来的    解码
            }
        }
    复制文件
    //文件复制
        public static void method5() throws IOException{
        FileInputStream fis=newFileInputStream("E:\java\demo.txt");
        FileOutputStream fos=newFileOutputStream("E:\java\a.txt");
            int len=0;
            while((len=fis.read())!=-1){
                fos.write(len);
            }
            fis.close();
            fos.close();
        }
    }
  • 相关阅读:
    00 学习资源整理
    07 MySQL的应用层调整,查询缓存设置,内存管理设置,并发参数的设置常识
    06 SQL语句编写优化
    05 Java的ReentrantLock与线程的顺序控制
    05 索引的使用常识(如何编写SQL语句避免索引失效)
    04 MYSQ的SQL优化需要了解的工具explain,profile,trace
    04 JAVA中park/unpark的原理以及JAVA在API层面线程状态总结
    03 MYSQL的体系结构以及存储引擎的基本知识
    02 链表编程题
    01 栈与队列
  • 原文地址:https://www.cnblogs.com/zhaotao11/p/10248119.html
Copyright © 2011-2022 走看看