Rhythmk 一步一步学 JAVA (19) JAVA IO 文件常用操作
2014-04-20 19:18
549 查看
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")); } }
如果迷茫,脚下的路不知道怎么走是好的时候,不要浪费时间在路口徘徊,凭感觉选择一条路走下去,。
相关文章推荐
- java.io.File操作文件的一些常用方法
- java.io.file 的常用文件操作
- JavaIO 文件递归遍历及常用File类操作
- 学习之使用Java IO 来实现复制文件的操作
- java io 文件操作
- java.io.File文件操作容易失误的两点--mkdirs()、删除目录
- Java io文件操作(按字符、字节、行、随机读取,追加,文件操作)
- java中关于文件操作常用工具类
- java IO 系统-java 文件操作大全
- Java 本地文件常用操作
- java_IO_文件的基本操作
- 【JavaWeb-19】Hibernate简介、入门案例、Session操作、2个配置文件详解
- java文件读写常用操作
- java.io.file 各种文件操作
- 使用java.io.*操作文件的拆分与合并,Application一例!
- java操作文件常用的 IO流对象
- Java IO对文件的操作
- java文件常用操作(2) ---- 从文件末尾开始读取文件
- java的io操作(将字符串写入到txt文件中)
- java IO文件操作入门经典例子