主要包括以下操作:
文件写入读取文件内容删除文件将文件内容复制到另一个文件向文件中追加数据创建临时文件修改文件最后的修改日期获取文件大小文件重命名设置文件只读检测文件是否存在在指定目录中创建文件获取文件修改时间创建文件文件路径比较实例如下:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.Date; /** * @author Amy E-mail:chuxiumin03@126.com * @date 创建时间:2017年4月12日 下午9:04:55 * @version 1.0 * @parameter * @return */ public class FileTest { /** * @ClassName: FileTest * @Description: TODO(文件的各种操作) * @author: Amy_Robot * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws IOException * @date: 2017年4月12日 下午9:04:55 */ public static void copy_file1_to_file2(String file1,String file2) throws UnsupportedEncodingException, FileNotFoundException { //将file1的内容复制到file2 BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(file1)), "utf-8")); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File(file2)), "utf-8")); String str; try { while ((str=br.readLine())!=null) { //读取file1的一行,写入file2中的一行 bw.write(str); bw.newLine(); /*向file2中追加数据*/ // bw.append(str); } bw.close(); br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //在指定目录下创建文件 public static File creatFile(String prefix,String suffix, File dir) { File file = null; try { file = File.createTempFile(prefix, suffix, dir); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(file.getPath()); return file; } public static void main(String[] args) throws IOException { File file = new File("e:\\chinese.txt"); //在e盘创建以auto为前缀,.txt为后缀的临时文件 File file2 = new File("e:\\"); File.createTempFile("auto",".txt", file2); //获取文件的修改时间 Date date = new Date(file.lastModified()); System.out.println(date); if (file.exists()) { //测试文件是否已存在 System.out.println("文件已存在"); file.delete(); System.out.println("文件已删除"); }else { System.out.println("文件不存在"); System.out.println("创建新文件"); file.createNewFile(); System.out.println("文件已创建"); String name = file.getName(); System.out.println(name); FileWriter fw = new FileWriter("2.txt"); BufferedWriter bw= new BufferedWriter(fw); System.out.println("文件内容写入————"); bw.write("中国人"); bw.close(); try { FileReader in = new FileReader("2.txt"); BufferedReader br = new BufferedReader(in); String str; System.out.println("文件内容读出————"); while ((str=br.readLine())!=null) { System.out.println(str); } br.close(); in.close(); } catch (Exception e) { e.printStackTrace();// TODO: handle exception } } copy_file1_to_file2("e:\\chinese_stopword.dic", "e:\\chinese.txt"); //文件重命名,限制:文件要在相同的目录中 System.out.println(file.renameTo(new File("e:\\java.txt"))); if (file.renameTo(new File("e:\\java.txt"))) { System.out.println(file.getName()); } file.setReadOnly();//设置文件为只读类型。 //文件目录比较,返回0,正数,负数 int s =file.compareTo(file2); System.out.println(s); } }