zoukankan      html  css  js  c++  java
  • 字节输出输入流

      在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候使用输入流读取数据
    当程序需要将一些数据保存起来的时候,就需要使用输出完成。
    InputStream和OutpuStream两个是为字节流设计的,用来处理自己流和二进制对象
    Reader和Writer为字符流(一个字符占两个字节),主要用来处理字符或字符串

     1 package IO;
     2 
     3 import java.io.File;
     4 import java.io.FileInputStream;
     5 import java.io.FileOutputStream;
     6 import java.io.IOException;
     7 import java.io.InputStream;
     8 import java.io.OutputStream;
     9 
    10 public class Demo {
    11     public static void main(String[] args) throws IOException {
    12         // 1、通过file找到一个文件
    13         File file = new File("d:\java\java\temp.txt");
    14         // 2、实例化OutputStream对象
    15         OutputStream out = new FileOutputStream(file, true);//true表示追加
    16         // 3、写出要输出的内容
    17         String info = "helloworld";
    18         // 4、将要输出的字符串变成字节数组(因为是字节流要变成字节数组)
    19         byte b[] = info.getBytes();
    20         // 5、输出内容并关闭流
    21         out.write(b);
    22         out.close();
    23 
    24         
    25         // 1、通过file找到一个文件
    26         File file2 = new File("d:\java\java\temp.txt");
    27         // 2、实例化InputStream对象
    28         InputStream input = new FileInputStream(file2);
    29         // 3、开辟一个数组空间
    30         byte b2[] = new byte[1024];
    31         // 4、读取内容,读取的是长度
    32         int result = input.read(b2);
    33         // 5、输出长度,将长度转换成字符串
    34         System.out.println(new String(b2));
    35         
    36         
    37 
    38     }
    39 }
  • 相关阅读:
    Spring读取properties内容
    SpringBoot全局异常处理
    Hibernate入门
    Oracle查询表及注释
    MySQL重复与不重复问题
    IDEA中other settings不见了
    01程序员修炼之道
    团队冲刺(四)
    单词字母查询频率
    学习进度(9)
  • 原文地址:https://www.cnblogs.com/lanyy/p/8652254.html
Copyright © 2011-2022 走看看