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"));
} }

如果迷茫,脚下的路不知道怎么走是好的时候,不要浪费时间在路口徘徊,凭感觉选择一条路走下去,。

最新文章

  1. grails项目获取前后台的值
  2. hdu3294 girl‘s research
  3. C语言之数组,字符串,指针
  4. Android Activity模拟dialog
  5. 04_IOC容器装配Bean(xml方式)
  6. 148. Sort List -- 时间复杂度O(n log n)
  7. POJ 2255 Tree Recovery 树的遍历,分治 难度:0
  8. 在解决方案中所使用 NuGet 管理软件包依赖
  9. 零基础学习Linux(三)linux与windows文件共享
  10. CSS学习笔记——定位position属性的学习
  11. WPF WebBroswer可以用到的接口
  12. win7安装ruby on rails
  13. C# 解析User-Agent工具
  14. git的初步使用---本地代码库的使用和提交
  15. JavaScript中return的用法详解
  16. Dede CMS如何在文章中增加“附件下载”操作说明
  17. Oracle execute and call
  18. 4步解决“Microsoft Office Professional Plus 2013在安装过程中出错”
  19. laravel 错误提示Fatal Error: Class 'Pheanstalk\Pheanstalk' not found
  20. Cpython解释器下实现并发编程

热门文章

  1. Android Studio真机测试失败-----''No target device found" (转)
  2. php simple_html_dom
  3. Jmeter图形结果
  4. SSZipArchive使用详解
  5. 【DUBBO】 Dubbo原理解析-Dubbo内核实现之基于SPI思想Dubbo内核实现
  6. 几张 ejabberd 架构部署图
  7. 人人都要培养AIQ
  8. bzoj1588[HNOI2002]营业额统计——双向链表
  9. Oracle修改字段值包含&字符
  10. Ubuntu secuerCRT连接失败,The remote system refused the connection.