package com01;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//使用FileReader类、BufferedReader类、FileInputStream类三种方式
//从D盘下HelloWorld.java文件中读取全部信息并打印到控制台
//异常处理使用throw关键字对可能的异常进行处理,细化异常的类型
public class Shiyan5 {
public static void main(String[] args) throws IOException {
//第一种方式HelloWorld.java的内容:
try {
FileReader fr=new FileReader("D:/HelloWorld.java");//使用FileReader类
int i=0;
try {
System.out.println("第一种方式HelloWorld.java的内容:");
while((i=fr.read())!=-1){
System.out.print((char)i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// TODO Auto-generated method stub
System.out.println();
//第二种方式HelloWorld.java的内容:
BufferedReader br=new BufferedReader(new FileReader("D:/HelloWorld.java"));
String a=null;
try {
System.out.println("第二种方式HelloWorld.java的内容:");
while((a=br.readLine())!=null){
System.out.println(a);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//第三种方式HelloWorld.java的内容:
FileInputStream fis=new FileInputStream("D:/HelloWorld.java");
int i;
System.out.println("第三种方式HelloWorld.java的内容:");
while((i=fis.read())!=-1){
System.out.print((char)i);
}
fis.close();
}
}
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
//使用FileReader类、BufferedReader类、FileInputStream类三种方式
//从D盘下HelloWorld.java文件中读取全部信息并打印到控制台
//异常处理使用throw关键字对可能的异常进行处理,细化异常的类型
public class Shiyan5 {
public static void main(String[] args) throws IOException {
//第一种方式HelloWorld.java的内容:
try {
FileReader fr=new FileReader("D:/HelloWorld.java");//使用FileReader类
int i=0;
try {
System.out.println("第一种方式HelloWorld.java的内容:");
while((i=fr.read())!=-1){
System.out.print((char)i);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// TODO Auto-generated method stub
System.out.println();
//第二种方式HelloWorld.java的内容:
BufferedReader br=new BufferedReader(new FileReader("D:/HelloWorld.java"));
String a=null;
try {
System.out.println("第二种方式HelloWorld.java的内容:");
while((a=br.readLine())!=null){
System.out.println(a);
}
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//第三种方式HelloWorld.java的内容:
FileInputStream fis=new FileInputStream("D:/HelloWorld.java");
int i;
System.out.println("第三种方式HelloWorld.java的内容:");
while((i=fis.read())!=-1){
System.out.print((char)i);
}
fis.close();
}
}