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

    //字节流:

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;

    import org.junit.Test;

    public class IOStreamTest {

    //fileInputStreamTest
    @Test
    public void fileInputStreamTest(){

    File file = new File("aa.txt");
    if (!file.exists()){
    try {
    file.createNewFile();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    try {
    FileOutputStream fos = new FileOutputStream("aa.txt");
    String b1 = "aa222";
    byte[] b = b1.getBytes();
    fos.write(b);
    FileInputStream fis = new FileInputStream("aa.txt");
    byte[] by = new byte[(int)file.length()];//file.length()=5
    int t;
    int count =0;
    //fis.read(by);//按照文件长度读取字节
    //System.out.println(new String(by));//aa222
    while((t=fis.read()) != -1 ){//读到文件末尾返回-1
    by[count++] = (byte)t;
    }
    System.out.println(new String(by));
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    }

  • 相关阅读:
    225. 用队列实现栈
    415. 字符串相加
    rabbitmq的基本使用
    3. 无重复字符的最长子串
    面试题59
    面试题30. 包含min函数的栈
    面试题09. 用两个栈实现队列
    287. 寻找重复数
    1137. 第 N 个泰波那契数
    70. 爬楼梯
  • 原文地址:https://www.cnblogs.com/muliu/p/6510593.html
Copyright © 2011-2022 走看看