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

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

Rhythmk 一步一步学 JAVA (19) JAVA IO 文件常用操作的更多相关文章

  1. Java 字符流实现文件读写操作(FileReader-FileWriter)

    Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...

  2. Java - 19 Java 异常处理

    Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用 ...

  3. java中的File文件读写操作

    之前有好几次碰到文件操作方面的问题,大都由于时间太赶而没有好好花时间去细致的研究研究.每次都是在百度或者博客或者论坛里面參照着大牛们写的步骤照搬过来,之后再次碰到又忘记了.刚好今天比較清闲.于是就在网 ...

  4. Java学习之==>IO文件操作体系

    一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...

  5. JAVA对数字证书的常用操作(转载)

    一:需要包含的包 import java.security. * ; import java.io. * ; import java.util. * ; import java.security. * ...

  6. Java篇-File类之常用操作

    /** * */ package com.io.file; import java.io.File; import java.io.IOException; /** * <pre> * & ...

  7. JAVA基于缓冲的文件读写操作

    File f2 = new File("e://index.java"); BufferedReader reader = new BufferedReader(new Input ...

  8. java学习之IO文件分割

    package om.gh.homework; import java.io.*; /** * 实现分割文件; * @param file */ public class HomeWork { /** ...

  9. .Net转Java.07.IDEA和VS常用操作、快捷键对照表

      功能 IDEA 2017.1 快捷键   Visual Studio 2015 快捷键 文档 格式化整个文档 Ctrl+Alt+L   Ctrl+E,D 或者 Ctrl+K,D  文件 显示最近的 ...

随机推荐

  1. js之自定义右键菜单

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  2. 正确的使用margin:0 auto与body{text-align:center;}实现元素居中(转)

    body{text-align:center}与margin:0 auto的异同? text-align是用于设置或对象中文本的对齐方式.一般情况下我们设置文本对齐方式的时候需要用此属性进行设置 我们 ...

  3. get、post接口测试-java

    public class HttpClient { //get请求方法 public String sendGet(String url, String data) { Date date = new ...

  4. MAC OS环境下搭建基于Python语言的Selenium2自动化测试环境

    #1安装Python Mac OS上自带python2.7,在此介绍安装python3.x版本 去官网下载Python for MAC版本 https://www.python.org 安装文件为pk ...

  5. windows中查看端口被什么应用程序占用并删除

    windows中查看端口的命令是netstat,具体用法如下: 查看端口信息时可以使用如下命令: netstat -ano 运行结果如下: 当前我的本地13067端口被占用,使用命令如下: c:\&g ...

  6. PHP的网站主要攻击方式有哪些

    1.命令注入(Command Injection) 2.eval注入(Eval Injection) 3.客户端脚本攻击(Script Insertion) 4.跨网站脚本攻击(Cross Site ...

  7. 【DUBBO】dubbo的registry配置

    [一]:配置项 注册中心地址:zookeeper://ip:端口 <dubbo:registry address="注册中心的地址" check="启动时检查注册中 ...

  8. flannel vxlan 实现原理【转】

    flannel是coreos为kubernets提供的网络解决方案,主要为打通跨节点的容器通信,其中vxlan模式为flannel实现的一种后端模式,其他模式还包括udp, host-gw等,可以通过 ...

  9. java入门很简单之各种循环

    1.if结构的语法: <1> 简单的if :if (条件){ //代码块                                                           ...

  10. Unit01: Web概述 、 HTML概述 、 文本处理 、 图像和超链接 、 表格 、 表单

    Unit01: Web概述 . HTML概述 . 文本处理 . 图像和超链接 . 表格 . 表单 demo1.html <!-- 声明网页的版本(文档类型) --> <!doctyp ...