I/O篇(1)——java.io.File類
阿新 • • 發佈:2018-12-30
一.java.io.File類:檔案和目錄路徑名的抽象表示形式,它就代表一個檔案或目錄(事實上是將變數名與硬碟上的檔案或目錄建立聯絡)
二. java.io.File類的屬性:
- static String pathSeparator — 這是系統相關的路徑分隔符,它被表示為一個字串以方便使用。
- static char pathSeparatorChar — 與系統有關的路徑分隔符
- static String separator — 與系統有關的預設名稱分隔符,它被表示為一個字串以方便使用。
- static char separatorChar — 與系統有關的預設名稱分隔符。
注意:在使用File類指定路徑的時候一定要注意作業系統間的差異,儘量使用separator進行分割
三.一些常用方法
1.構造方法
構造方法 | 描述 |
---|---|
File(String pathname) | 通過將給定的路徑名字串轉換為抽象路徑名來建立一個新的檔案例項。 |
File(String parent , String child) | 通過給定的父路徑名字串和子路徑名字串來建立一個新的檔案例項。 |
File(File parent , String child) | 通過給定的父抽象路徑物件和子路徑名字串來建立一個新的檔案例項。 |
File(URI uri) | 通過給定的URI來建立一個新的檔案例項 |
public class FileDemo {
public static void main(String[] args) throws URISyntaxException {
String pathname = "E:\\Recommended system";
// File(String pathname)
File file1 =new File(pathname);
// File(String parent,String child)
File file2 =new File(pathname,"train_data.txt" );
// File(File parent,String child)
File file3 = new File(file1, "train_data.txt");
// File(URI uri)
// File file4 =new File(new URI(""));
// separator 跨平臺分隔符
File file4 =new File("E:"+File.separator+"Recommended system");
System.out.println(file1);
System.out.println(file2);
System.out.println(file3);
System.out.println(file4);
}
}
執行結果:
E:\Recommended system
E:\Recommended system\train_data.txt
E:\Recommended system\train_data.txt
E:\Recommended system
2.建立與刪除方法
方法 | 描述 |
---|---|
boolean createNewFile() | 如果檔案存在返回false,否則返回true並且建立檔案 |
boolean mkdir() | 建立目錄,必須確保父目錄鏈存在,如果不存在則建立失敗 |
boolean mkdirs() | 建立多級目錄,如果父目錄鏈不存在也會創建出來 |
boolean delete() | 如果檔案存在返回true並且刪除檔案,否則返回false |
boolean deleteOnExit() | 檔案使用完成後刪除 |
static File createTempFile(String prefix, String suffix) | 在預設目錄中建立一個臨時檔案,使用給定字首和字尾生成其名稱。 |
static File createTempFile(String prefix, String suffix, File directory) | 在指定目錄中建立一個臨時檔案,使用給定字首和字尾生成其名稱。 |
public class FileDemo {
public static void main(String[] args) throws URISyntaxException {
String pathname = "D:\\Recommended system.txt";
// 建立檔案例項
File file = new File(pathname);
try {
// 如果檔案存在返回false,否則返回true並且建立檔案
if(file.createNewFile()){
System.out.println("建立檔案["+pathname+"]");
}//if
else{
System.out.println("檔案["+pathname+"]已存在");
}//else
} catch (IOException e) {
e.printStackTrace();
}
}
}
public class FileDemo {
public static void main(String[] args) {
String pathname = "D:\\Recommended system.txt";
// 建立檔案例項
File file = new File(pathname);
//如果檔案存在返回true並且刪除檔案,否則返回false
if(file.delete()){
System.out.println("刪除檔案["+pathname+"]");
}//if
else{
System.out.println("檔案["+pathname+"]不存在");
}//else
}
}
public class FileDemo {
public static void main(String[] args) {
String pathname = "D:\\Recommended system";
String pathname2 = "D:\\Recommended system2\\Paper\\News";
// 建立檔案例項
File file = new File(pathname);
File file2 = new File(pathname2);
//如果目錄不存在返回true並且建立目錄,否則返回false
if(file.mkdir()){
System.out.println("建立目錄["+pathname+"]");
}//if
else{
System.out.println("目錄["+pathname+"]已存在");
}//else
//如果多級目錄不存在返回true並且建立多級目錄,否則返回false
if(file2.mkdirs()){
System.out.println("建立多級目錄["+pathname2+"]");
}//if
else{
System.out.println("多級目錄["+pathname2+"]已存在");
}//else
}
}
3.判斷方法
方法 | 描述 |
---|---|
boolean canExecute() | 判斷檔案是否可執行 |
boolean canRead() | 判斷檔案是否可讀 |
boolean canWrite() | 判斷檔案是否可寫 |
boolean exists() | 判斷檔案是否存在 |
boolean isDirectory() | 判斷是否是目錄 |
boolean isFile() | 判斷是否是檔案 |
boolean isHidden() | 判斷是否隱藏 |
boolean isAbsolute() | 判斷是否是絕對路徑 檔案不存在也能判斷 |
public class FileDemo {
public static void main(String[] args) {
String pathname = "D:\\Recommended system\\train_data.txt";
// 建立檔案例項
File file = new File(pathname);
// 判斷檔案是否可執行
if(file.canExecute()){
System.out.println("檔案["+pathname+"]可執行");
}//if
else{
System.out.println("檔案["+pathname+"]不可執行");
}//else
// 判斷檔案是否可讀
if(file.canRead()){
System.out.println("檔案["+pathname+"]可讀");
}//if
else{
System.out.println("檔案["+pathname+"]不可讀");
}//else
// 判斷檔案是否可寫
if(file.canWrite()){
System.out.println("檔案["+pathname+"]可寫");
}//if
else{
System.out.println("檔案["+pathname+"]不可寫");
}//else
// 判斷檔案是否存在
if(file.exists()){
System.out.println("檔案["+pathname+"]存在");
}//if
else{
System.out.println("檔案["+pathname+"]不存在");
}//else
// 判斷檔案是否是目錄
if(file.isDirectory()){
System.out.println("檔案["+pathname+"]是目錄檔案");
}//if
else{
System.out.println("檔案["+pathname+"]不是目錄檔案");
}//else
// 判斷是否是一個絕對路徑
if(file.isAbsolute()){
System.out.println("["+pathname+"]是絕對路徑");
}//if
else{
System.out.println("["+pathname+"]不是絕對路徑");
}//else
}
}
4.獲取方法
方法 | 描述 |
---|---|
String getName() | 返回檔案或者是目錄的名稱 |
String getPath() | 返回路徑 |
String getAbsolutePath() | 返回絕對路徑 |
String getParent() | 返回上一級錄,如果沒有上一級目錄則返回null |
long lastModified() | 返回最後一次修改的時間 |
long length() | 返回檔案的長度(位元組數)(只有檔案才能讀出長度,資料夾的長度為0) |
File[] liseRoots() | 返回系統可用的系統盤 |
String[] list() | 返回一個字串陣列,陣列內容是給定路徑下的檔案或目錄名稱 |
String[] list(FilenameFilter filter) | 返回滿足過濾器要求的一個字串陣列 |
File[] listFiles() | 返回一個檔案物件陣列,給定路徑下檔案或目錄 |
File[] listFiles(FilenameFilter filter) | 返回滿足過濾器要求的一個檔案物件陣列 |
public class FileDemo {
public static void main(String[] args) {
String pathname = "E:\\Recommended system";
// 建立檔案例項
File file = new File(pathname);
// 返回檔案或者目錄的名稱
System.out.println("GetName->"+file.getName());
// 返回路徑
System.out.println("GetPath->"+file.getPath());
// 返回檔案長度
System.out.println("Length->"+file.length());
// 返回給定路徑下的檔案和目錄字串陣列
String[] files = file.list();
for (String name : files) {
System.out.println("名稱:"+name);
}//for
File[] files2 = file.listFiles();
for (File file2 : files2) {
if(file2.isFile()){
System.out.println("檔名稱:"+file2.getName());
}//if
else if(file2.isDirectory()){
System.out.println("目錄名稱:"+file2.getName());
}//else
}//for
// 列出可用的系統盤
File[] files3 = file.listRoots();
for (File file3 : files3) {
System.out.println("檔名稱:"+file3.getPath());
}
}
}
5.檔案過濾例項
public class FileDemo {
public static void main(String[] args) {
String pathname = "E:\\Recommended system";
// 建立檔案例項
File file = new File(pathname);
// 檔案過濾
File[] files = file.listFiles(new FilenameFilter() {
@Override
public boolean accept(File file, String filename) {
return filename.endsWith(".mp3");
}
});
// 列印輸出
for (File file2 : files) {
System.out.println("名稱:"+file2.getName());
}//for
}
}
執行結果:
名稱:1.mp3
名稱:2.mp3
名稱:3.mp3
6.列出資料夾下全部目錄和檔案
public class FileDemo {
public static void ListAllFile(File file){
if(!file.exists()){
throw new IllegalArgumentException("目錄["+file+"]不存在");
}//if
if(!file.isDirectory()){
throw new IllegalArgumentException("["+file+"]不是目錄");
}//if
// 列出指定路徑下的全部檔案或目錄
File[] files = file.listFiles();
for (File fileName : files) {
// 判斷是否是目錄 如果是遞迴
if(fileName.isDirectory()){
ListAllFile(fileName);
}//if
// 否則列印輸出
else{
System.out.println(fileName.getPath());
}//else
}//for
}
public static void main(String[] args) {
String pathname = "E:\\Recommended system";
// 建立檔案例項
File file = new File(pathname);
// 列出全部檔案或目錄
ListAllFile(file);
}
}
7.移動檔案(找出d盤下所有的 .java 檔案,拷貝至 c:\jad 目錄下,並將所有檔案的型別由.java 修改為.jad)
public class Test5 {
public static void main(String[] args){
File f1 = new File("d:\\");
moveFile(f1);
}
public static void moveFile(File dir){
File[] files=dir.listFiles();
for(File file:files){
if(file.isDirectory())
moveFile(file);
else{
if(file.getName().endsWith(".java"))
file.renameTo(new File("c:\\jad\\"+
file.getName().substring(0,file.getName().lastIndexOf('.'))+".jad"));
}
}
}
}