//示例1,file基本操作
import java.io.File;
import java.io.IOException;
class FileDemo{
public static void main(String args[]){
try{
File file=new File("file1.txt");
System.out.println("文件是否存在: "+file.exists());
System.out.println("是文件吗: "+file.isFile());
System.out.println("是文件夹吗: "+file.isDirectory());
System.out.println("是否隐藏: "+file.isHidden());
System.out.println("名称: "+file.getName());
System.out.println("标准文件名: "+file.getCanonicalFile());
System.out.println("相对路径: "+file.getPath());
System.out.println("绝对路径: "+file.getAbsolutePath());
System.out.println("标准路径: "+file.getCanonicalPath());
System.out.println("最后修改时间: "+file.lastModified());
System.out.println("文件大小: "+file.length()+" 字节");
}
catch(IOException ex){
ex.printStackTrace();
}
}
}
//示例2,简单读写操作
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.IOException;
class BufferedReaderAndBufferedWriterTest{
public static void main(String args[]){
try{
FileWriter fw=new FileWriter("file1.txt");
BufferedWriter bw=new BufferedWriter(fw);
bw.write("大家好,我正在学习Java");
bw.newLine();
bw.write("请多多指教");
System.out.println("file1.txt写入成功!***************开始读..\n");
bw.flush();
bw.close();
FileReader fr=new FileReader("file1.txt");
BufferedReader br=new BufferedReader(fr);
String temp=null;
do{
temp=br.readLine();
System.out.println(temp==null?"":temp);
}
while(temp!=null);
fr.close();
br.close();
System.out.println("file1.txt已经读完!***************");
}
catch(IOException ex){
ex.printStackTrace();
}
}
}