• 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();
    }
    }

    }

  • 相关阅读:
    事件
    dom对象
    逻辑运算和作用域的问题
    json
    数组
    字符串
    函数
    js的数据类型和全局方法
    js
    10.16 js内容
  • 原文地址:https://www.cnblogs.com/muliu/p/6510593.html
Copyright © 2020-2023  润新知