(JAVA)File类
生活随笔
收集整理的這篇文章主要介紹了
(JAVA)File类
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
package filedemo;import java.io.File;
import java.io.IOException;/*** @author Alina* @date 2021年10月07日 10:33 下午* File類 提供方法操作 文件 目錄 文件夾* 1.兩個(gè)靜態(tài)成員變量 static String pathSeparator 路徑分隔符* 2.static String separator 名稱分隔符* 3.創(chuàng)建文件boolean createNewFile()* 4.創(chuàng)建單一文件夾 boolean mkdir()* 5.創(chuàng)建多層目錄 boolean mkdirs()* 6.刪除目錄(文件) boolean delete()* 7.重命名 boolean renameTo( File newFileName)* 8.判斷文件是否存在 boolean exists()* 9.判斷是否文件夾 boolean isDirectory()* 10.判斷是否為文件 boolean isFile* 11.獲取當(dāng)前路徑下所有的文件static File[] listRoots()* 12.獲取File構(gòu)造器封裝的絕對路徑 String getAbsolutePath() File getAbsoluteFile()* 13.獲取文件名字**/
public class FileDemo {public static void main(String[] args)throws Exception {method_getAbsolutesFile();}public static void method_1(){File parent = new File("/Users/yuzhang/Desktop/");File file = new File(parent,"IOPrciice");System.out.println(file);}//創(chuàng)建文件功能public static void method_2() throws IOException {File parent = new File("/Users/yuzhang/Desktop/");File file = new File(parent,"test.txt");boolean b = file.createNewFile();}//創(chuàng)建文件夾public static void method_3() throws IOException{File parent = new File("/Users/yuzhang/Desktop/");File file = new File(parent,"新建文件夾");boolean c = file.mkdir();System.out.println(c);}//創(chuàng)建文件夾public static void method_4() throws IOException{File parent = new File("/Users/yuzhang/Desktop/");File file = new File(parent,"新建文件夾/a/b");boolean c = file.mkdirs();System.out.println(c);}//刪除文件夾public static void method_5(){File parent = new File("/Users/yuzhang/Desktop");File file = new File(parent,"新建文件夾");boolean d = file.delete();System.out.println(d);}//重命名public static void method_6(){File parent = new File("/Users/yuzhang/Desktop");File file = new File(parent,"新建文件夾1");file.mkdir();File newFile = new File("新建");boolean b = file.renameTo(newFile);System.out.println(b);}public static void method_exists() throws Exception{File parent = new File("/Users/yuzhang/Desktop");File file = new File(parent,"test.txt");file.createNewFile();boolean b = file.exists();System.out.println(b);}public static void method_listRoots(){File file[] = File.listRoots();for (File f : file){System.out.println(f);}}public static void method_getAbsolutesFile(){File file = new File("/Users/yuzhang");File p = file.getAbsoluteFile();System.out.println(p);System.out.println(p.getName());}public static void method_getAbsolutesPath(){File file = new File("/Users/yuzhang");String s = file.getAbsolutePath();System.out.println(s);}}
總結(jié)
以上是生活随笔為你收集整理的(JAVA)File类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 2019编译ffepeg vs_如何在w
- 下一篇: 谷歌引擎html,国内免费使用谷歌翻译引