zoukankan      html  css  js  c++  java
  • java_IO_3

    Reader和Writer针对字符文件  对图片类文件可能就显得无能为力了  会损坏文件

    package ioStudy;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.io.Reader;
    
    /*
    1.创建源
    2.选择流
    3.操作
    4.释放资源
    */
    public class IOstudy5 {
        public static void main(String[] args) {
            File file = new File("test.txt");
            //针对字符文件
            Reader reader = null;
            try {
                reader = new FileReader(file);
                char[] buffer = new char[1024];
                int len;
                while ((len = reader.read(buffer)) != -1) {    //一次读取多个
                    String s = new String(buffer,0,len);
                    System.out.print(s);
                }
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }finally {
                if(reader!=null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }
    View Code
     1 package ioStudy;
     2 
     3 import java.io.File;
     4 import java.io.FileNotFoundException;
     5 import java.io.FileWriter;
     6 import java.io.IOException;
     7 import java.io.Writer;
     8 
     9 /*
    10 1.创建源
    11 2.选择流
    12 3.操作
    13 4.释放资源
    14 */
    15 public class IOstudy6 {
    16     public static void main(String[] args) {
    17         File file = new File("output.txt"); // 不存在会自动创建
    18         Writer writer = null;
    19 
    20         try {
    21             writer = new FileWriter(file,true);  //开启向后追加  不然每次都删掉原来的 再写  默认的是false
    22             String temp = "hello world!
    哈哈哈哈,测试中文可以用嘛";
    23 //            byte[] b = temp.getBytes();
    24             writer.write(temp);
    25             writer.flush();   //刷新内存
    26         } catch (FileNotFoundException e) {
    27             // TODO Auto-generated catch block
    28             e.printStackTrace();
    29         } catch (IOException e) {
    30             // TODO Auto-generated catch block
    31             e.printStackTrace();
    32         } finally {
    33             if (writer != null) {
    34                 try {
    35                     writer.close();
    36                 } catch (IOException e) {
    37                     // TODO Auto-generated catch block
    38                     e.printStackTrace();
    39                 }
    40             }
    41         }
    42     }
    43 }
    View Code
  • 相关阅读:
    html常用标签_new
    Nginx缓存
    购物车
    css的属性选择
    前端基础之css
    htm基础知识
    TypeScript(1)为什么需要TypeScript
    Electron
    Ant Design
    Umi
  • 原文地址:https://www.cnblogs.com/ustc-anmin/p/10946901.html
Copyright © 2011-2022 走看看