Stream 2

package demo;

import java.io.*;

public class TestStream2{
public void testWrite()throws Exception{
//如果haha.txt文件还没有,则FileOutputStream会自动创建该文件
FileOutputStream fos = new FileOutputStream("haha.txt");
String str = "hello adam";
fos.write(str.getBytes());
fos.close();
}

public void testRead()throws Exception{
FileInputStream fis = new FileInputStream("haha.txt");
int offset = 0;
while((offset = fis.read()) != -1){
System.out.println((char)offset+"---还剩余:"+fis.available()+"个字节没读");
}
}

public static void main(String[] args)throws Exception{
TestStream2 t = new TestStream2();
t.testWrite();//先写入内容到文件
t.testRead();//再从文件里边读取内容出来
}
}

你可能感兴趣的:(Stream)