package com.rhythmk.filedemo; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.text.DateFormat; import java.text.FieldPosition; import java.text.ParsePosition; import java.text.SimpleDateFormat; import java.util.Properties; import java.util.UUID; import org.junit.Test; public class file_demo1 { public String getPath() { return System.getProperty("user.dir"); } @Test public void 获取系统路径() { String path = System.getProperty("user.dir"); System.out.println(path); } @Test public void 写入文件() throws IOException { String filePath = getPath() + "\\a.txt"; File file = new File(filePath); if (file.canWrite()) { System.out.println("可写"); } FileWriter writer = new FileWriter(file, true); // 写入时间 SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); writer.write("-------" + time.format(new java.util.Date()) + "\r\n"); // 写入随机GUID writer.write(UUID.randomUUID().toString() + "\r\n"); writer.flush(); writer.close(); System.out.println("写入文件成功路径:" + filePath); } @Test public void 读取文件() throws IOException { String filePath = getPath() + "\\a.txt"; BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(filePath), "UTF-8")); String txt = null; while ((txt = reader.readLine()) != null) { System.out.println(txt); } } @Test public void 遍历文件夹() throws IOException { String filePath = getPath() + "\\src"; ReadFile(filePath); } private void ReadFile(String path) throws IOException { File file = new File(path); if (file.isDirectory()) { System.out.println("当前目录地址为:" + path + "\r\n"); File[] filelist = file.listFiles(); for (File f : filelist) { ReadFile(f.getAbsolutePath()); } } else { System.out.println("------当前文件地址为:" + path + "\r\n"); BufferedReader reader = new BufferedReader(new InputStreamReader( new FileInputStream(path), "UTF-8")); String txt = null; while ((txt = reader.readLine()) != null) { System.out.println(txt); } } } @Test public void 删除文件() { String filePath = getPath() + "\\12.txt"; File file = new File(filePath); file.deleteOnExit(); } @Test public void 获取文件信息() { String filePath = getPath() + "\\a.txt"; File file = new File(filePath); System.out.println("是否可读(canRead):" + file.canRead()); System.out.println("当前文件路径(getAbsolutePath):" + file.getAbsolutePath()); System.out.println("文件名称:" + file.getName()); System.out.println("文件大小:" + file.length()); System.out.println("文件是否存在:" + file.exists()); String fileNAME = file.getName(); System.out.println("后缀名:" + fileNAME.substring(fileNAME.lastIndexOf(".") + 1)); } @Test public void 重命名() { String filePath = getPath() + "\\a.txt"; File file = new File(filePath); File file2 = new File(getPath() + "\\aaa.txt"); file.renameTo(file2); System.out.println("文件是否存在:" + file2.exists()); } @Test public void 读取属性文件() { String filePath = getPath() + "\\src\\app.properties"; Properties pro = new Properties(); try { InputStream input = new FileInputStream(new File(filePath)); pro.load(input); input.close(); } catch (Exception e) { // TODO: handle exception } System.out.println(pro.getProperty("rhythmk")); } }
如果迷茫,脚下的路不知道怎么走是好的时候,不要浪费时间在路口徘徊,凭感觉选择一条路走下去,。