zoukankan      html  css  js  c++  java
  • 文件的输入输出

    OutputStream###

    public static void main(String[] args) throws IOException{
            FileOutputStream fos = new FileOutputStream("D:\1.txt");
                String s = "hello";
                fos.write(s.getBytes());
                
                fos.flush();
                fos.close();    
        }
    

    InputStream###

    public static void main(String[] args) throws IOException{
            FileInputStream fis = new FileInputStream("D:\src\1.txt");
            /*
            int ch;
            while((ch = fis.read()) != -1) {
                System.out.print((char)ch);
            }
            */
            //定义一个刚刚好的缓冲区,不用再循环了
            byte[] buf = new byte[fis.available()];
            fis.read(buf);
            System.out.println(new String(buf));
            fis.close();
        }
    

    Reader###

    public static void main(String[] args) throws IOException{
            //创建一个文件读取流对象,和指定名称的文件相关连
            //如果文件不存在,会发成异常FileNotFoundException
            FileReader fr = new FileReader("D:\src\1.txt");
            
            /*方法一
            //调用读取流对象的read方法
            //read() 一次读一个字符,而且会自动往下读
            
            int ch;
            while((ch = fr.read()) != -1) {
                System.out.print((char)ch);
            }
            */
            //方法二
            //定义字符数组,用于存储读到的字符
            //read(char[]) 返回的是读到字符个数
            char[] buf = new char[1024];
            int num;
            while((num = fr.read(buf)) != -1) {
                //把buf里面的字符打包成字符串输出
                System.out.print(new String(buf, 0, num));
            }
        }
    

    Writer###

    public static void main(String[] args) throws IOException {
    		//创建一个FileWriter对象。该对象一被初始化就必须要明确被操作的文件
    		//而且该文件就会被创建到指定的目录下。如果该目录下已有同名文件,将被覆盖
    		//明确数据存放的目的地
    		FileWriter fw = new FileWriter("D:\1.txt");
    		
    		//调用Writer方法,将字符串写入到流中
    		fw.Writer("Hello");
    
    		//刷新流对象中的缓冲数据,将数据刷到目的地中
    		fw.flush();
    		
    		//关闭资源
    		//和flush的区别:刷新缓冲并关闭资源,close刷新后,流会关闭
    		fw.close();
    	}
    

    对已有文件的续写###

    public static void main(String[] args) throws IOException{
            FileWriter fw = new FileWriter("D:\1.txt", true);
            
            fw.write("
    world!");    // 
     换行续写
            fw.close();
        }
    

    文件的拷贝###

    public static void main(String[] args) throws IOException{
            //定义读取流和文件相关联
            FileReader fr = new FileReader("D:\1.txt");
            //创建目的地
            FileWriter fw = new FileWriter("D:\2.txt");
            char[] buf = new char[1024];
            int len;
            //通过不断读写来拷贝数据
            while((len = fr.read(buf)) != -1) {
                fw.write(buf, 0, len);
            }
            fw.close();
            fr.close();
        }
    

    IO异常的处理###

    public static void main(String[] args) {
            FileWriter fw = null;
            try {
                fw = new FileWriter("D:\1.txt");
                fw.write("Hello");
            } catch (IOException e) {
                System.out.println("User: "+e.toString());
            } finally {
                try {
                    if(fw != null) {
                        fw.close();
                    } 
                } catch (IOException e) {
                        System.out.println("User: "+e.toString());
                }
            }
        }
    
  • 相关阅读:
    敏捷开发设计模式的五大原则(读敏捷软件开发:原则、模式与实践笔记记录一下)
    使用AOP和Semaphore对项目中具体的某一个接口进行限流
    java正则使用全记录!
    推荐一本书学习springcloud书籍的SpringCloud微服务全栈技术与案例解析
    使用springboot Admin 2.0.6版本 集成监控springcloud微服务应用
    推荐一本学习Groovy的书籍Groovy程序设计!
    Datagrip 快捷键和常用插件持续更新一集一些使用技巧
    Eclipse中项目不会自动编译问题的坑和注意点
    使用Groovy的mixin方法注入,和mixedIn属性实现过滤链
    vscode常用快捷键和插件(持续更新),以及一些常用设置的坑和技巧
  • 原文地址:https://www.cnblogs.com/rancvl/p/5439033.html
Copyright © 2011-2022 走看看