IO相关Demo
这几天复习了IO相关知识
只为记录,好记性不如烂笔头
有误请指正
ありがとうございます。
我的公众号
个人微信公众号:程序猿的月光宝盒
1.判断存在,存在改名,并延迟删除,不存在新建
package pers.day19;
import java.io.File;
/**
* 判断存在,存在改名,并延迟删除,不存在新建
* @author Administrator
*
*/
public class IODemon {
public static void main(String[] args) throws Exception{
test();
}
private static void test() throws Exception {
File dir = new File("E:");
File f1 = new File(dir,"jsc.txt");
System.out.println(f1.exists());
//不存在
if(!f1.exists()){
//新建
f1.createNewFile();
}else{
//存在,把他改名为test.txt,其中new 是新建file对象
f1.renameTo(new File(dir,"test.txt"));
Thread.sleep(10000);
new File(dir,"test.txt").delete();
}
}
}
2.文件夹递归
package pers.day19;
import java.io.File;
/**
* 文件夹递归
* @author Administrator
*
*/
public class IODemon2 {
public static void main(String[] args) {
//新建文件对象
File f = new File("E:/CAPH2018");
//查找所有文件
try {
listAllFiles(f);
} catch (Exception e) {
System.out.println("null");
}
}
private static void listAllFiles(File f) {
//得到所有文件及目录
File[] files = f.listFiles();
for (File file : files) {
//列出所有文件及目录
System.out.println(file);
//file如果是目录,进行递归
if(file.isDirectory()){
listAllFiles(file);
}
}
}
}
3.列出分层结构
package pers.day19;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
/**
* 列出分层结构
* @author Administrator
*
*/
public class IODemon3 {
public static void main(String[] args) {
//效果:CAPH2018->LiveJT_01-AutoRun->Setup->AutoInst->LiveJT_01.exe
String file = "E:/CAPH2018/LiveJT_01-AutoRun/Setup/AutoInst/LiveJT_01.exe";
File f = new File(file);
//把他放在List里
ArrayList<String> parentNames = new ArrayList<>();
//自定义的方法
listAllParent(parentNames, f);
System.out.println(parentNames);//答应得到的List
Collections.reverse(parentNames);//颠倒顺序
System.out.println(parentNames);//答应颠倒顺序后的List
//新建一个字符串做拼接
StringBuilder sb = new StringBuilder(40);
//循环添加List中的内容拼接字符转
for (String name : parentNames) {
sb.append(name).append("->");
}
//删除最后的字符转
sb.deleteCharAt(sb.length() - 1).deleteCharAt(sb.length() - 1);
//最终打印
System.out.println(sb);
}
private static void listAllParent(ArrayList<String> parentNames, File f) {
//如果文件的上级目录的名字不等于空
if (!"".equals(f.getParentFile().getName())) {
//把这个名字加入parentNames的List中
parentNames.add(f.getParentFile().getName());
}
//如文件的上上级目录不等于空(有上上级目录)
if (f.getParentFile().getParentFile() != null) {
//就调用自己,文件是改文件的上级文件
listAllParent(parentNames, f.getParentFile());
}
}
}
4.批量修改文件名,截取掉所有视频文件:教育-Java学院-老师-
package pers.day19;
import java.io.File;
/**
* 批量修改文件名,截取掉所有视频文件:教育-Java学院-老师-.
* @author Administrator
*
*/
public class IOdemon4 {
public static void main(String[] args) {
//路径
File dir = new File("E:/123");
//所有文件
File[] allFiles = dir.listFiles();
//需要删除的字段
String deletes = "教育-学院-老师-";
//循环遍历
for (File file : allFiles) {
//打印获取到的文件
System.out.println(file);
//如果这个文件的名字包含需要删除的字段
if(file.getName().contains(deletes)){
//新的文件名,把需要删除的字段替换成空字符串
String newFileName = file.getName().replace(deletes, "");
//重命名文件
file.renameTo(new File(dir,newFileName ));
}
}
System.out.println("--------------------------------------------------");
//重新获取文件放入之前的File数组
allFiles = dir.listFiles();
//遍历
for (File file : allFiles) {
System.out.println(file);
}
}
}
5.文件过滤器
package pers.day19;
import java.io.File;
import java.io.FilenameFilter;
/**
* 文件过滤器
* @author Administrator
*
*/
public class IOdemon5 {
public static void main(String[] args) {
File dir = new File("E:/123");
File[] files = dir.listFiles(new FilenameFilter() {
//新建匿名内部类
public boolean accept(File dir, String name) {
//这个文件是文件,且name以“.avi结尾”,为true,存到files数组中
return new File(dir,name).isFile() && name.endsWith(".avi");
}
});
for (File file : files) {
System.out.println(file);
}
}
}
6.文件字节输出
package pers.day19;
import java.io.File;
import java.io.FileInputStream;
import java.util.Arrays;
/**
* 文件字节输出
* @author Administrator
*
*/
public class IODemon6 {
public static void main(String[] args) throws Exception {
//1.创建目标对象(表示把数据保存到那一个文件)
File target = new File("file/test.txt");
//2.创建文件字节输出流对象
FileInputStream in = new FileInputStream(target);
//3.具体的输出操作
byte[] b = new byte[5];
System.out.println(Arrays.toString(b));
int len = -1;
while((len = in.read(b)) != -1){
String str = new String(b,0,len);
System.out.println(str);
}
//4.关闭资源
in.close();
}
}
7.文件字节输入
package pers.day19;
import java.io.File;
import java.io.FileOutputStream;
/**
* 文件字节输入
* @author Administrator
*
*/
public class IODemon7 {
public static void main(String[] args) throws Exception {
//1.创建目标对象(表示把数据保存到那一个文件)
File target = new File("file/test.txt");
//2.创建文件字节输出流对象
FileOutputStream out = new FileOutputStream(target,false);
//3.具体的输出操作
out.write("I LOVE U".getBytes(),0,"I LOVE U".length());
//4.关闭资源
out.close();
}
}
8.文件拷贝
package pers.day19;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 文件拷贝
* @author Administrator
*
*/
public class IODemon8 {
public static void main(String[] args) throws Exception {
//1.创建源
File srcFile = new File("file/test.txt");
File destFile = new File("file/test_copy.txt");
//2.创建输入流对象
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
//3.读取操作
byte[] buffer = new byte[1024];//创建1024字节的缓冲区(存贮已经读取的字节数据)
int len = -1;//表示已经读取的字节数,如果-1表示文件读取到最后了
while ((len = in.read(buffer)) != -1) {
//打印读取的数据
System.out.println(new String(buffer,0,len));
//数据在buffer数组中
out.write(buffer, 0, len);
}
//4.关闭输入流对象(资源)
in.close();
out.close();
}
}
9.资源正确关闭
package pers.day19;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
/**
* 文件拷贝关闭资源
* @author Administrator
*
*/
public class IODemon9 {
public static void main(String[] args) throws Exception {
// test1();
test2();
}
/**
* java1.7以前的正常关闭资源
*/
private static void test1() {
//声明资源对象
FileInputStream in = null;
FileOutputStream out = null;
try {
//可能出现的异常代码
//1.创建源
File srcFile = new File("file/test.txt");
File destFile = new File("file/test_copy.txt");
//2.创建输入流对象
in = new FileInputStream(srcFile);
out = new FileOutputStream(destFile);
//3.读取操作
byte[] buffer = new byte[1024];//创建1024字节的缓冲区(存贮已经读取的字节数据)
int len = -1;//表示已经读取的字节数,如果-1表示文件读取到最后了
while ((len = in.read(buffer)) != -1) {
//打印读取的数据
System.out.println(new String(buffer, 0, len));
//数据在buffer数组中
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//4.关闭输入流对象(资源)
try {
if (in != null) {
in.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (Exception e3) {
e3.printStackTrace();
}
}
}
/**
* java1.7新特性 自动关闭资源
*/
private static void test2() {
//1.创建源
File srcFile = new File("file/test.txt");
File destFile = new File("file/test_copy.txt");
try (
//打开资源的代码
//2.创建输入流对象
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
) {
//可能出现异常的代码
//3.读取操作
byte[] buffer = new byte[1024];//创建1024字节的缓冲区(存贮已经读取的字节数据)
int len = -1;//表示已经读取的字节数,如果-1表示文件读取到最后了
while ((len = in.read(buffer)) != -1) {
//打印读取的数据
System.out.println(new String(buffer, 0, len));
//数据在buffer数组中
out.write(buffer, 0, len);
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
IO相关Demo的更多相关文章
- Innodb Read IO 相关参数源代码解析
前言:最近在阅读Innodb IO相关部分的源代码.在阅读之前一直有个疑问,show global status 中有两个指标innodb_data_reads 和 innodb_data_read. ...
- 与IO相关的等待事件troubleshooting-系列9
Buffer Cache与IO相关的等待事件: 这种等待事件的产生原因是包含DBWR进程和IO Slaves的Buffer Cache操作. 'db file parallel write' , 'd ...
- 文件IO 相关的包:java.io文件——API
文件IO 相关的包:java.io文件——API 1.Java.io.File类的使用(1)两种路径绝对路径:相对于当前路径:当前为 “工程名”(2)File类创建,对象为一个文件/目录,可能存在或不 ...
- IO相关操作
IO相关操作 对于IO操作而言,有四个基本的操作:open .read .write .close 我们来逐个解释. 在此之前我们先解释一下什么是文件描述符 文件描述符 操作系统通过一个整数开代 ...
- Properties -IO相关的双列集合类
IO相关的集合类 java.util.Properties集合 extends hashtable(淘汰) Properties类表示了一个持久的属性集.Properties可保存流中或从流中加载 P ...
- 关于golang中IO相关的Buffer类浅析
io重要的接口 在介绍buffer之前,先来认识两个重要的接口,如下边所示: type Reader interface { Read(p []byte) (n int, err error) } t ...
- Java IO相关使用
date: 2020-06-14 14:42:22 updated: 2020-08-21 17:35:45 Java IO相关使用 1. 文件 创建 File 对象的三种方式 一个路径名:File( ...
- IO 相关配置参数
INNODB I/O相关配置 记录日志为顺序I/O,刷新日志到数据文件为随机操作.顺序操作性能快于随机IO. innodb_log_file_size innodb_log_files_in_grou ...
- NodeJs多进程和socket.io通讯-DEMO
一.开启多进程 const os = require('os'); const cp = require('child_process'); const forkList = {}; const fo ...
随机推荐
- Caffe源码-InsertSplits()函数
InsertSplits()函数 在Net初始化的过程中,存在一个特殊的修改网络结构的操作,那就是当某层的输出blob对应多个其他层的输入blob时,会在输出blob所在层的后面插入一个新的Split ...
- Cannot read property 'createElement' of undefined
场景: 架构:React+TS+DVA 具体场景: 在将之前后缀为jsx的组件转化为tsx后缀的组件时,抛出Cannot read property 'createElement' of unde ...
- C#基础——break ,continue, return用法
- DOM事件流的三个阶段
事件发生时会在元素节点之间按照特定的顺序传播,这个传播过程即DOM事件流. DOM事件流分为三个阶段,分别为: 捕获阶段:事件从Document节点自上而下向目标节点传播的阶段: 目标阶段:真正的目标 ...
- Linux 版本控制工具之rabbitvcs
原文地址:http://www.robotshell.com/2017/11/04/Linux-%E7%89%88%E6%9C%AC%E6%8E%A7%E5%88%B6%E5%B7%A5%E5%85% ...
- Feign Date类型时间错误问题
问题 在feign传输date类型的数据时,在调用方时间正确,而被调用方获取时时间会相差14个小时. 原因 Feign客户端在进行通信时,会将Date类型对象转为String类型,如果这个时间是北京时 ...
- 【BZOJ 2138】stone
Problem Description 话说 \(Nan\) 在海边等人,预计还要等上 \(M\) 分钟.为了打发时间,他玩起了石子. \(Nan\) 搬来了 \(N\) 堆石子,编号为 \(1\) ...
- Android OkHttp + Retrofit 断点续传
本文链接 前面我们已经知道如何使用OkHttp+Retrofit下载文件. 下载文件时,可能会遇到一些意外情况,比如网络错误或是用户暂停了下载. 再次启动下载,如果又要从头开始,会白白浪费前面下载好的 ...
- JVM学习分享-思考题
package zero.desk.stringconstantpool; import org.junit.Test; /** * @author Zero * @since 2019-09-17. ...
- mysql创建用户后无法进入
说明 在mysql中添加用户: #mysql -u root -p >use mysql: >update user set password="" where use ...