zoukankan      html  css  js  c++  java
  • JAVA,字节流文件读写

    写入文件:

    package com.java.day28OutputStream;
    
    import java.io.FileOutputStream;
    import java.io.IOException;
    
    public class OutputStream01 {
        public static void main(String[] args){
            output();
        }
    
        public static void output(){
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream("D:\IdeaProjects\StudyJava\day01\src\com\java\day28OutputStream\a.txt");
                String str = "Hello World";
                // todo getBytes方法将字符串转为byte数组
                fos.write(str.getBytes());
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                if (fos !=null){
                    try {
                        fos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    
        }
    }

    运行效果:

     读取文件:

    package com.java.day29InputStream;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    public class InputStream {
        public static void main(String[] args) {
            input();
        }
    
        public static void input() {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream("D:\IdeaProjects\StudyJava\day01\src\com\java\day28OutputStream\a.txt");
                int len = 0;
                while ((len = fis.read()) !=-1){
                    System.out.print((char) len);
                }
            }catch (IOException e){
                e.printStackTrace();
            }finally {
                if (fis != null) {
                    try {
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    运行效果:

  • 相关阅读:
    centos部署bladex boot 之docker安装
    git ssh key创建和github使用
    Debian root登录设置
    Linux软件源
    Secure backup
    Python简易web服务
    好久没有更新博客了
    Python实现无向图最短路径
    DWZ使用中遇到的坑
    tronado学习
  • 原文地址:https://www.cnblogs.com/xiamaojjie/p/15067866.html
Copyright © 2011-2022 走看看