Java——File类成员方法
body, table{font-family: 微软雅黑}
table{border-collapse: collapse; border: solid gray; border-width: 2px 0 2px 0;}
th{border: 1px solid gray; padding: 4px; background-color: #DDD;}
td{border: 1px solid gray; padding: 4px;}
tr:nth-child(2n){background-color: #f8f8f8;}
div, p, blockquote{line-height: 150%}
|
File file=new File("D:\\temp\\1.txt"); //创建指向文件的对象实例
File tempFold=new File("D:\\temp");
try {
boolean result=tempFold.mkdir(); //用实例创建文件夹
System.out.println("mkdir(): "+result);
boolean flag=file.createNewFile(); //创建文件
System.out.println("createNewFile(): "+flag);
} catch (IOException e) {
e.printStackTrace();
}
|
File path=new File("D:\\Demo\\java\\File"); //多级目录,包含三个文件夹
//boolean pathCreat=path.mkdirs(); //如果上面路径上有两个或以上的文件夹不存在,他会都创建,返回true
boolean pathCreat=path.mkdir(); //如果只有上面最后一个File 不存在,他会创建并返回True,否则False
System.out.println("mkdirs(): "+pathCreat);
|
|
File newFile=new File("1.txt"); //会在资源管理器列表下创建文件
File newFold=new File("myfold");
boolean ret;
try {
ret = newFile.createNewFile();
ret = newFold.mkdir();
System.out.println("creatNewFile(): "+ret);
ret = newFile.delete();
} catch (IOException e) {
e.printStackTrace();
}
newFold.delete(); //文件夹删除不会抛出异常,不用try catch 包围。
|
|
File newFile = new File("1.txt"); //1.txt必须事先存在,否则后面的 renameTo() 返回false
File newFile2 = new File("D:\\Demo\\java\\File\\2.txt");
//相当于剪切,把1.txt剪切到D:\Demo\java\File\目录下,并改名为2.txt;如果newFile2里面要改的名也是1.txt,返回false,更改失败
ret = newFile.renameTo(newFile2);
System.out.println("renameFile(): "+ret);
|
|
boolean ret;
File newFile = new File("2.txt");
File newFold = new File("2_txt");
try {
newFold.mkdir();
newFile.createNewFile();
ret = newFold.isFile();
System.out.println("newFold.isFile(): "+ret);
ret = newFold.isDirectory();
System.out.println("newFold.isDirectory(): "+ret);
ret = newFile.isFile();
System.out.println("newFile.isFile(): "+ret);
ret = newFile.isDirectory();
System.out.println("newFile.isDirectory(): "+ret);
} catch (IOException e) {
e.printStackTrace();
}
|
System.out.println("newFold.exists(): "+newFold.exists());
System.out.println("newFile.exists(): "+newFile.exists());
System.out.println("newFold.canRead(): "+newFold.canRead());
System.out.println("newFile.canRead(): "+newFile.canRead());
System.out.println("newFile.isHidden(): "+newFile.isHidden()); //false 没有隐藏
|
|
String path = newFile.getAbsolutePath(); //绝对路径+文件名
System.out.println("newFile.getAbsolutePath(): "+path);
File path_file = newFile.getAbsoluteFile(); //返回文件型的路径
String pth = path_file.getAbsolutePath();
System.out.println("path_file.getAbsolutePath(): "+pth);
String path2 = newFile.getPath();
System.out.println("newFile.getPath(): "+path2); //相对路径,对于文件,获取文件名 2.txt
//对于文件夹,返回新建该文件夹时传入的相对路径
|
File newFold2 = new File("2_txt\\test");
File newFold3 = new File("D:\\2_txt\\test");
String path3 = newFold.getPath();
String path4 = newFold2.getPath();
String path5 = newFold3.getPath();
System.out.println("newFold.getPath(): "+path3);
System.out.println("newFold.getPath(): "+path4); //2_txt ;
System.out.println("newFold.getPath(): "+path5);
System.out.println("newFile.length(): "+newFile.length()); //0;文件里我没有放东西
long time = newFile.lastModified(); //1970-01-01 0时到现在经过了多少秒
System.out.println("newFile.lastModified(): "+time);
Date date = new Date(time); //date = Tue Mar 28 13:17:41 CST 2017
System.out.println("newFile.lastModified(): "+date.toLocaleString()); //函数划横线就是过时了
System.out.println("newFile.lastModified(): "+date.toString());
//java 1.7 toString() 会显示2017-03-28
|
|
//list 获取某个目录下面的所有文件和文件夹的名字,生成一个名字数组返回;
File path = new File("D:\\");
String[] list = path.list();
for(int i = 0;i<list.length;++i){ //数组长度,用length属性,其他的用length()方法
System.out.println("path.list(): "+list[i]);
}
|
//listFiles 获取某个目录下面的所有文件和文件夹的file实例,生成一个文件数组返回;
File[] listFiles = path.listFiles();
for(int i = 0;i<listFiles.length;++i){
System.out.println("path.listFiles(): "+listFiles[i]+" (是否为文件): "+listFiles[i].isFile());
}
|
|
/**
* 批量修改文件名称
* 假设你某天跟朋友出去玩,使用某数码相机拍摄了一些照片。但是当你回来的时候你发现所有的照片都是如下命名的。
* P1020335.JPG
* P1020336.JPG
* P1020337.JPG
* P1020338.JPG
* P1020339.JPG
* 这些文件名实际上是自动生成的,对你来说不方便看。
* 你现在希望将这些照片都改成“2015-4-15-i” i表示第几张照片
* 如
* 2015-4-15-1
* 2015-4-15-2
* 2015-4-15-3
* 请设计一个程序实现自动修改。
*/
|
public static void main(String[] args) {
for(int i = 0;i<10;++i){
File file = new File("D:\\test\\12016001233"+i+".jpg");
try {
boolean ret = file.createNewFile(); //新建要改写的文件
} catch (IOException e) {
e.printStackTrace();
}
}
File file2 = new File("D:\\test");
File[] files = file2.listFiles();
System.out.println(files.length);
for(int j = 0;j<files.length;++j){
File file3 = new File("D:\\test\\2015-4-15-"+(j+1)+".JPG"); //最终要改成的文件名
files[j].renameTo(file3); //更改;第一次运行正确,多次运行失败,因为要改的名字文件夹中已经存在;
}
}
|
Java——File类成员方法的更多相关文章
- Java File类总结和FileUtils类
Java File类总结和FileUtils类 文件存在和类型判断 创建出File类的对象并不代表该路径下有此文件或目录. 用public boolean exists()可以判断文件是否存在. Fi ...
- Java File 类的使用方法详解
Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对Java File文件操作类进行详细地分析,并将File类中的常用方法进行简单介绍,有需要的Java开发者可以看 ...
- Java File 类的使用方法详解(转)
转自:http://www.codeceo.com/article/java-file-class.html Java File类的功能非常强大,利用Java基本上可以对文件进行所有的操作.本文将对J ...
- Java File类 mkdir 不能创建多层目录
File f = new File("/home/jp/Upload"); if ((!f.exists()) || (!f.isDirectory())) {boolean re ...
- Java File类基础解析 1
Java File类基础解析 1 File类的构造方法 public File(String pathname) :通过给定的路径名字符转换为抽象路径名来创建新的File实例 String path ...
- Java File类基本操作
我们可以利用Java.io.File类对文件进行操作,基本操作如下: 1)创建文件: public boolean createNewFile() throws IOException 2)删除文件: ...
- JAVA File类 分析(三)
前面两篇与大家一起研究了unix下的文件系统,本篇将和大家一起分析 文件的属性和文件夹. ok,废话不说,先来段代码 #include <stdio.h> #include <sys ...
- Java——File类概述
body, table{font-family: 微软雅黑} table{border-collapse: collapse; border: solid gray; border-width: 2p ...
- java File类的基本使用
package com.soar.file; import java.io.File; import java.io.IOException; public class Demo2_FileMetho ...
随机推荐
- BZOJ5168: [HAOI2014]贴海报 线段树
Description Bytetown城市要进行市长竞选,所有的选民可以畅所欲言地对竞选市长的候选人发表言论.为了统一管理,城市委 员 会为选民准备了一个张贴海报的electoral墙.张贴规则如下 ...
- ajax用beforeSend自定义请求过程中客户端事件,提高用户体验
本文为博主原创,未经允许不得转载: 在应用ajax的过程中,当我们再前台提交请求的时候,如果服务端响应事件比较长,就会导致需要等很长时间在前台才能接受到服务端返回的 响应结果,往往会导致用户重复点击按 ...
- OpenFlow protocol version 1.0 通信过程
参考 李呈:[原创]OpenFlow通信流程解读 OpenFlow protocol version 1.0 通信过程 通信双方: OpenFlow控制器,OpenFlow交换机. 通信模块: Sec ...
- spring cloud kubernetes之serviceaccount permisson报错
spring boot项目引用spring-cloud-starter-kubernetes <dependency> <groupId>org.springframework ...
- nginx缓存功能的设置
首先用的缓存是proxy_cache. 在http段里加入下列几句: [plain] view plain copy proxy_connect_timeout 5; proxy_read_tim ...
- Linux——文件处理命令简单学习总结
linux中一共有三种用户: 1: 所有者u(User) 2: 所属组g(group) 3: 其他用户o(other) linux中权限一共分三种: 1: r read 读权限 2: w write ...
- selenium-chrome-headless
#coding=utf-8 from selenium import webdriver import time chrome_options = webdriver.ChromeOptions() ...
- TDD、BDD、DDD
TDDTest-Driven DevelopmentTest-Driven Development (TDD) is a software development technique where au ...
- 《剑指offer》第三十二题(分行从上到下打印二叉树)
// 面试题32(二):分行从上到下打印二叉树 // 题目:从上到下按层打印二叉树,同一层的结点按从左到右的顺序打印,每一层 // 打印到一行. #include <cstdio> #in ...
- Tensorflow的基本概念与常用函数
Tensorflow一些常用基本概念与函数(一) 1.tensorflow的基本运作 为了快速的熟悉TensorFlow编程,下面从一段简单的代码开始: import tensorflow as tf ...