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 ...
随机推荐
- Netty原理剖析
1. Netty简介 Netty是一个高性能.异步事件驱动的NIO框架,基于JAVA NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作 ...
- Ubuntu 14.04 更新gcc版本至4.9.2
参考: ubuntu 14.04 更新 gcc/g++ 4.9.2 Ubuntu 14.04 更新gcc版本至4.9.2 1.更新源,安装gcc v4.9.2 $ sudo add-apt-repos ...
- Linux——系统引导流程学习简单笔记
开启电源: 固件 firmware(CMOS/BIOS) → POST 加电自检 对硬件就行检查 ↓ 自举程序 BootLoader(GRUB) → 载入内核 ↓ 载入内核 Kernel 1:驱动硬件 ...
- python网络编程之TCP通信实例
一. server.py import socket host="localhost" port= s=socket.socket(socket.AF_INET,socket.SO ...
- 电脑上装两个JDK的方法
在window操作系统上配置两个JDK方便开发以及新JDK的学习 我的机子上的JDk环境为1.8 在cmd中执行:java -version 查看JDK版本 安装方法 在系统变量中配置中设置JAVA_ ...
- MS SQL动态创建临时表
开发业务需求,需要对一个表作数据分析,由于数据量较大,而且分析时字段会随条件相应变化而变化. 因此计划先把数据转插入一个临时表,再对临时表的数据进行分析. 问题点是如何动态创建临时表.原先Insus. ...
- Java字节流实现文件夹的拷贝
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io ...
- 《剑指offer》第八题(重要!查找二叉树的中序遍历的下一个结点)
文件一:main.cpp // 面试题:二叉树的下一个结点 // 题目:给定一棵二叉树和其中的一个结点,如何找出中序遍历顺序的下一个结点? // 树中的结点除了有两个分别指向左右子结点的指针以外,还有 ...
- 8天掌握EF的Code First开发
C#高级知识点&(ABP框架理论学习高级篇)——白金版 http://www.cnblogs.com/farb/p/ABPAdvancedTheoryContent.html
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 成员 Schema 设置
请注意:这部分仅在拷贝用户登录(Copy User on Login)和 同步组成员(Synchronize Group Memberships)被启用后可见. 用户组成员属性(Group Membe ...