File類里面有兩個方法可以實現(xiàn):
創(chuàng)新互聯(lián)建站長期為近千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為建鄴企業(yè)提供專業(yè)的網(wǎng)站建設、成都網(wǎng)站制作,建鄴網(wǎng)站改版等技術服務。擁有10多年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
一個是mkdir():創(chuàng)建此抽象路徑名指定的目錄。
另外一個是mkdirs(): 創(chuàng)建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。
比如你想在A文件夾創(chuàng)建一個B文件夾,并在B文件夾下創(chuàng)建c和D文件夾,可以用下面的代碼實現(xiàn):
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();
}
}
java
是跨平臺的開發(fā)語言,建立文件夾的方式是一樣的
File
file
=
new
File("/usr/local/java");
file.mkdirs();
這樣就行了
具體的創(chuàng)建方法參照下面的實例:
public class FileTest {
public?static?void?main(String[]?args)?{
//?根據(jù)系統(tǒng)的實際情況選擇目錄分隔符(windows下是,linux下是/)
String?separator?=?File.separator;
String?directory?=?"myDir1"?+?separator?+?"myDir2";
//?以下這句的效果等同于上面兩句,windows下正斜杠/和反斜杠都是可以的
//?linux下只認正斜杠,為了保證跨平臺性,不建議使用反斜杠(在java程序中是轉(zhuǎn)義字符,用\來表示反斜杠)
//?String?directory?=?"myDir1/myDir2";
String?fileName?=?"myFile.txt";
//?在內(nèi)存中創(chuàng)建一個文件對象,注意:此時還沒有在硬盤對應目錄下創(chuàng)建實實在在的文件
File?f?=?new?File(directory,fileName);
if(f.exists())?{
//?文件已經(jīng)存在,輸出文件的相關信息
System.out.println(f.getAbsolutePath());
System.out.println(f.getName());
System.out.println(f.length());
}?else?{
//??先創(chuàng)建文件所在的目錄
f.getParentFile().mkdirs();
try?{
//?創(chuàng)建新文件
f.createNewFile();
}?catch?(IOException?e)?{
System.out.println("創(chuàng)建新文件時出現(xiàn)了錯誤。。。");
e.printStackTrace();
}
}
}
}
參考下面代碼,說明已在代碼中注釋:
import?java.io.File;
import?java.io.FileOutputStream;
import?java.io.IOException;
import?java.text.SimpleDateFormat;
import?java.util.Date;
public?class?WriteFile?{
public?static?void?main(String[]?args)?{
writeFile();
}
public?static?void?writeFile(){
SimpleDateFormat?sdf?=?new?SimpleDateFormat("yyyy-MM-dd?HH:mm:ss");
String?content?=?sdf.format(new?Date());
System.out.println("現(xiàn)在時間:"?+?content);
FileOutputStream?out?=?null;
File?file;
try?{
String?rootFile?=?"D:\\tests\\license";
file?=?new?File(rootFile);
if?(!file.exists())?{
/*
file.mkdirs():創(chuàng)建沒有存在的所有文件夾
file.mkdir():創(chuàng)建沒有存在的最后一層文件夾
例如:在硬盤上有D://test?文件夾,但是現(xiàn)在需要創(chuàng)建D://test//license//save,這個時候就需要使用file.mkdirs()而不能使用file.mkdir(),另外這兩個方法都是僅僅能創(chuàng)建文件夾,不能創(chuàng)建文件,即使創(chuàng)建D://test//license//save//systemTime.dat如果使用該方法創(chuàng)建的SystemTime.dat也是一個文件夾?,而不是文件
*/
file.mkdirs();
}
File?fileDat?=?new?File(rootFile?+?"\\systemFile.dat");
/*
if(!fileDat.exists()){
//創(chuàng)建文件?不是文件夾,在程序中這這一步?jīng)]有必要,因為
new?FileOutputStream(fileDat);該語句有創(chuàng)建文件的功能
fileDat.createNewFile();//
}
*/
out?=?new?FileOutputStream(fileDat);
byte[]?contentInBytes?=?content.getBytes();
out.write(contentInBytes);
out.flush();
out.close();
System.out.println("Done");
}?catch?(IOException?e)?{
e.printStackTrace();
}?finally?{
try?{
if?(out?!=?null)?{
out.close();
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
Java項目添加文件夾選項的意思是:你的java代碼寫的不全,必須要把空文件夾的情況寫入才行。
java項目創(chuàng)建包以及調(diào)試運行的方法:
1、首先我們在桌面找到eclipse,雙擊將其打開。
2、在這里我已經(jīng)創(chuàng)建了一個名為helloworld的java項目,但其下還為創(chuàng)建任何的包以及文件。下邊我們開始創(chuàng)建第一個包路徑。一般來說java代碼都是寫在src文件夾下,選中src右擊鼠標創(chuàng)建一個包路徑,選擇new然后選擇package。
3、在創(chuàng)建包路徑界面,我們需要給我們的包進行命名。命名規(guī)則一般為域名.公司名.工程名.模塊名……假如需要建立一個百度地圖的應用包ditu.baidu.com,那我們的包名即為com.baidu.ditu。命名結束后點擊Finish。
4、我們可以看到包的路徑已經(jīng)建好了。
5、接下來我們需要在包里面建立java文件,右擊包名稱,選擇new-〉class。
6、java文件的命名規(guī)則為單詞首字母大寫,如果多個單詞則每個首字母都需要大寫。在新建java文件頁面,我們看到下方有個設置項為public static void main (string[] args),這個選項是對該java類自動創(chuàng)建一個主函數(shù)。我們將其選中,并點擊Finish。
7、可以看到在新創(chuàng)建的這個類中已經(jīng)自動創(chuàng)建了一個主函數(shù),以及類的包路徑都已經(jīng)自動的引入。在左側試圖中可以查看到新創(chuàng)建的java類。
8、那eclipse中如何對java工程進行調(diào)試呢?這里我們通過一個最簡單也是初學者入門就接觸的一個實例叫'hell oworld!'。我們在主程序中添加一行代碼,也是最常見的輸出命令。改行代碼的意思為當程序運行時將‘測試輸出!’輸出到控制臺。
9、接下來我們再java類中右擊鼠標,選擇run as-〉java ?application。
10、接下來我們可以看到‘測試輸出!’的字樣在控制臺打印出來了。這樣一個完整的java調(diào)試就結束了。
public class ReadFromFile {
/**
* 以字節(jié)為單位讀取文件,常用于讀二進制文件,如圖片、聲音、影像等文件。
*/
public static void readFileByBytes(String fileName) {
File file = new File(fileName);
InputStream in = null;
try {
System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
// 一次讀一個字節(jié)
in = new FileInputStream(file);
int tempbyte;
while ((tempbyte = in.read()) != -1) {
System.out.write(tempbyte);
}
in.close();
} catch (IOException e) {
e.printStackTrace();
return;
}
try {
System.out.println("以字節(jié)為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
// 一次讀多個字節(jié)
byte[] tempbytes = new byte[100];
int byteread = 0;
in = new FileInputStream(fileName);
ReadFromFile.showAvailableBytes(in);
// 讀入多個字節(jié)到字節(jié)數(shù)組中,byteread為一次讀入的字節(jié)數(shù)
while ((byteread = in.read(tempbytes)) != -1) {
System.out.write(tempbytes, 0, byteread);
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以字符為單位讀取文件,常用于讀文本,數(shù)字等類型的文件
*/
public static void readFileByChars(String fileName) {
File file = new File(fileName);
Reader reader = null;
try {
System.out.println("以字符為單位讀取文件內(nèi)容,一次讀一個字節(jié):");
// 一次讀一個字符
reader = new InputStreamReader(new FileInputStream(file));
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 對于windows下,\r\n這兩個字符在一起時,表示一個換行。
// 但如果這兩個字符分開顯示時,會換兩次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否則,將會多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
System.out.println("以字符為單位讀取文件內(nèi)容,一次讀多個字節(jié):");
// 一次讀多個字符
char[] tempchars = new char[30];
int charread = 0;
reader = new InputStreamReader(new FileInputStream(fileName));
// 讀入多個字符到字符數(shù)組中,charread為一次讀取字符數(shù)
while ((charread = reader.read(tempchars)) != -1) {
// 同樣屏蔽掉\r不顯示
if ((charread == tempchars.length)
(tempchars[tempchars.length - 1] != '\r')) {
System.out.print(tempchars);
} else {
for (int i = 0; i charread; i++) {
if (tempchars[i] == '\r') {
continue;
} else {
System.out.print(tempchars[i]);
}
}
}
}
} catch (Exception e1) {
e1.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 以行為單位讀取文件,常用于讀面向行的格式化文件
*/
public static void readFileByLines(String fileName) {
File file = new File(fileName);
BufferedReader reader = null;
try {
System.out.println("以行為單位讀取文件內(nèi)容,一次讀一整行:");
reader = new BufferedReader(new FileReader(file));
String tempString = null;
int line = 1;
// 一次讀入一行,直到讀入null為文件結束
while ((tempString = reader.readLine()) != null) {
// 顯示行號
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
* 隨機讀取文件內(nèi)容
*/
public static void readFileByRandomAccess(String fileName) {
RandomAccessFile randomFile = null;
try {
System.out.println("隨機讀取一段文件內(nèi)容:");
// 打開一個隨機訪問文件流,按只讀方式
randomFile = new RandomAccessFile(fileName, "r");
// 文件長度,字節(jié)數(shù)
long fileLength = randomFile.length();
// 讀文件的起始位置
int beginIndex = (fileLength 4) ? 4 : 0;
// 將讀文件的開始位置移到beginIndex位置。
randomFile.seek(beginIndex);
byte[] bytes = new byte[10];
int byteread = 0;
// 一次讀10個字節(jié),如果文件內(nèi)容不足10個字節(jié),則讀剩下的字節(jié)。
// 將一次讀取的字節(jié)數(shù)賦給byteread
while ((byteread = randomFile.read(bytes)) != -1) {
System.out.write(bytes, 0, byteread);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (randomFile != null) {
try {
randomFile.close();
} catch (IOException e1) {
}
}
}
}
/**
* 顯示輸入流中還剩的字節(jié)數(shù)
*/
private static void showAvailableBytes(InputStream in) {
try {
System.out.println("當前字節(jié)輸入流中的字節(jié)數(shù)為:" + in.available());
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
ReadFromFile.readFileByBytes(fileName);
ReadFromFile.readFileByChars(fileName);
ReadFromFile.readFileByLines(fileName);
ReadFromFile.readFileByRandomAccess(fileName);
}
}
復制代碼
5、將內(nèi)容追加到文件尾部
public class AppendToFile {
/**
* A方法追加文件:使用RandomAccessFile
*/
public static void appendMethodA(String fileName, String content) {
try {
// 打開一個隨機訪問文件流,按讀寫方式
RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw");
// 文件長度,字節(jié)數(shù)
long fileLength = randomFile.length();
//將寫文件指針移到文件尾。
randomFile.seek(fileLength);
randomFile.writeBytes(content);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* B方法追加文件:使用FileWriter
*/
public static void appendMethodB(String fileName, String content) {
try {
//打開一個寫文件器,構造函數(shù)中的第二個參數(shù)true表示以追加形式寫文件
FileWriter writer = new FileWriter(fileName, true);
writer.write(content);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
String fileName = "C:/temp/newTemp.txt";
String content = "new append!";
//按方法A追加文件
AppendToFile.appendMethodA(fileName, content);
AppendToFile.appendMethodA(fileName, "append end. \n");
//顯示文件內(nèi)容
ReadFromFile.readFileByLines(fileName);
//按方法B追加文件
AppendToFile.appendMethodB(fileName, content);
AppendToFile.appendMethodB(fileName, "append end. \n");
//顯示文件內(nèi)容
ReadFromFile.readFileByLines(fileName);
}
}
網(wǎng)站欄目:java添加文件夾的代碼,java中如何創(chuàng)建文件夾
文章位置:http://chinadenli.net/article36/hesjpg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站策劃、虛擬主機、小程序開發(fā)、網(wǎng)站改版、企業(yè)網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)