java IO流 之 FIle類基礎

來源:互聯網
上載者:User

標籤:abs   pictures   res   test   lse   檔案大小   let   反編譯   getname   

package IO;

import java.io.File;
import java.io.IOException;

public class FileIO {


/**
* 構建及擷取檔案名稱資訊
* @param args
*/
public static void FileName(String[] args) {
System.out.println("路徑分隔字元: "+File.pathSeparator); //路徑分隔字元
System.out.println("名稱分隔字元: "+File.separator);//名稱分隔字元

//相對路徑 以工程為起始路徑去找到對應的檔案 例如: 1.jpg
String path = "img"; //相對路徑
String name = "1.jpg";

//絕對路徑 從電腦硬碟中找對應的檔案例如:C:\Users\Yonim\Pictures\Camera Roll\1.jpg
//在java當中 “\”是特殊字元 需要反編譯才行 其中的\\ 為 \
String path2 = "C:\\Users\\Yonim\\Pictures\\Camera Roll\\2.jpg";
//String name2 = "";


File file = new File(path,name); //這是建立一個相對路徑的構造器
file =new File(new File(path),name);//第二種構建方式

System.out.println("找出檔案名稱:"+file.getName());
System.out.println("找出檔案路徑:"+file.getPath());

// 以絕對路徑建立的構造器

File f = new File(path2);
System.out.println("絕對路徑的檔案名稱:"+f.getName());
System.out.println("絕對路徑的檔案路徑:"+f.getPath());
System.out.println("上一級目錄:"+f.getParent());//如果沒有上一級目錄返回空
File url = new File("Users\\Yonim\\Pictures\\Camera Roll\\2.jpg");

//在檔案沒有盤符的情況下 以工程檔案名稱的盤符做為絕對路徑的盤符
System.out.println("絕對路徑的檔案名稱"+url.getAbsolutePath());

}

/**
* 判斷檔案資訊
*/
public static void isFile(){

String path = "img";
String name = "1.jpg";

File file = new File(path,name);

System.out.println(file.exists()); //判斷檔案或者檔案夾是否存在,並且是否為可寫的狀態,存在並且可寫則返回true

/**
* 判斷是否檔案,存在此檔案時返回true
* 當不是檔案或者不存在的時候都是返回false
*/
if (file.isFile()) { //是否檔案
System.out.println("存在此檔案");
}else if(file.isDirectory()){//是否檔案夾
System.out.println("是檔案夾");
}else{
System.out.println("檔案不存在");
}

/**
* 判斷是否絕對路徑
* 是返回true 否則返回false
*/
if (file.isAbsolute()) {
System.out.println("是絕對路徑");
}else{
System.out.println("相對路徑");
}

/**
* 可以利用檔案大小來判斷是否檔案還是檔案夾
* file.length不能讀取檔案夾大小
*/
if (file.length()>0) {
System.out.println(file.getName()+"的檔案大小為:"+file.length()+"位元組");
}

}

/**
* 建立、刪除檔案資訊
* @throws IOException
*/
public static void newFile() throws IOException{

String path = "D:\\myImg";
String name = "1.jpg";
File file = new File(path,name);

//如果不是檔案
if (!file.exists()) {
//如果存在則返回false
System.out.println("是否建立成功:"+file.createNewFile());
}

//刪除檔案
if (file.delete()) {
System.out.println("刪除成功");
}else{
System.out.println("刪除錯誤,檔案不可被刪除");
}



}


/**
* 操作目錄
*/
public static void operFile(){

String path = "D:\\MyImg\\test";
String name = "3.jpg";

File file = new File(path,name);
if(file.mkdir()){//如果沒有父目錄則不能建立

}

if (file.mkdirs()) {//如果沒有父目錄則連父目錄一同建立

}
}


public static void main(String[] args) {

}



}

java IO流 之 FIle類基礎

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.