简介:如题
import java.io.FileInputStream;
/**
* @author czchina
*
*/
public class TestStream {
public static void main(String[] args) {
// TODO Auto-generated method stub
//声明输入流的引用
FileInputStream fls = null;
//声明输出流的引用
FileOutputStream fos =null;
try{
//一、生成代表输入流的对象
fls = new FileInputStream("E:/Android/AndroidStudioProjects/text.txt");
//生成一个字节数组
byte [] buffer= new byte [100];
//调用输入流对象的read方法,读取数据(将读出来的长度为<buffer.length-5>的数据放入buffer数组中,5是开始存放的位置)
//注意读出来的数据长度不要超过数组长度。
int num = fls.read(buffer,5,buffer.length-5);
System.out.println("1、从输入流读出的字节数:\n"+num);//看看从输入流中读取了多少数据
String s = new String(buffer);
System.out.println("2、buffer to string, and print: \n"+s);
//调用一个String对象的trim方法,会去掉字符串的首尾空格,测试如下
s = s.trim();
System.out.println("3、String对象调用其trim方法后,print:\n"+s);
}
catch(Exception e){
System.out.println(e.toString());
}
}
}
text.txt
console: