JAVA读取文件的方法
import java.io.*;
import java.util.Scanner;
import java.util.regex.Pattern;
public class Suru {
public static void main(String[] args) throws IOException {
File file = new File("g:/book" + File.separator + "OUTPU" + File.separator + "wend.txt");
Pattern pat = Pattern.compile("\\d{4}-\\d{2}-\\d{2}");
Scanner scan= new Scanner(file);
scan.useDelimiter("\n");
System.out.println("输入你的年龄:");
if(scan.hasNextInt()){
System.out.println("你的年龄是:"+scan.nextInt());
}else if(scan.hasNext(pat)) {
System.out.println("你的生日是:"+scan.next());
}else
{
System.out.println("你输入的内容是:"+scan.next()+"\n");
}
scan.close();
{
BufferedReader rea = new BufferedReader(new FileReader(file));
String mai = rea.readLine();
System.out.println("缓冲输入流:" + mai + "\n");
rea.close();
}
{
InputStream read1= new FileInputStream(file);
byte data[]=new byte[1024];
int len= read1.read(data);
System.out.println("字节输入流:"+new String(data,0,len)+"\n");
read1.close();
}
Reader er= new FileReader(file);
char dat[] =new char[1024];
int chang=er.read(dat);
System.out.println("字符输入流:"+new String(dat,0,chang));
er.close();
}
}