創建檔案操作
在JavaIO編程中,有以下三種創建檔案的方式,分別是:
new File(String pathname)//根據路徑構建一個File物件
new File(File parent,String child)//根據父目錄檔案+子路徑構建
new File(String parent,String chile)//根據父目錄+子路徑構建
先看下面的這段程式,分別通過creat01,creat02,creat03三種方法來演示檔案的創建:
package IOstream;
import java.io.File;
import java.io.IOException;
@SuppressWarnings({"all"})
/**
* @Author Blueshadow
* @Date 2021/9/7 20:36
* @Version 1.0
* 創建檔案
*/
public class filesCreat {
public static void main(String[] args) {
filesCreat filesCreat = new filesCreat();
filesCreat.creat01();
filesCreat.creat02();
filesCreat.creat03();
}
//方式一:根據檔案路徑來創建檔案
public void creat01(){
String path = "E:\\學習資料/編程/folder/news1.txt";//根據路徑
File file1 = new File(path);//還沒有創建file物件
try {
file1.createNewFile();
System.out.println("檔案創建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式二:根據父目錄檔案+子路徑構建
public void creat02(){
File pathName = new File("E:\\學習資料/編程/folder");//父目錄檔案,即檔案目錄
String fileName = "news2.txt";//子路徑,即檔案名
File file1 = new File(pathName,fileName);
try {
file1.createNewFile();
System.out.println("檔案創建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
//方式三:根據父目錄+子路徑構建
public void creat03(){
String parentPath = "E:\\學習資料/編程/folder";//父目錄
String filePath = "/folder01/news1.txt";//檔案路徑
File file = new File(parentPath,filePath);//創建檔案物件
try {
file.createNewFile();
System.out.println("檔案創建成功");
} catch (IOException e) {
}
}
}
- creat01:根據檔案路徑來創建檔案,
public void creat01(){
String path = "E:\\學習資料/編程/folder/news1.txt";//定義了一個檔案的路徑
File file1 = new File(path);//創建了一個File物件
try {
file1.createNewFile();//使用物件的createNewFile方法,根據定義好的路徑去創建檔案
System.out.println("檔案創建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
- creat02:根據父目錄檔案+子路徑構建
public void creat02(){
File pathName = new File("E:\\學習資料/編程/folder");//父目錄檔案,即要創建檔案所在的目錄
String fileName = "news2.txt";//子路徑,即檔案名
File file1 = new File(pathName,fileName);//創建File物件
try {
file1.createNewFile();
System.out.println("檔案創建成功");
} catch (IOException e) {
e.printStackTrace();
}
}
- creat03:根據父目錄檔案+子路徑構建,
public void creat03(){
String parentPath = "E:\\學習資料/編程/folder";//父目錄
String filePath = "/folder01/news1.txt";//檔案路徑
File file = new File(parentPath,filePath);//創建檔案物件
try {
file.createNewFile();
System.out.println("檔案創建成功");
} catch (IOException e) {
}
}
要使用以上方法去創建檔案,只需要在主程式中去新建類物件,然后呼叫對應的方法即可,
filesCreat filesCreat = new filesCreat();
filesCreat.creat01();
filesCreat.creat02();
filesCreat.creat03();
獲取檔案的相關資訊
在JavaIO編程中,最常見的獲取檔案相關資訊的方法有很多種,以下面程式為例:
package IOstream;
import java.io.File;
import java.io.IOException;
@SuppressWarnings({"all"})
/**
* @Author Blueshadow
* @Date 2021/7/28 9:02
* @Version 1.0
*/
public class filesCreat {
public static void main(String[] args) {
filesCreat files = new filesCreat();
files.info();
}
public void info(){
//創建檔案路徑
File file = new File("F:\\test/news.txt");
System.out.println(file.getName());//檔案名稱
System.out.println(file.length());//檔案大小(按照位元組來計算)
System.out.println(file.exists());//判斷檔案是否存在
System.out.println(file.getAbsolutePath());//檔案絕對路徑
System.out.println(file.getParent());//檔案父級目錄
System.out.println(file.isFile());//判斷是否是一個檔案
System.out.println(file.isDirectory());//判斷是否是一個目錄
}
}
目錄操作和檔案洗掉
通過下面的程式來演示最常見的目錄操作和檔案洗掉
package IOstream;
import java.io.File;
import java.io.IOException;
@SuppressWarnings({"all"})
/**
* @Author Blueshadow
* @Date 2021/7/28 9:02
* @Version 1.0
*/
public class filesCreat {
public static void main(String[] args) {
filesCreat files = new filesCreat();
files.m1();
files.m2();
}
public void m1(){//判斷檔案是否存在,若是存在,則洗掉
String filePath = "F:\\test/news.txt";
File file = new File(filePath);
if (file.exists()){
System.out.println("檔案存在,進行洗掉");
if (file.delete()){
System.out.println("洗掉成功!");
}else {
System.out.println("洗掉失敗!");
}
}else {
System.out.println("檔案不存在");
}
}
public void m2(){//判斷目錄是否存在,若是存在,則洗掉
String filePath = "F:\\test";
File file = new File(filePath);
if (file.exists()){
System.out.println("該目錄存在,進行洗掉");
if (file.delete()){
System.out.println("洗掉成功!");
}else {
System.out.println("洗掉失敗!");
}
}else {
System.out.println("目錄不存在");
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/298236.html
標籤:Java
下一篇:JVM(一)類加載器與類加載程序
