【Java】常用的文件操作
1. 文件

2. 文件流
2. 常用的文件操作
2.1 創建文件對象相關構造器和方法
2.2 獲取文件相關信息的方法
2.4 目錄的操作和文件刪除
1. 文件
2. 文件流
2. 常用的文件操作
2.1 創建文件對象相關構造器和方法
2.2 獲取文件相關信息的方法
2.4 目錄的操作和文件刪除
1. 文件
文件,對我們并不陌生,文件是保存數據的地方,比如經常使用的word文檔,txt文件,excel文件…都是文件。它既可以保存一張圖片,也可以保持視頻,聲音等。
2. 文件流
文件在程序中是以流的形式來操作的
流:數據在數據源(文件)和程序(內存)之間經歷的路徑
輸入流:數據從數據源(文件)到程序(內存)的路徑
輸出流:數據從程序(內存)到數據源(文件)的路徑
2. 常用的文件操作
2.1 創建文件對象相關構造器和方法
相關方法
new File(String pathname)//根據路徑構建一個File對象 new File(File parent,String child)//根據父目錄文件+子路徑構建 new File(String parent,String child)//根據父目錄+子路徑構建
createNewFile創建新文件
案例演示
請在e盤下,創建文件 news1.txt、news2.txt、news3.txt,用三種不同的方式創建
public class FileCreate { public static void main(String[] args) { } //方式1 new File(String pathname) @Test public void create01() { String filePath = "e:\\news1.txt"; File file = new File(filePath); try { file.createNewFile(); System.out.println("文件創建成功"); } catch (IOException e) { e.printStackTrace(); } } //方式2 new File(File parent,String child) //根據父目錄文件+子路徑構建 //e:\\news2.txt @Test public void create02() { File parentFile = new File("e:\\"); String fileName = "news2.txt"; //這里的file對象,在java程序中,只是一個對象 //只有執行了createNewFile 方法,才會真正的,在磁盤創建該文件 File file = new File(parentFile, fileName); try { file.createNewFile(); System.out.println("創建成功~"); } catch (IOException e) { e.printStackTrace(); } } //方式3 new File(String parent,String child) //根據父目錄+子路徑構建 @Test public void create03() { String parentPath = "e:\\"; String fileName = "news4.txt"; File file = new File(parentPath, fileName); try { file.createNewFile(); System.out.println("創建成功~"); } catch (IOException e) { e.printStackTrace(); } } }
2.2 獲取文件相關信息的方法
getName、getAbsolutePath、getParent、length、exists、isFile、isDirectory
應用案例演示
如何獲取到文件的大小,文件名,路徑,父File,是文件還是目錄(目錄本質也是文件,一種特殊的文件)是否存在。
//獲取文件信息 @Test public void info() { File file = new File("e:\\news1.txt"); //調用X響應方法,得到相應信息 System.out.println("文件名字=" + file.getName()); System.out.println("文件絕對路徑=" + file.getAbsolutePath()); System.out.println("文件父級目錄=" + file.getParent()); System.out.println("文件大小(字節)=" + file.length()); System.out.println("文件是否存在=" + file.exists()); System.out.println("是不是一個文件=" + file.isFile()); System.out.println("是不是一個目錄=" + file.isDirectory()); }
2.4 目錄的操作和文件刪除
mkdir創建一級目錄、mkdirs創建多級目錄、delete刪除空目錄或文件
案例演示
判斷d:\\news1.txt是否存在,如果存在就刪除,否則提示不存在
@Test public void m1() { String filePath = "e:\\news1.txt"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "刪除成功"); } else { System.out.println(filePath + "刪除失敗"); } } else { System.out.println("該文件不存在..."); } }
判斷 d:\\demo02是否存在,存在就刪除,否則提示不存在
//在java編程中,目錄也被當做文件 @Test public void m2() { String filePath = "D:\\demo02"; File file = new File(filePath); if (file.exists()) { if (file.delete()) { System.out.println(filePath + "刪除成功"); } else { System.out.println(filePath + "刪除失敗"); } } else { System.out.println("該目錄不存在..."); } }
判斷 d:\\demo\\a\\b\\c目錄是否存在,如果存在就提示已經存在,否則就創建
@Test public void m3() { String directoryPath = "D:\\demo\\a\\b\\c"; File file = new File(directoryPath); if (file.exists()) { System.out.println(directoryPath + "存在.."); } else { if (file.mkdirs()) { //創建一級目錄使用mkdir() ,創建多級目錄使用mkdirs() System.out.println(directoryPath + "創建成功.."); } else { System.out.println(directoryPath + "創建失敗..."); } } }
Java
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。