當前位置:首頁 » 編程語言 » java創建file

java創建file

發布時間: 2022-05-05 14:41:27

java 怎麼用File建文件「夾」

mkdirs()方法就是創建此抽象路徑名指定的目錄,並包括所有必須但不存在的父目錄。也就是說,比如File
f=new
File("d:\\me\\you.txt");
我們想要在me文件夾下創建you.txt這個文件,但是me這個文件夾本來沒有,所以這時調用f.mkdirs();就直接把me這個文件夾也一起創建了。

㈡ java中創建文件

public void createFile(){

//path表示你所創建文件的路徑
String path = "d:/tr/rt";
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
// fileName表示你創建的文件名;為txt類型;
String fileName="test.txt";
File file = new File(f,fileName);
if(!file.exists()){
try {
file.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
//現在你可以在d:/tr/rt 目錄下找到test.txt文件

㈢ 用java怎麼創建文件往裡面寫入數據

/**
*
重新寫入
*
@param
path
寫入文件路徑
*
@param
str
寫入內容
*
@return
*
@throws
ioexception
*/
public
boolean
writer(string
path,string
str)
throws
ioexception
{
file
file
=
new
file(path);
bufferedoutputstream
bout
=
null;
try
{
outputstream
out
=
new
fileoutputstream(file);
bout
=
new
bufferedoutputstream(out);
bout.write(str.getbytes());
bout.flush();
}
catch
(ioexception
e)
{
e.printstacktrace();
throw
e;
}
finally
{
if
(bout
!=
null)
{
bout.close();
}
}
return
true;
}
調用此方法,將參數設為你要的路徑和內容即可

㈣ java怎麼創建一個指定位置的文件

File
f=new
File("D:/abc/txt.txt");
try
{
f.createNewFile();
}
catch
(IOException
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
但是目錄必須存在否則會報找不到路徑異常,最好先判斷一下目錄是否存在。
也可以用這個方法,2個參數一個路徑,一個文件名File
f=new
File(path,filename);

㈤ java 創建file讀取file

/***主要是輸入流的使用,最常用的寫法*@paramfilePath*@return*/publicstaticStringread(StringfilePath){//讀取txt內容為字元串StringBuffertxtContent=newStringBuffer();//每次讀取的byte數byte[]b=newbyte[8*1024];InputStreamin=null;try{//文件輸入流in=newFileInputStream(filePath);while(in.read(b)!=-1){//字元串拼接txtContent.append(newString(b));}//關閉流in.close();}catch(FileNotFoundExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}finally{if(in!=null){try{in.close();}catch(IOExceptione){//TODOAuto-generatedcatchblocke.printStackTrace();}}}returntxtContent.toString();}

㈥ java 怎樣創建文本文件

可以通過「fileoutputstream」創建文件文本文件,之後過「outputstreamwriter」流的形式進行文件內容存儲,舉例:
outputstreamwriter
pw
=
null;//定義一個流
pw
=
new
outputstreamwriter(new
fileoutputstream(「d:/test.txt」),"gbk");//確認流的輸出文件和編碼格式,此過程創建了「test.txt」實例
pw.write("我是要寫入到記事本文件的內容");//將要寫入文件的內容,可以多次write
pw.close();//關閉流
備註:文件流用完之後必須及時通過close方法關閉,否則會一直處於打開狀態,直至程序停止,增加系統負擔。

㈦ java中創建一個file類對象,意思是在磁碟里創建一個文件嗎

不是,是和硬碟裡面的一個文件/文件夾想關聯。如果所指向的文件不存在,調用file。exist將返回false

㈧ Java File類文件創建

publicclass makedir { publicstaticvoid mkdir(String mkdirName) { try { File dirFile =new File(mkdirName); //mkdirName為傳建文件夾路徑boolean bFile = dirFile.exists(); if( bFile ==true ) { System.out.println("The folder exists."); } else { System.out.println("The folder do not exist,now trying to create a one..."); bFile = dirFile.mkdir(); if( bFile ==true ) { System.out.println("Create successfully!"); System.out.println("創建文件夾"); } else { System.out.println("Disable to make the folder,please check the disk is full or not."); System.out.println(" 文件夾創建失敗,清確認磁碟沒有防寫並且空件足夠");// System.exit(1); } } } catch(Exception err) { System.err.println("ELS - Chart : 文件夾創建發生異常"); err.printStackTrace(); } } }

㈨ 如何在java中創建一個文件夾

File類裡面有兩個方法可以實現:
一個是mkdir():創建此抽象路徑名指定的目錄。
另外一個是mkdirs(): 創建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。

比如你想在A文件夾創建一個B文件夾,並在B文件夾下創建c和D文件夾,可以用下面的代碼實現:

import java.io.File;

public class Test {
public static void main(String args[]) {
File file = new File("D:\\A\\B\\C");
file.mkdirs();

file = new File("D:\\A\\B\\D");
file.mkdir();
}
}

熱點內容
python位元組轉字元串 發布:2025-05-14 07:06:35 瀏覽:420
subplotpython 發布:2025-05-14 06:53:51 瀏覽:661
豎屏大屏導航工廠密碼一般是多少 發布:2025-05-14 06:49:29 瀏覽:806
如何在手機里設置無線網密碼 發布:2025-05-14 06:47:54 瀏覽:120
動態ip文件伺服器 發布:2025-05-14 06:44:22 瀏覽:891
文字分行的腳本有什麼 發布:2025-05-14 06:33:10 瀏覽:288
svn小烏龜怎麼配置 發布:2025-05-14 06:31:43 瀏覽:393
視頻播放器android 發布:2025-05-14 06:31:43 瀏覽:720
android工作室 發布:2025-05-14 06:26:00 瀏覽:658
汽車官方配置表如何下載 發布:2025-05-14 06:21:41 瀏覽:800