java简单txt文件读取方法。
public static void readFile(String address,String filename) {
byte[] t=new byte[2500];
try{
File file=new File(address,filename);
FileInputStream readFile=new FileInputStream(file);
while(readFile.read(t, 0, 100)!=-1){
String s=new String(t,0,100);
System.out.println(s);
}
}
catch(IOException e) {
e.printStackTrace();
}
//以下是使用BufferedInputStream实现的:
public static void readFile2(String address,String name) {
byte[] data=new byte[1];
try{
File file=new File(address,name);
FileInputStream fis=new FileInputStream(file);
BufferedInputStream bis=new BufferedInputStream(fis);
while(bis.read(data)!=-1) {
String s=new String(data,0,1);
System.out.print(s);
}
}
catch(IOException e){
e.printStackTrace();
}
catch(ArrayIndexOutOfBoundsException e) {
System.out.println(“using: java onlyfun caterpillar “);
e.printStackTrace();
}
}