File类
构造器
File(String pathname)
File file = new File ( "test1.txt" ) ;
File file1 = new File ( "D:\\javaproject\\javasenior\\test2.txt" ) ;
File file2 = new File ( "D:" + File . separator + "javasenior" + File . separator + "test03.txt" ) ;
File.separator文件路径分隔符,Windows环境自动转为\,Linux环境自动转为/
File(String parent, String child)
File file3 = new File ( "D:\\javaproject" , "javasenior" ) ;
File(File parent, String child)
File file4 = new File ( file3, "refine.txt" ) ;
常用方法
getAbsoluteFile():获取文件或目录的绝对路径
System . out. println ( file. getAbsoluteFile ( ) ) ;
getPath():获取文件或目录的构造路径
System . out. println ( file. getPath ( ) ) ;
getParent():获取文件或目录的上级目录
System . out. println ( file. getParent ( ) ) ;
getName():返回文件名称
System . out. println ( file1. getName ( ) ) ;
length():返回文件内容或目录的长度
System . out. println ( file1. length ( ) ) ;
lastModified():返回文件或目录的最近修改时间
System . out. println ( file1. lastModified ( ) ) ;
list():返回一个String数组,表示当前目录下的文件和目录列表
File file = new File ( "D:\\javaproject\\javasenior" ) ;
String [ ] list = file. list ( ) ;
for ( String s: list) {
System . out. println ( s) ;
}
listFiles():返回一个File数组,表示当前目录下的文件和目录列表
File [ ] files = file. listFiles ( ) ;
for ( File f: files) {
System . out. println ( f. getName ( ) ) ;
}
renameTo(File destFile):将文件移动到destFile
要想返回true,必须保证destFile文件在磁盘中不存在
File file = new File ( "test1.txt" ) ;
File file1 = new File ( "D:\\javaproject\\javasenior\\day02\\hello.txt" ) ;
boolean b = file. renameTo ( file1) ;
System . out. println ( b) ;
isFile():判断file是否是一个文件
isDirectory():判断file是否是一个目录
exists():判断文件或目录是否存在
canRead():判断文件是否可读
canWrite():判断文件是否可写
canExecute():判断文件是否可以运行
isHidden():判断是否是隐藏文件
File file = new File ( "D:\\javaproject\\javasenior" ) ;
System . out. println ( file. isFile ( ) ) ;
System . out. println ( file. isDirectory ( ) ) ;
System . out. println ( file. exists ( ) ) ;
System . out. println ( file. canRead ( ) ) ;
System . out. println ( file. canWrite ( ) ) ;
System . out. println ( file. canExecute ( ) ) ;
System . out. println ( file. isHidden ( ) ) ;
createNewFile():文件不存在,创建一个新的空文件并返回true,文件存在,不创建文件并返回false
delete():删除文件或目录
如果文件不存在,则创建文件,如果文件存在,则删除文件。
public void test6 ( ) {
File file = new File ( "test1.txt" ) ;
if ( ! file. exists ( ) ) {
try {
file. createNewFile ( ) ;
System . out. println ( "Create successfully" ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
} else {
file. delete ( ) ;
System . out. println ( "Delete successfully" ) ;
}
}
mkdir():创建目录
mkdirs() :创建目录,如果父目录不存在会一起创建
File file = new File ( "D:\\javaproject\\javasenior\\" ) ;
boolean mkdir = file. mkdir ( ) ;
if ( mkdir) {
System . out. println ( "Create successfully...." ) ;
}
File file1 = new File ( "D:\\javaproject\\javasenior\\io\\tom\\lucy" ) ;
boolean mkdirs = file1. mkdirs ( ) ;
if ( mkdirs) {
System . out. println ( "Create successfully..lucy.." ) ;
}
Path类、Paths类
实例化
Path path = Paths . get ( "D:\\javaproject\\javasenior\\test.txt" ) ;
Path path1 = Paths . get ( "D:\\javaproject\\javasenior" , "test.txt" ) ;
equals():判断是否相等
toAbsolutePath():返回绝对路径
toFile():转为File对象
endsWith():判断此路径是否以给定路径结束。
startsWith():判断此路径是否以给定路径开始。
getFileName():获取文件名称
getParent():获取父级目录
normalize():返回一个路径,该路径消除了冗余的名称元素,比如’.', ‘…’
iterator():迭代器,可以循环输出各级目录
System . out. println ( path. equals ( path1) ) ;
System . out. println ( path. endsWith ( "texs.txt" ) ) ;
System . out. println ( path. endsWith ( path2) ) ;
System . out. println ( path. toAbsolutePath ( ) ) ;
File file = path. toFile ( ) ;
System . out. println ( path. getFileName ( ) ) ;
System . out. println ( path. getParent ( ) ) ;
System . out. println ( path. startsWith ( "D:\\" ) ) ;
System . out. println ( path. normalize ( ) ) ;
Iterator < Path > iterator = path. iterator ( ) ;
while ( iterator. hasNext ( ) ) {
System . out. println ( iterator. next ( ) ) ;
}
Files类
readAllBytes(Path path):按字节读取文件
byte [ ] bytes = Files . readAllBytes ( path) ;
System . out. println ( new String ( bytes) ) ;
readAllLines(Path path):逐行读取文件
List < String > lines = Files . readAllLines ( path) ;
for ( String s: lines) {
System . out. println ( s) ;
}
Files.write():写入文件
Files.write(path,“XXX”.getBytes()):直接写入文件 Files.write(path,“XXX”.getBytes(),StandardOpenOption.APPEND):追加写入 Files.write(path,list):将一个list写入文件,可以将path文件readAllLines读取的内容直接写入文件path1
Path path3 = Paths . get ( "D:\\javaproject\\javasenior" , "refine.txt" ) ;
try {
List < String > list= new ArrayList ( ) ;
list. add ( "cat" ) ;
list. add ( "dog" ) ;
list. add ( "mouse" ) ;
Files . write ( path3, "I LOVE YOU" . getBytes ( ) , StandardOpenOption . APPEND) ;
Files . write ( path3, list) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
Files.newInputStream(Path path):实例化一个字节输入流
Files.newOutputStream(Path path):实例化一个字节输出流
@Test
public void test1 ( ) {
Path srcPath = Paths . get ( "D:\\javaproject\\javasenior\\test.txt" ) ;
Path destPath = Paths . get ( "D:\\javaproject\\javasenior" , "refine.txt" ) ;
InputStream is = null ;
OutputStream os = null ;
try {
is = Files . newInputStream ( srcPath) ;
os= Files . newOutputStream ( destPath) ;
byte [ ] bytes= new byte [ 1024 ] ;
int len;
while ( ( len= is. read ( bytes) ) != - 1 ) {
os. write ( bytes, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( is != null ) {
try {
is. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( os != null ) {
try {
os. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}
Files.newBufferedReader(srcPath):实例化一个字符缓冲输入流
Files.newBufferedWriter(destPath):实例化一个字符缓冲输出流
public static void main ( String [ ] args) {
Path srcPath = Paths . get ( "D:\\01_RefineDocuments\\javaproject\\javasenior\\day02\\test.txt" ) ;
Path destPath = Paths . get ( "D:\\01_RefineDocuments\\javaproject\\javasenior\\day02" , "refine.txt" ) ;
BufferedReader br= null ;
BufferedWriter bw = null ;
try {
br = Files . newBufferedReader ( srcPath) ;
bw = Files . newBufferedWriter ( destPath) ;
int len;
char [ ] chars= new char [ 1024 ] ;
while ( ( len= br. read ( chars) ) != - 1 ) {
bw. write ( chars, 0 , len) ;
}
} catch ( IOException e) {
throw new RuntimeException ( e) ;
} finally {
if ( br!= null ) {
try {
br. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
if ( bw!= null ) {
try {
bw. close ( ) ;
} catch ( IOException e) {
throw new RuntimeException ( e) ;
}
}
}
}