import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demofile2 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
method();
method2();
}
//一次一个字节
public static void method() throws IOException {
FileInputStream fis = new FileInputStream("dema.txt");
//输入数据
//定义变量,临时记录读取的这个字节
int c;
while((c=fis.read())!=-1){
System.out.println(c);
}
//关闭流
fis.close();
}
//一次一个字节数组
public static void method2() throws IOException{
//创建流对象
FileInputStream fis = new FileInputStream("riven.txt");
//输入数据
byte [] bytes = new byte[1024];
//定义变量,临时记录读取到的字节的个数
int len;
while((len= fis.read(bytes))!=-1){
String s = new String(bytes ,0 ,len);
System.out.println(s);
}
fis.close();
}
}
|
|