檔案的基本使用
一、檔案
-
什么是檔案
檔案是保存資料的地方,比如word檔案,txt檔案,excel檔案……都是檔案,即可以保存一張圖片,也可以保持視頻,聲音……
-
檔案流
檔案在程式中是以流的形式來操作的
流:資料在資料源(檔案)和程式(記憶體)之間經歷的路徑
輸入流: 資料從資料源(檔案)到程式(記憶體)的路徑
輸出流:資料從程式(記憶體)到資料源(檔案)的路徑
二、常用的檔案操作
1. 創建檔案物件相關構造器和方法
- new File(String pathName) :根據路徑構建一個File物件,類似絕對路徑
- new File(File parent, String child):根據父目錄檔案夾和子路徑構建,類似根據相對路徑
- new File(String parent, String child):根據父目錄 和 子路徑構建
- createNewFile(): 創建新檔案
-
代碼演示:
import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; /** * @author * @version 1.0 * 演示創建檔案 */ public class FileCreate { public static void main(String[] args) { } //方式1 new File(String pathName),類似根據絕對路徑創建? @Test public void create01(){ String pathName = "e:\\news1.txt"; File file = new File(pathName);//創建檔案物件,此時只是有一個物件在jvm記憶體中 try { file.createNewFile();//創建檔案,這里才對磁盤做出操作,創建出檔案 System.out.println("檔案創建成功"); } catch (IOException e) { e.printStackTrace(); } } //方式2 new File(File parent, String child),類似根據相對路徑創建? //根據父目錄檔案夾和子路徑構建 @Test public void create02(){ File parentFile = new File("e:\\"); String fileName = "news2.txt"; 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 parentPath = "e:/";//也可以這么寫 String fileName = "news3.txt"; File file = new File(parentPath, fileName); try { file.createNewFile(); System.out.println("檔案創建成功"); } catch (IOException e) { e.printStackTrace(); } } }
2. 獲取檔案的相關資訊的方法
-
getName(),getAbsolutePath(),getParent(),length(),exists(),isFile(),isDirectory()
-
代碼演示:
import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; public class FileInformation { public static void main(String[] args) { } //獲取檔案資訊 @Test public void info(){ //創建檔案物件 File file = new File("e:\\news1.txt");//File物件只是一個路徑,可能是檔案也可能是目錄(檔案夾) // try { // file.createNewFile(); // } catch (IOException e) { // e.printStackTrace(); // } //獲取檔案名字 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()); } } /* 運行結果: 檔案名字 = news1.txt 檔案絕對路徑 = e:\news1.txt 檔案父目錄 = e:\ 檔案大小 = 18 檔案是否存在 = true 是不是一個檔案 = true 是不是一個目錄 = false */
3. 目錄的操作和檔案洗掉
- mkdir():創建一級目錄
- mkdirs():創建多級目錄
- delete():洗掉空目錄或檔案
代碼演示:
import org.junit.jupiter.api.Test;
import java.io.File;
public class Directory_ {
public static void main(String[] args) {
}
//判斷 e:\\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 是否存在,存在就洗掉
//目錄也是檔案
@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 + "創建失敗...");
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/554254.html
標籤:其他
下一篇:返回列表