檔案以及資料夾的建立。目錄下所有檔案和子目錄查詢
File file = new File(destFileName);
if(file.exists()) {
System.out.println("建立單個檔案" + destFileName + "失敗,目標檔案已存在!");
return false;
}
if (destFileName.endsWith(File.separator)) {
System.out.println("建立單個檔案" + destFileName + "失敗,目標檔案不能為目錄!");
return false;
}
//判斷目標檔案所在的目錄是否存在
if(!file.getParentFile().exists()) {
//如果目標檔案所在的目錄不存在,則建立父目錄
System.out.println("目標檔案所在目錄不存在,準備建立它!");
if(!file.getParentFile().mkdirs()) {//建立目錄並且返回建立結果
System.out.println("建立目標檔案所在目錄失敗!");
return false;
}
}
//建立目標檔案
try {
if (file.createNewFile()) { //當且僅當不存在具有此抽象路徑名指定名稱的檔案時,不可分地建立一個新的空檔案
System.out.println("建立單個檔案" + destFileName + "成功!");
return true;
} else {
System.out.println("建立單個檔案" + destFileName + "失敗!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("建立單個檔案" + destFileName + "失敗!" + e.getMessage());
return false;
}
}
public static boolean createDir(String destDirName) {
File dir = new File(destDirName);
if (dir.exists()) {
System.out.println("建立目錄" + destDirName + "失敗,目標目錄已經存在");
return false;
}
if (!destDirName.endsWith(File.separator)) {
destDirName = destDirName + File.separator;
}
//建立目錄
if (dir.mkdirs()) { //mkdirs()可以建立多級資料夾, mkdir()只會建立一級的資料夾
System.out.println("建立目錄" + destDirName + "成功!");
return true;
} else {
System.out.println("建立目錄" + destDirName + "失敗!");
return false;
}
}
public static String createTempFile(String prefix, String suffix, String dirName) {
File tempFile = null;
if (dirName == null) {
try{
//在預設資料夾下建立臨時檔案
tempFile = File.createTempFile(prefix, suffix); // 在指定目錄中建立一個新的空檔案,使用給定的字首和字尾字串生成其名稱
//返回臨時檔案的路徑
return tempFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
System.out.println("建立臨時檔案失敗!" + e.getMessage());
return null;
}
} else {
File dir = new File(dirName);
//如果臨時檔案所在目錄不存在,首先建立
if (!dir.exists()) {
if (!createDir(dirName)) {
System.out.println("建立臨時檔案失敗,不能建立臨時檔案所在的目錄!");
return null;
}
}
try {
//在指定目錄下建立臨時檔案
tempFile = File.createTempFile(prefix, suffix, dir);
return tempFile.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
System.out.println("建立臨時檔案失敗!" + e.getMessage());
return null;
}
}
}
public static void main(String[] args) {
//建立目錄
String dirName = "G:/test";
createDir(dirName);
//建立檔案
String fileName = dirName + "/test1/test1.txt";
createFile(fileName);
//建立臨時檔案
String prefix = "temp";
String suffix = ".txt";
for (int i = 0; i < 10; i++) {
System.out.println("建立了臨時檔案:"
+ createTempFile(prefix, suffix, dirName));
}
//在預設目錄下建立臨時檔案
for (int i = 0; i < 10; i++) {
System.out.println("在預設目錄下建立了臨時檔案:"
+ createTempFile(prefix, suffix, null));
}
}
//顯示目錄下所有子目錄和檔案
public static void tree(File f){
//判斷傳入物件是否為一個資料夾物件
if(!f.isDirectory()){//判斷是否是目錄
System.out.println("你輸入的不是一個資料夾,請檢查路徑是否有誤!!");
}
else{
File[] t = f.listFiles();
for(int i=0;i<t.length;i++){
//判斷檔案列表中的物件是否為資料夾物件,如果是則執行tree遞迴,直到把此資料夾中所有檔案輸出為止
if(t[i].isDirectory()){//目錄
System.out.println("package\t"+t[i].getName());
tree(t[i]);
}
else{
System.out.println("File\t"+t[i].getName());
}
}
}
}
相關推薦
檔案以及資料夾的建立。目錄下所有檔案和子目錄查詢
public static boolean createFile(String destFileName) { File file = new File(destFileName); if(file.exists()) {
顯示當前目錄下檔案以及資料夾的GUI
跟著《Python 核心程式設計》照著做了一個能夠顯示當前目錄的GUI小工具。 底層是呼叫的python的OS包 1 from tkinter import * 2 import os 3 from time import sleep 4 5 class Dir
批處理解決實際問題1——將目錄下所有檔案(*.cpp)分別建立同名資料夾並移入其中
問題描述: 學習OpenGL時,從網上下載了一些原始碼,解壓後發現所有.cpp檔案在同一個目錄下,這樣直接一個一個全部編譯的話,就亂套了,雖然不影響結果但不利於管理。於是,我需要寫一個程式將所有*.cpp檔案放入到同名檔案加下。 解決方案: 毫無疑問,採用批處理技術。思路
Java實現檔案以及資料夾的建立和刪除
1,檔案建立 /** * 建立指定路徑檔案 * * @param filePath:指定路徑(包括檔名的絕對路徑) */ public static void createFile(String filePath) { File file = new File(
Linux下用ls和du命令檢視檔案以及資料夾大小 (轉載)
ls的用法 ls -l |grep "^-"|wc -l或find ./company -type f | wc -l 檢視某資料夾下檔案的個數,包括子資料夾裡的。 ls -lR|grep "^-"|wc -l 檢視某資料夾下資料夾的個數,包括子資料夾裡的
ava刪除目錄及目錄下所有檔案、資料夾(非遞迴)
方法一: public static void deleteFiles(File srcFile) { if (srcFile.exists()) { File[] files; //存放資料夾 Lin
遞迴實現任意目錄,列出檔案以及資料夾
import java.io.File; import java.util.List; import java.util.Vector; /** * @author QSJ * @date 2018-03-11 * @describe 遞迴實現輸入任意目錄,列出檔案以及資料夾 */ pub
Linux下用ls和du命令檢視檔案以及資料夾大小
ls -l |grep "^-"|wc -l或find ./company -type f | wc -l 檢視某資料夾下檔案的個數,包括子資料夾裡的。 ls -lR|grep "^-"|wc -l 檢視某資料夾下資料夾的個數,包括子資料夾裡的。 ls -lR|grep "^d"|wc -l 說明:
Java-讀取某個目錄下所有檔案、資料夾和3種從檔案路徑中獲取檔名的方法
1 讀取某個目錄下所有檔案、資料夾 public static ArrayList<String> getFiles(String path) { ArrayList<Str
Java遞迴實現輸入任意目錄,列出檔案以及資料夾
import java.io.File; import java.util.ArrayList; import java.util.Scanner; public class IOPractice { public static ArrayList&l
python檢視當前目錄下所有檔案和資料夾
檢視當前目錄所有檔案:import os print(os.listdir('.'))檢視當前目錄所有資料夾:import os path = os.listdir(os.getcwd()) for p in path: if os.path.isdir(p):
Android 拷貝assets目錄下所有檔案及資料夾到指定路徑
專案中需要將一些使用NDK編譯的so庫檔案放到assets目錄打包。因為所有so檔案是按照目錄存放和查詢載入的,libs路徑打包有問題,就考慮放到assets下,啟動的時候進行拷貝。從網路上找到一個拷貝的類,做了些小的修改,以滿足拷貝所有檔案及資料夾的需求。 /*****
C# 刪除指定檔案以及資料夾下的所有檔案的方法
public void DeleteDir(string aimPath) { try { // 檢查目標目錄是否以目錄分割字元結束如果不是則新增之
不需要進控制器就訪問的檔案以及資料夾
RewriteEngine on 過濾 可以 不進控制器訪問的字尾名以及資料夾 RewriteCond $1 !^((.*).php|(.*).rar|(.*).zip|(.*).js|(.*).txt|(.*).csv|ct|f|phpmyadmin|test|mp|templat
工作專案看門狗(記錄專案檔案以及資料夾的改動)
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2018/9/18 18:06 # @Author : qhh # @Site : # @File : pywatchdog.py
Go實戰--golang中檔案以及資料夾路徑相關操作
生命不止,繼續 go go go!!! 之前介紹過golang的標準庫:path/filepath, os 今天就跟大家分享幾個關於檔案以及資料夾的相關操作。 獲取目錄中所有檔案 使用包: io/ioutil 使用方法: ioutil.Re
每日學點python之十(檔案以及資料夾操作)
open() 方法 Python open() 方法用於開啟一個檔案,並返回檔案物件,在對檔案進行處理過程都需要使用到這個函式,如果該檔案無法被開啟,會丟擲 OSError。 注意:使用 open() 方法一定要保證關閉檔案物件,即呼叫 close() 方法。 open() 函式常用
adb命令複製檔案或資料夾到system目錄
我是將 F:\android目錄下的Test資料夾(含有Test.apk檔案) 複製到system\priv-app目錄下 首先system目錄是需要許可權的 &nbs
AngularJs專案檔案以及資料夾結構
app/ ----- Libs/ // references for all libs ---------- angular.js ---------- angular-route.js ----- common/ // acts as reusable/shared components or partia
使用python實現帶密碼的百度雲資源自動儲存以及資料夾建立
實現功能 將網盤的密碼自動輸入,得到真實的連結頁面,並得到引數將檔案自動儲存到指定的資料夾,資料夾可以自己建立 只需要提供網盤的連結和密碼,以及自己登陸百度網盤後得到的兩個cookie 可以