zoukankan      html  css  js  c++  java
  • java IO流

     1 /*输出流必须执行flush(),不然写不进去*/
     2 import java.io.*;
     3 public class TestIO {
     4     public static void main(String[] args) throws Exception {
     5       /*单个字符的读取: int read();一个字符被拆成2个8字节,每个8字节占有int的低8位,
     6       作为一个整数返回。*/
     7         Reader r1 = new FileReader("C:/Users/well/Desktop/IO测试.txt");
     8         int b = 0;
     9         while((b = r1.read())!=-1) {
    10             System.out.print((char)b);
    11         }
    12         System.out.println();
    13         /*读取的字符暂存到字符数组中,返回本次读取字符的长度*/
    14       Reader r2 = new FileReader("C:/Users/well/Desktop/IO测试.txt");
    15         char[] ch = new char[5];
    16         int len;
    17         while((len = r2.read(ch))!=-1) {
    18             System.out.println(new String(ch,0,len));
    19         }
    20         /*写入一个字符串*/
    21         Writer w1 = new FileWriter("C:/Users/well/Desktop/新建.txt");
    22         w1.write("hello java");
    23         w1.flush();
    24         /*字符数组写入*/    /*true,不覆盖文档原来的内容*/
    25         Writer w2 = new FileWriter("C:/Users/well/Desktop/新建.txt",true);
    26         String str = new String("      aha 算法");
    27         char[] ch2 = str.toCharArray();//字符串转换成字符数组
    28         w2.write(ch2);
    29         w2.flush();
    30         /*单个字符写入*/
    31         Writer w3 = new FileWriter("C:/Users/well/Desktop/新建.txt",true);
    32         int k = 65;
    33         w3.write(k);
    34         w3.flush();
    35     }
    36 }
    View Code

     缓冲流

     1 /*输出流必须执行flush(),不然写不进去*/
     2 import java.io.*;
     3 public class TestIO {
     4     public static void main(String[] args) throws Exception {
     5         /*buffered类不仅提供缓冲机制,额外提供两种针对字符的读写方法
     6           (1)按行读取   String readLine();
     7           (2)写入时随时插入换行 newLine();
     8           */
     9       BufferedReader br = new BufferedReader(new FileReader("C:/Users/well/Desktop/IO测试.txt"));
    10         String str = null;
    11         while((str = br.readLine())!=null)
    12             System.out.println(str);
    13             
    14         BufferedWriter bw = new BufferedWriter(new FileWriter("C:/Users/well/Desktop/新建.txt"));
    15         bw.write("Jack Alibaba hangzhou");
    16         bw.newLine();
    17         bw.write("Pony tengxun shenzhen");
    18         bw.newLine();
    19         bw.write("BaoLuo baidu beijing");
    20         bw.newLine();
    21         bw.flush();
    22     }
    23 }
    View Code

     数据流

     InputStream和OutputStream

     1 import java.io.*;
     2 public class TestIO {
     3     public static void main(String[] args) throws Exception {
     4         InputStream is = new FileInputStream("C:/Users/well/Desktop/IO测试.txt");
     5         int a;
     6         while((a = is.read())!=-1)
     7         System.out.print((char)a);
     8         
     9       InputStream is1 = new FileInputStream("C:/Users/well/Desktop/IO测试.txt");
    10         byte[] bt = new byte[500];
    11         int b;
    12         while((b = is1.read(bt))!=-1) {
    13             System.out.print(new String(bt,0,b)); 
    14         }
    15         
    16         OutputStream os = new FileOutputStream("C:/Users/well/Desktop/IO测试.txt",true);
    17         os.write(65);
    18         os.flush();
    19         byte by = (byte)0x40;
    20         os.write(by);
    21         os.flush();
    22     }
    23 }
    View Code
  • 相关阅读:
    Cron表达式,springboot定时任务
    go 语言中windows Linux 交叉编译
    SSM框架处理跨域问题
    golang gin解决跨域访问
    关于Integer类的值使用==比较
    IoC注解
    spring基础知识
    SQL SERVER大话存储结构(3)_数据行的行结构
    SQL SERVER
    MySQL-记一次备份失败的排查过程
  • 原文地址:https://www.cnblogs.com/joyeehe/p/7873378.html
Copyright © 2011-2022 走看看