Rhythmk 一步一步学 JAVA (19) JAVA IO 文件常用操作
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 文件常用操作的更多相关文章
- Java 字符流实现文件读写操作(FileReader-FileWriter)
Java 字符流实现文件读写操作(FileReader-FileWriter) 备注:字符流效率高,但是没有字节流底层 字节流地址:http://pengyan5945.iteye.com/blog/ ...
- Java - 19 Java 异常处理
Java 异常处理 异常是程序中的一些错误,但并不是所有的错误都是异常,并且错误有时候是可以避免的. 比如说,你的代码少了一个分号,那么运行出来结果是提示是错误java.lang.Error:如果你用 ...
- java中的File文件读写操作
之前有好几次碰到文件操作方面的问题,大都由于时间太赶而没有好好花时间去细致的研究研究.每次都是在百度或者博客或者论坛里面參照着大牛们写的步骤照搬过来,之后再次碰到又忘记了.刚好今天比較清闲.于是就在网 ...
- Java学习之==>IO文件操作体系
一.概述 在整个 Java.io 中最重要的就是5个类和一个接口.5个类指的是 File.InputStream.OutputStream.Reader.Writer,一个接口指的是Serializa ...
- JAVA对数字证书的常用操作(转载)
一:需要包含的包 import java.security. * ; import java.io. * ; import java.util. * ; import java.security. * ...
- Java篇-File类之常用操作
/** * */ package com.io.file; import java.io.File; import java.io.IOException; /** * <pre> * & ...
- JAVA基于缓冲的文件读写操作
File f2 = new File("e://index.java"); BufferedReader reader = new BufferedReader(new Input ...
- java学习之IO文件分割
package om.gh.homework; import java.io.*; /** * 实现分割文件; * @param file */ public class HomeWork { /** ...
- .Net转Java.07.IDEA和VS常用操作、快捷键对照表
功能 IDEA 2017.1 快捷键 Visual Studio 2015 快捷键 文档 格式化整个文档 Ctrl+Alt+L Ctrl+E,D 或者 Ctrl+K,D 文件 显示最近的 ...
随机推荐
- ubuntu 终端命令颜色的修改
http://blog.chinaunix.net/uid-13954789-id-3137184.html http://blog.chinaunix.net/uid-26021340-id-348 ...
- 【剑指offer】二叉树的镜像,C++实现(先序遍历)
原创博文,转载请注明出处!github地址 博客文章索引地址 1.题目 输入一颗二叉树,将二叉树变换为原二叉树的镜像,如下图所示: 2.思路 二叉树有0个节点 二叉树有1个节点 二叉树有 ...
- Mysql查询架构信息
今天想给整个数据库做初始化,也就是清空所有表,然后让索引归零,使用truncate table 就可以,但好多张表,怎么批量搞定呢? 有人说重建表吧,dump一下,然后再重建,但我还是想用trunca ...
- php simple_html_dom
这个真的很好用,如果用正则,就太麻烦了. 首先,下载simple_html_dom,用include_once就可以使用了. 可以直接定位,可以像个对象一样操作,很方便. $ret=file_get_ ...
- scrapy 的基本命令
scrapy stratproject projectname ##创建一个项目 scrapy genspider myspidername fider ##创建一个spider文件 scrapy ...
- python模块--os模块、sys模块
一.os模块 1 os.getcwd() 获取当前工作的目录,即当前python脚本工作的目录路径 2 3 os.chdir("dirname") 改变当前脚本的工作目录:相当于s ...
- oracle之 11g RAC R2 体系结构---Grid
-- 查看cluster 所维护的资源列表,不包括 OHAS 栈的 daemon [root@node1 bin]# ./crsctl status resource -t-------------- ...
- git clone遇到的[ssh: connect to host github.com port 22]
起因 在学习递归的时候,对汉诺塔小研究了一番,参考网上写了个demo,后面就想同步到github. 过程 这台电脑是新电脑,所以需要先本地生成ssh key:ssh-keygen -t rsa -C ...
- (转)Android内存泄漏分析及调试
http://blog.csdn.net/gemmem/article/details/13017999 此文承接我的另一篇文章:Android进程的内存管理分析 首先了解一下dalvik的Ga ...
- AT指令(二)
1.常用操作1.1 AT命令解释:检测 Module 与串口是否连通,能否接收 AT 命令:命令格式:AT<CR>命令返回:OK (与串口通信正常) (无返回,与串 ...