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

    1  FileOutputStream类

    OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。

    FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。

    public class Demo01 {
        public static void main(String[] args) throws IOException {
            //明确目的地
            //构造方法在文件中不存在时,自动创建
            //文件存在则覆盖
            FileOutputStream fos=new FileOutputStream("D:\test\s.txt");
            //向文件中写一个字节
            fos.write(100);    
            //向文件中写字节数组
            byte[]bytes={97,98,99,100,101};
            fos.write(bytes,2,2);
            //释放资源
            fos.close();
        }
    }

    2.文件的续写和换行

    " ":实现换行


    public class Demo02 {
        public static void main(String[] args){
            FileOutputStream fos=null;
            try{
            //明确目的地,true表示续写,false则新建一个覆盖
            fos=new FileOutputStream("D:\test\s.txt",true);
            fos.write("续写内容".getBytes());
            fos.write("
    换行了".getBytes());
            }catch(IOException ex){
                ex.printStackTrace();
                throw new RuntimeException("文件写入失败!");
            }finally{
                //释放资源
                try {
                    if(fos!=null)
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    throw new RuntimeException("输出流关闭失败!");
                }
            }
        }
    }

    3      FileInputStream类

    InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。

    FileInputStream 从文件系统中的某个文件中获得输入字节。

    一次读一个字节:

    public class Dome03 {
        public static void main(String[] args) throws IOException {
            //明确数据源
            FileInputStream fis=new FileInputStream("D:\test\s.txt");
            //一次读一个字节
            int len=0;
            while((len=fis.read())!=-1){
                System.out.println((char)len);
            }
            fis.close();
        }
    }

    一次读一个数组:

    public class Demo04 {
        public static void main(String[] args) throws IOException {
            FileInputStream fis=new FileInputStream("D:\test\s.txt");
            byte[]bytes=new byte[2];
            //一个字节数组的读
            int len=0;
            while((len=fis.read(bytes))!=-1){
                System.out.println(new String (bytes,0,len));
            }
            //释放资源
            fis.close();
        }
    }

    4  字节流复制文件

    public class Demo05 {
        public static void main(String[] args) throws IOException {
            //明确数据源
            FileInputStream fis=new FileInputStream("D:\test\s.txt");
            //明确目的地
            FileOutputStream fos=new FileOutputStream("D:\test\e\s.txt");
            //开始复制
            //数组缓冲
            byte[]bytes=new byte[1024];
            int len=0;
            while((len=fis.read(bytes))!=-1){
                fos.write(bytes,0,len);
            }
            //释放资源
            fis.close();
            fos.close();
        }    
    }
  • 相关阅读:
    (双指针 二分) leetcode 167. Two Sum II
    (双指针) leetcode 485. Max Consecutive Ones
    (双指针) leetcode 27. Remove Element
    (String) leetcode 67. Add Binary
    (数组) leetcode 66. Plus One
    (N叉树 BFS) leetcode429. N-ary Tree Level Order Traversal
    (N叉树 递归) leetcode 590. N-ary Tree Postorder Traversal
    (N叉树 递归) leetcode589. N-ary Tree Preorder Traversal
    (N叉树 DFS 递归 BFS) leetcode 559. Maximum Depth of N-ary Tree
    (BST 递归) leetcode98. Validate Binary Search Tree
  • 原文地址:https://www.cnblogs.com/quanjunkang/p/10655178.html
Copyright © 2011-2022 走看看