进阶day08-File类的获取方法
This commit is contained in:
29
javaSE-day08/src/com/inmind/file01/FileDemo02.java
Normal file
29
javaSE-day08/src/com/inmind/file01/FileDemo02.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package com.inmind.file01;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
/*
|
||||
File类的获取方法
|
||||
public String getAbsolutePath() :返回此File的绝对路径名字符串。(重点)
|
||||
public String getPath() :将此File转换为路径名字符串。(了解)
|
||||
public String getName() :返回由此File表示的文件或目录的名称。(重点)
|
||||
public long length() :返回由此File表示的文件的长度。
|
||||
|
||||
注意:
|
||||
1.对于length只能来获取文件的大小,而不能获取文件夹的大小.
|
||||
2.在UTF-8编码方式中,一个中文占3个字节
|
||||
在GBK编码方式中,一个中文占2个字节
|
||||
|
||||
|
||||
*/
|
||||
public class FileDemo02 {
|
||||
public static void main(String[] args) {
|
||||
//创建:D:\io_test\a.txt的文件对象
|
||||
File file = new File("D:/io_test/a1.txt");
|
||||
System.out.println(file.getAbsolutePath());//绝对路径
|
||||
System.out.println(file.getPath());//传入的是什么路径就是什么路径
|
||||
System.out.println(file.getName());//文件或目录的名称
|
||||
System.out.println(file.length());//文件的长度(单位:字节)
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user