zoukankan      html  css  js  c++  java
  • 课堂所讲整理:输入输出流(I/O)2(修改版)

     1 package org.hanqi.ex;
     2 
     3 import java.io.*;
     4 
     5 public class TestFile2 {
     6 
     7     public static void main(String[] args) {
     8         
     9         try {
    10             //覆盖写入
    11             //true 追加写入
    12             FileOutputStream fos = new FileOutputStream("d:\test.txt",true);
    13             String str = "
    心情不错";
    14             fos.write(str.getBytes());            
    15             fos.close();
    16             
    17             FileInputStream fis = new FileInputStream("d:\test.txt");
    18             byte[] b = new byte[200];
    19             int i = fis.read(b);
    20             String str1 = new String(b,0,i);
    21             System.out.println("读取内容:"+str1);
    22             fis.close();
    23         } catch (Exception e) {
    24             // TODO 自动生成的 catch 块
    25             e.printStackTrace();
    26         }
    27     }
    28 }
     1 package org.hanqi.ex;
     2 
     3 import java.io.*;
     4 
     5 public class TestFile3 {
     6 
     7     public static void main(String[] args) {
     8         
     9         try {
    10             //读取
    11             FileReader fr = new FileReader("d:\test.txt");
    12             char[] c = new char[200];
    13             int i = fr.read(c);
    14             String str = new String(c,0,i);
    15             System.out.println("读取内容:"+str);
    16             fr.close();
    17             
    18             //写入
    19             FileWriter fw = new FileWriter("d:\test.txt", true);
    20             fw.write("
    新追加的内容");
    21             fw.close();
    22         } catch (Exception e) {
    23             // TODO 自动生成的 catch 块
    24             e.printStackTrace();
    25         }
    26     }
    27 }
     1 package org.hanqi.ex;
     2 
     3 import java.io.*;
     4 
     5 public class TestFile4 {
     6 
     7     public static void main(String[] args) {
     8                 
     9         try {
    10             File f = new File("d:\test.txt");
    11             //带缓存
    12             //Writer接口的实现类
    13             FileWriter fw = new FileWriter(f);
    14             //缓存写入类,构造时需要传入Writer实例
    15             BufferedWriter bw = new BufferedWriter(fw);
    16             bw.write("
    这是清空前缓存方式写入的字符串");
    17             //自动管理缓存:
    18             //自动写入:1.缓存满了 2.缓存关闭之前
    19             bw.flush();  //主动清空缓存,写入数据
    20             
    21             bw.write("
    这是清空后缓存方式写入的字符串");
    22             
    23             bw.close();  //注意关闭顺序
    24             fw.close();
    25             System.out.println("写入完成");
    26             //缓存读
    27             FileReader fr = new FileReader(f);
    28             BufferedReader br = new BufferedReader(fr);
    29 //            //第一次读
    30 //            String str = br.readLine();
    31 //            
    32 //            while(str!=null)
    33 //            {
    34 //                System.out.println(str);
    35 //                 str = br.readLine();                
    36 //            }
    37             while(true)
    38             {
    39                 String str = br.readLine();
    40                 if(str==null)
    41                 {
    42                     break;                    
    43                 }
    44                 System.out.println(str);
    45             }
    46             
    47         } catch (Exception e) {
    48             // TODO 自动生成的 catch 块
    49             e.printStackTrace();
    50         }
    51     }
    52 }

     附相关思维导图:

  • 相关阅读:
    41.给你一个未排序的整数数组,请你找出其中没有出现的最小的正整数。
    Java反射学习记录
    LeetCode算法笔记-回溯法
    LeetCode算法笔记(二)
    LeetCode算法笔记(一)
    JDBC学习笔记--通用的查询方法
    JDBC学习笔记--ResultSetMetaData
    JDBC学习笔记--PreparedStatement
    Java学习笔记---字符串
    Java学习笔记---通过异常处理错误
  • 原文地址:https://www.cnblogs.com/hanazawalove/p/5276755.html
Copyright © 2011-2022 走看看