1. 程式人生 > 實用技巧 >實現計算器

實現計算器

package com.chunzhi.Test01.File;

import java.io.File;

/*
    File類獲取功能的方法
        public String getAbsolutePath():返回此File的絕對路徑名字串
        public String getPath():將此File轉換為路徑名字串
        public String getName():返回由此File表示的檔案或資料夾(目錄)的名稱
        public long length():返回由此File表示的檔案的長度(大小)
 */
public class Test03File {
    
public static void main(String[] args) { method04(); } /* public String getAbsolutePath():返回此File的絕對路徑名字串 獲取構造方法中傳遞的路徑 無論路徑是絕對的還是相對的,getAbsolutePath方法返回的都是絕對路徑 */ public static void method01() { File f = new File("C:\\Other\\IdeaProjects\\API-Two"); File absoluteFile
= f.getAbsoluteFile(); System.out.println(absoluteFile); // C:\Other\IdeaProjects\API-Two File f1 = new File("a.txt"); File absoluteFile1 = f1.getAbsoluteFile(); System.out.println(absoluteFile1); // C:\Other\IdeaProjects\API-Two\a.txt } /* public String getPath():將此File轉換為路徑名字串(將路徑原封不動列印輸出) 獲取構造方法中傳遞的路徑
*/ public static void method02() { File f = new File("C:\\Other\\IdeaProjects\\API-Two\\a.txt"); File f1 = new File("a.txt"); System.out.println(f.getPath()); // C:\Other\IdeaProjects\API-Two\a.txt System.out.println(f1.getPath()); // a.txt // File裡面的toString方法,就是呼叫的getPath方法 System.out.println(f1.toString()); // a.txt } /* public String getName():返回由此File表示的檔案或目錄的名稱 獲取的就是構造方法傳遞路徑的結尾部分(檔案/資料夾) */ public static void method03() { File f = new File("C:\\Other\\IdeaProjects\\API-Two\\a.txt"); File f1 = new File("C:\\Other\\IdeaProjects\\API-Two"); System.out.println(f.getName()); // a.txt System.out.println(f1.getName()); // API-Two } /* public long length():返回由此File表示的檔案的長度 獲取的是構造方法指定的檔案的大小,以位元組為單位 注意: 資料夾是沒有大小概念的,不能獲取資料夾的大小 如果構造方法中給出的路徑不存在,那麼length方法返回0 */ public static void method04() { File f = new File("C:\\Other\\IdeaProjects\\API-Two\\a.txt"); File f1 = new File("C:\\Other\\IdeaProjects"); System.out.println(f.length()); // 57 System.out.println(f1.length()); // 0 } }