導航:首頁 > 文件教程 > file創建文件目錄

file創建文件目錄

發布時間:2022-07-06 17:18:20

㈠ C#創建文件目錄

1、通過Path類的Combine方法可以合並路徑。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");

2、目錄的創建。
創建目錄時如果目錄已存在,則不會重新創建目錄,且不會報錯。創建目錄時會自動創建路徑中各級不存在的目錄。
(1)通過Directory類的CreateDirectory方法創建。
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);

(1)通過DirectoryInfo的對象創建。
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\myDirTwo\mySubDirThree");
di.Create();

3、文件的創建。
通過Create方法創建文件,會覆蓋同名的現有文件。創建文件時,該文件所在路徑的目錄必須存在,否則報錯。
(1)通過File類的Create方法創建。

string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);

//創建一個空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.File.Create(filePathOne);

(2)通過FileInfo對象創建。

//通過Combine合並目錄
//然後創建目錄
string activeDir = @"C:\myDir";
string newPath = System.IO.Path.Combine(activeDir, "mySubDirOne");
System.IO.Directory.CreateDirectory(newPath);

//創建一個空白文件
string fileNameOne = DateTime.Now.ToString("yyyyMMddHHmmssffff")
+ ".txt";
string filePathOne = System.IO.Path.Combine(newPath, fileNameOne);
System.IO.FileInfo fi = new System.IO.FileInfo(filePathOne);
fi.Create();

復制目錄文件

//復制單個文件到指定目錄
string fileName = "test.txt";
string sourcePath = @"C:\testDir\subTestDir";
string targetPath = @"C:\testDir\subTestDirTwo";

string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

if (!System.IO.Directory.Exists(targetPath))
System.IO.Directory.CreateDirectory(targetPath);

//如果已存在,參數為false時將報錯,參數為true重寫該文件
//當方法為兩個參數時,默認重寫為false。
System.IO.File.Copy(sourceFile, destFile, true);

//以下為復制一個目錄下所有文件到指定目錄
//如果復制有子目錄的目錄的所有文件,可以用遞歸或堆棧演算法實現
if (System.IO.Directory.Exists(sourcePath))
{
string[] files = System.IO.Directory.GetFiles(sourcePath);

foreach (string s in files)
{
//僅返迴路徑字元串的文件名及後綴
fileName = System.IO.Path.GetFileName(s);
destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}

}

移動目錄和文件

/*移動文件*/
string sourceFile = @"C:\testDir\subTestDir\test.txt";
string destFile = @"C:\testDir\subTestDirTwo\test.txt";
//當目標文件存在時,拋出異常
System.IO.File.Move(sourceFile, destFile);

/*移動目錄*/
//移動目錄將移動改目錄的子目錄和文件
System.IO.Directory.Move(@"C:\testDir\subTestDirTwo\", @"C:\testDir\subTestDir");

刪除目錄和文件
1、刪除目錄
刪除目錄,如果該目錄不存在,會拋出異常。可以通過File類的Delete方法刪除目錄,也可以通過FileInfo對象方法刪除目錄。
(1)通過 File類的Delete方法刪除目錄

//刪除可寫空目錄
//如果不為空拋出目錄不為空異常
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir");
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

//第二參數為false時,只能刪除空目錄,否則拋出不為空異常
//第二參數為true時,刪除目錄,包括子目錄和文件
try
{
System.IO.Directory.Delete(@"C:\testDir\subTestDir", true);
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

(2)通過FileInfo對象方法刪除目錄

System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\testDir\subTestDirTwo");
try
{
//無參數刪除空目錄
//當參數為false,可刪除空目錄;為true,刪除目錄,包括子目錄和文件
di.Delete(true);
}
catch (System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

2、刪除文件
刪除文件時如果指定文件的目錄存在,而文件不存在,則不會拋出異常,如果指定文件的目錄不存在,則會拋出異常。
(1)通過File類Delete方法刪除文件

try
{
System.IO.File.Delete(@"C:\testDir\subTestDir\test.txt");
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

(2)通過FileInfo對象Delete方法刪除文件

System.IO.FileInfo fi = new System.IO.FileInfo(@"C:\testDir\subTestDir\test1.txt");
try
{
fi.Delete();
}
catch(System.IO.IOException e)
{
Console.WriteLine(e.Message);
}

java中file怎麼在指定的文件夾中創建多個文件

具體的創建方法參照下面的實例:

public class FileTest {

publicstaticvoidmain(String[]args){
//根據系統的實際情況選擇目錄分隔符(windows下是,linux下是/)
Stringseparator=File.separator;
Stringdirectory="myDir1"+separator+"myDir2";
//以下這句的效果等同於上面兩句,windows下正斜杠/和反斜杠都是可以的
//linux下只認正斜杠,為了保證跨平台性,不建議使用反斜杠(在java程序中是轉義字元,用來表示反斜杠)
//Stringdirectory="myDir1/myDir2";
StringfileName="myFile.txt";
//在內存中創建一個文件對象,注意:此時還沒有在硬碟對應目錄下創建實實在在的文件
Filef=newFile(directory,fileName);
if(f.exists()){
//文件已經存在,輸出文件的相關信息
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
}else{
//先創建文件所在的目錄
f.getParentFile().mkdirs();
try{
//創建新文件
f.createNewFile();
}catch(IOExceptione){
System.out.println("創建新文件時出現了錯誤。。。");
e.printStackTrace();
}
}

}

}

㈢ file類型中定義了什麼方法來創建一級目錄

String strPath = "";//要創建文件路徑 File objFile = new File(strPath);//邏輯文件 if(objFile.exist()){//如果已經存在 objFile.delete();//刪除之 } objFile.createNewFile();//物理磁碟上創建文件

㈣ linux如何創建文件夾

假設我們在/home里創建1、創建一個叫test的文件夾輸入 cd /home 回車 就到了home目錄;輸入 mkdir test 就可以了。2、在文件夾里添加(就是創建 一個文件,例如a.txt)輸入 touch test/a.txt 回車。3、刪除輸入 rm -rf test/ 回車。(4)file創建文件目錄擴展閱讀:一、LINUX通用命令:1.date :print or set the system date and time2. stty -a: 可以查看或者列印控制字元(Ctrl-C, Ctrl-D, Ctrl-Z等)3. passwd: print or set the system date and time (用passwd -h查看)4. logout, login: 登錄shell的登錄和注銷命令5. pwd: print working directory6. more, less, head tail: 顯示或部分顯示文件內容.7. lp/lpstat/cancel, lpr/lpq/lprm: 列印文件.8. 更改文件許可權: chmod u+x...9. 刪除非空目錄:rm -fr dir10. fg jobid :可以將一個後台進程放到前台。Ctrl-z 可以將前台進程掛起(suspend), 然後可以用bg jobid 讓其到後台運行。job & 可以直接讓job直接在後台運行。11. kill 的作用: send a signal to a process. eg: kill -9 發送的是SIG_KILL信號。。。 具體發送什麼信號 可以通過 man kill 查看。12. ps 的用法, ps -e 或 ps -o pid,ppid,session,tpgid, comm (其中session顯示的sessionid, tpgid顯示前台進程組id, comm顯示命令名稱。)參考資料:LINUX命令-網路
創建文件夾使用【mkdir x】命令;創建文件使用【touch x/a.txt】命令;刪除文件夾使用【rm -rf x/】命令 。以下是詳細介紹:1、創建一個叫test的文件夾;輸入【cd /home】回車 就到了home目錄;輸入【mkdir test】就可以了;2、在文件夾里添加(就是創建 一個文件 例如a.txt);輸入【touch test/a.txt】回車;3、刪除;輸入【rm -rf test/】回車。
Linux常用命令 1、Linux文件的復制、刪除和移動命令 ·cp復制,相當於dos中"" 用法:# cp [選項] 源文件或目錄 目標文件或目錄<Enter> 常用參數: -i interactive,詢問模式。覆蓋前是否詢問 -r recursive,當復制原文件夾是一個目錄文件, 目標必須為目錄名 # cp -i install.log /tmp<Enter> // 正常 # cp -i install.log /tmp<Enter> // 詢問,<y>覆蓋 # cp -r /root /tmp<Enter> // 復制文件夾 ·mv移動、重命名,相當於dos中"move" 用法: # mv [選項] 源文件或目錄 目標文件或目錄<Enter> -i interactive,詢問模式. -f force,強制操作,不詢問. # mv /tmp/install.log .<Enter> // 目標存在,移動 # mv /tmp/install.log ./i3<Enter> // 目標不存在,重命名 # cp i3 /tmp<Enter> # mv -i i3 /tmp<Enter> // 詢問 # mv -f a*.* /tmp/i3<Enter> // 不詢問 ·rm(remove)刪除文件、文件夾,相當於dos中"del" 用法: # rm [選項] 文件<Enter> -i、-r、-f 同上."-r"主要針對文件夾,將全部目錄和子目錄遞歸地刪除 # mkdir folder<Enter> // 創建文件夾 # touch folder/{a,b,c}<Enter> // 生成abc三個文件 # ls folder<Enter> // 確認3個文件 # rm -i folder/a<Enter> // 刪除folder下a文件 # ls folder<Enter> // 確認少了a文件 # rm -rf folder<Enter> // 刪除文件夾2、Linux目錄的創建與刪除命令 ·mkdir創建文件夾,DOS同 用法:# mkdir [選項] 文件夾名<Enter> -p parents,父母.當父文件夾不存在時,先創建父文件夾 # mkdir /tmp/haha<Enter> ==> # file /tmp/haha<Enter> # mkdir -p /tmp/a/b/c/d<Enter> ==> # file /tmp/a/b/c/d<Enter> # mkdir /tmp/{x,y,z}<Enter> ·rmdir刪除空文件夾,DOS同 用法: # rmdir [選項] 文件夾名<Enter> # rmdir /tmp/a/b/c/d<Enter> // 成功 # rmdir /tmp/a/b<Enter> // 不成功 ·cd更改目錄,DOS同 用法:# cd [文件夾]<Enter> - // 當前目錄與上一次工作目錄切換 ~ // 用戶主目錄 # cd -<Enter> ==> # pwd<Enter> # cd ~<Enter> ==> # pwd<Enter>

01:01

㈤ java中關於IO關於File對像創建文件夾的問題

看源代碼會發現mkdirs會創建指定的目錄,包括所不存在的父目錄

public boolean mkdirs() {
if (exists()) {
return false;
}
if (mkdir()) {
return true;
}
File canonFile = null;
try {
canonFile = getCanonicalFile();
} catch (IOException e) {
return false;
}

File parent = canonFile.getParentFile();
return (parent != null && (parent.mkdirs() || parent.exists()) &&
canonFile.mkdir());
}
至於失敗的原因,你把源碼完整貼出來看看

㈥ JAVA用File創建一個多級目錄a/b/c/d/e/f,然後在每一個目錄裡面添加一些文件和目錄

以下為一些基本操作

importjava.io.*;

publicclassTest{

publicstaticvoidmain(String[]args)throwsIOException{
Filefile=newFile("D:/test/a/b/c/d");
if(!file.exists()){
//創建文件夾,上級目錄不存在時自動創建,使用file.mkdir()方法時上級目錄不存在會拋異常
file.mkdirs();
}

Filefile2=newFile("D:/test/a/b/c/d/test.txt");
if(!file2.exists()){
//在D:/test/a/b/c/d/下創建一個新文件
file2.createNewFile();
}

Filefile3=newFile("D:/test/a/b/c/c-child");
if(!file3.exists()){
//在D:/test/a/b/c/下創建一個新文件夾c-child
file3.mkdir();
}

//在D盤根目錄下創建一個文件test.txt並寫入一下內容
//將D:/test.txt復制到D:/test/a/b/c/下並重命名為.txt
File(newFile("D:/test.txt"),newFile("D:/test/a/b/c/.txt"));
}

/**
*文件復制
*
*@paramsource源文件
*@paramtarget目標路徑
*@throwsIOException
*/
publicstaticvoidFile(Filesource,Filetarget)throwsIOException{
try(FileInputStreamins=newFileInputStream(source);
FileOutputStreamout=newFileOutputStream(target)){
byte[]b=newbyte[1024];
intn;
while((n=ins.read(b))!=-1){
out.write(b,0,n);
}
}
}
}

㈦ 創建了一個file對象,就會在某個物理路徑下創建一個文件或目錄嗎

那是肯定的,不過是在邏輯路徑下

㈧ 在linux下怎麼創建一個目錄

通過linux的mkdir命令來創建文件路徑。

操作步驟:通過cd命令進入要創建自路徑的文件夾,通過以下方回法創建:

方法一通過命答令:mkdir - p {filename1,filename2,filename3,filename4}(文件名)實現。

㈨ 怎樣創建新文件夾

1、首復先回到電腦的桌面制,右鍵滑鼠隨意點擊電腦桌面的空白處。

㈩ Android中File創建文件和文件夾的問題

  1. 是的

  2. 是的

  3. 返回的不是路徑,是創建的目錄對應的File對象

  4. File對象可以理解為一個實體文件的抽象屬性集合,比如文件名,是否目錄等

閱讀全文

與file創建文件目錄相關的資料

熱點內容
al創世者電影完整版免費2023 瀏覽:303
小說電影免費網站有哪些 瀏覽:567
應城市網站到期怎麼續費 瀏覽:772
360擴展器固件在哪升級 瀏覽:103
青春愛情激情電影 瀏覽:209
韓國電影 女主角在療養院工作 瀏覽:926
javascriptsidebar 瀏覽:769
義烏用什麼app坐公交 瀏覽:14
矩陣縱橫cad文件下載 瀏覽:832
四個字的動畫片 瀏覽:461
js最新皮膚2017 瀏覽:587
恐怖電影無限流小說 瀏覽:178
主人公葉天的小說 瀏覽:834
dede採集基礎教程 瀏覽:487
牌技百度雲網盤文件 瀏覽:306
韓國大寸度電影大全美容院 瀏覽:678
網上自學編程怎麼樣 瀏覽:609
傳文件給領導叫什麼 瀏覽:937
225升級235 瀏覽:613
電影院和男朋友親熱 瀏覽:392

友情鏈接