zoukankan      html  css  js  c++  java
  • 字节流 文件字节流 缓冲字节流

    图片为二进制文件,采用字节流调取,实现图片的拷贝;

    package io;
    
    /**
     * file类的基本用法
     */
    import java.io.*;
    
    public class test {
        public static void main(String[] args) {
            FileInputStream fis = null;// 输入流
            FileOutputStream fos = null;// 输出流
            try {
                fis = new FileInputStream("D:\txt\qq.jpg");
                fos = new FileOutputStream("D:\txt\hello\qq.jpg");
                byte buf[] = new byte[1024];
                int n = 0;
                while ((n = fis.read(buf)) != -1) {
                    fos.write(buf);
                }
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    View Code

    文件字符流,FileReader  Filewrite

    读取一个文件并写入另一个文件中,char转

    package io;
    
    /**
     * file类的基本用法
     */
    import java.io.*;
    
    public class test {
        public static void main(String[] args) {
            FileReader fis = null;// 输入流
            FileWriter fos = null;// 输出流
            try {
                fis = new FileReader("D:\txt\test1.txt");
                fos = new FileWriter("D:\txt\hello\test1.txt");
                char buf[] = new char[1024];
                int n = 0;
                while ((n = fis.read(buf)) != -1) {
                    String s = new String(buf);
                    System.out.println(s);
                    fos.write(buf);
                }
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                try {
                    fis.close();
                    fos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    View Code

    缓冲字符流 BufferedReader BufferWriter

    package io;
    
    /**
     * file类的基本用法
     */
    import java.io.*;
    
    public class test {
        public static void main(String[] args) {
            BufferedReader br = null;// 输入流
            BufferedWriter bw = null;// 输出流
            try {
                FileReader fr = new FileReader("D:\txt\test1.txt");
                br = new BufferedReader(fr);
                FileWriter fw = new FileWriter("D:\txt\hello\test2.txt");
                bw = new BufferedWriter(fw);
                String s = "";
                while ((s = br.readLine()) != null) {
                    System.out.println(s);
                    bw.write(s + "
    ");
                }
    
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            } finally {
                try {
                    br.close();
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }
    View Code
  • 相关阅读:
    数学思想方法-python计算战(8)-机器视觉-二值化
    04-05组合问题_算法训练
    至HDFS附加内容
    HDU 1010 Tempter of the Bone heuristic 修剪
    二叉树3种遍历的非递归算法
    [Ramda] R.project -- Select a Subset of Properties from a Collection of Objects in Ramda
    [Ramda] Refactor a Promise Chain to Function Composition using Ramda
    [SVG] Combine Multiple SVGs into an SVG Sprite
    [Ramda] Difference between R.converge and R.useWith
    [Ramda] Refactor to a Point Free Function with Ramda's useWith Function
  • 原文地址:https://www.cnblogs.com/helloworld2019/p/10835104.html
Copyright © 2011-2022 走看看