1.文件读取的两种方式package IO;
import java.io.FileReader;
import java.io.IOException;
//文件读取的两种方式
public class FileReaderDemo {
public static void main(String[] args) {
// 通过变量len中转站
// read_File1();
// 通过字符数组进行读取
read_File2();
}
private static void read_File2() {
FileReader fr = null;
try {
fr = new FileReader("demo1.txt");
int len = 0;
char[] ch = new char[1024];
while((len=fr.read(ch))!=-1){
String s = new String(ch,0,len);
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void read_File1() {
FileReader fr = null;
try {
fr = new FileReader("demo1.txt");
int len = 0;
while((len=fr.read())!=-1){
System.out.print((char)len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import java.io.FileReader;
import java.io.IOException;
//文件读取的两种方式
public class FileReaderDemo {
public static void main(String[] args) {
// 通过变量len中转站
// read_File1();
// 通过字符数组进行读取
read_File2();
}
private static void read_File2() {
FileReader fr = null;
try {
fr = new FileReader("demo1.txt");
int len = 0;
char[] ch = new char[1024];
while((len=fr.read(ch))!=-1){
String s = new String(ch,0,len);
System.out.println(s);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
if(fr!=null)
try {
fr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
private static void read_File1() {
FileReader fr = null;
try {
fr = new FileReader("demo1.txt");
int len = 0;
while((len=fr.read())!=-1){
System.out.print((char)len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}