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 文件 显示最近的 ...
随机推荐
- DevExpress相关控件中非字符数值居左显示
用了这么长时间的DevExpress控件,今天遇到俩问题. 一个是从头到尾看了一遍编译成功的例子,只能感慨,功能太丰富了,自己所用的不过是冰山一角.有些自己一直想实现的效果,原来早就有现成的可用,汗颜 ...
- Jmeter查看结果树
取样结果: Thread Name: test 1-2 线程名称:测试1 - 2Sample S ...
- 【错误】More than one row with the given identifier was found: 1, for class:
问题原因: 数据库测试数据自己填入,两条数据的id都为1,所以报错 问题解决: 找到数据库表 删除全部数据
- test20181223
Written with StackEdit. T1 取石子(stone) Description 有\(n\)堆石子,第\(i\)堆有\(x_i\)个. \(Alice\)和\(Bob\)轮流取石子 ...
- BZOJ5059 前鬼后鬼的守护 【堆扩展】*
BZOJ5059 前鬼后鬼的守护 Description 八云紫的式神八云蓝有一张符卡名为[式神-前鬼后鬼的守护],这张符卡的弹幕为BOSS从两侧向自机发射大玉,大玉后面跟着一些小玉,形成一个&quo ...
- webdriver常用函数总结
#1 创建浏览器对象 driver = webdriver.Chrome() #2 设置隐式等待10秒 driver.implicitly_wait(10) #3 最大化浏览器窗口 driver.ma ...
- Appium+python (3) 异常处理
有时候定位时会发现无法定位到具体的元素,右侧元素定位处只告诉你这是一个网页视图: 点击里面的具体元素是无法选中的,船长的做法是回到App里点一下元素,再返回要定位的页面,重新点一下Device Scr ...
- sql server2008升级
安装了试用版的sql server2008,要升级为正式企业版.仅仅须要使用安装程序的 维护-升级 功能.在升级时输入企业版序列号,就能升级为正式版.以下给两个序列号 开发版: PTTFM-X467G ...
- 分享Win7 将svn增加系统服务并成功启动的方法
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/default7/article/details/32728717 依照网上搜索到的方法,结果一直提示 ...
- 异常处理—Exception(三)
最近有点事,把这个系列给落下了,给大家道个歉,这里还要感谢我的老婆,谢谢她一直对我的支持:) 系列回顾: 1.异常处理--Exception(一) 2.异常处理—Exception(二) 上一篇中主要 ...