public class Test_FileInputStream {
// 1.从硬盘存在的一个文件中,读取其内容到程序中,使用FileInputStream
// 2.创建一个file的对象
@Test
public void Test1() throws IOException{
// 1.创建一个File类的对象
File file = new File("C:\\Users\\Administrator\\Desktop\\heihei.txt");
// 2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
/*
* read():读取文件的一个字节
*/
int b = fis.read();
while(b != -1){
System.out.print(b);
b = fis.read();
}
// 4.关闭相应的流
fis.close();
}
@Test
public void Test2(){
FileInputStream fis = null;
try{
File file = new File("C:\\Users\\Administrator\\Desktop\\heihei.txt");
int b = fis.read();
while(b != -1){
System.out.print(b);
b = fis.read();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
// 4.关闭相应的流
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
// 1.从硬盘存在的一个文件中,读取其内容到程序中,使用FileInputStream
// 2.创建一个file的对象
@Test
public void Test1() throws IOException{
// 1.创建一个File类的对象
File file = new File("C:\\Users\\Administrator\\Desktop\\heihei.txt");
// 2.创建一个FileInputStream类的对象
FileInputStream fis = new FileInputStream(file);
// 3.调用FileInputStream的方法,实现file文件的读取
/*
* read():读取文件的一个字节
*/
int b = fis.read();
while(b != -1){
System.out.print(b);
b = fis.read();
}
// 4.关闭相应的流
fis.close();
}
@Test
public void Test2(){
FileInputStream fis = null;
try{
File file = new File("C:\\Users\\Administrator\\Desktop\\heihei.txt");
int b = fis.read();
while(b != -1){
System.out.print(b);
b = fis.read();
}
}catch(IOException e){
e.printStackTrace();
}finally{
try{
// 4.关闭相应的流
fis.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}