新建文件
/* 下面是在當(dāng)前文件目錄下(即工程根目錄下)創(chuàng)建hello.txt文件 */
File file = new File('hello.txt');
判斷是文件還是目錄
file.isFile() /* file是文件 */
file.isDirectory() /* file是目錄 */
判斷文件是否存在
file.exsits()
文件的 創(chuàng)建丧凤、刪除、重命名
針對文件的重命名:新建文件2,將文件1的文件名重命名為文件2的文件名,可達(dá)到文件移動(dòng)的目的
注意:對于文件的移動(dòng)困介,文件必須處于同一分區(qū),例如某文件在C盤中移動(dòng)蘸际,而不能從C盤移動(dòng)到D盤座哩。如果要移動(dòng)到不同的分區(qū),需要使用文件的拷貝粮彤,而不是重命名根穷。
file.createNewFile(); /* 創(chuàng)建文件 */
file.delete(); /* 刪除文件 */
File nameTo = new File("new hello.txt");
file.renameTo(nameTo); /* 將file文件重命名 */
file類既可以表示文件也可以表示文件夾
/* 創(chuàng)建一個(gè)文件對象 */
File folder = new File("my folder");
/* 創(chuàng)建一個(gè)文件夾 */
folder.mkdir(); /* 返回true 代表文件創(chuàng)建成功 返回false 代表文件創(chuàng)建失敗 */
folder.mkdirs();
mkdir和mkdirs的區(qū)別:
????mkdir:只能創(chuàng)建一級(jí)文件夾姜骡,上級(jí)文件夾必須存在
????mkdirs:能夠創(chuàng)建多級(jí)文件夾,不要求上級(jí)文件夾必須存在
文件夾的重命名:文件夾的重命名必須處于同一分區(qū)當(dāng)中
File folder = new File("my new folder");
File newFolder = new File("my new folder-new");
folder.renameTo(newFolder);
文件夾的刪除:delete只能刪除空文件夾
File folder = new File("my new folder-new/one/two/three");
folder.delete() /* 返回true 刪除成功 返回false 刪除失敗 */
文件屬性的讀取
File file = new File("test.txt");
// 判斷文件是否存在
file.exsits()
// 讀取文件名稱
file.getName()
// 讀取文件路徑
file.getPath()
// 讀取文件絕對路徑
file.getAbsolutePath()
// 讀取文件父級(jí)路徑(file是相對路徑屿良,如何利用相對路徑獲取其上一級(jí)路徑呢圈澈?)
new FIle(file.getAbsolutePath()).getParent()
// 讀取文件大小(file.length()方法的返回是按照字節(jié)來返回的,尘惧,返回類型是long類型)
// 在硬盤上康栈,字節(jié)轉(zhuǎn)化為KB是除以1000
(long)file.length()/1000 + "KB"
// 判斷文件是否被隱藏
file.isHidden();
// 判斷文件是否可讀
file.canRead();
// 判斷文件是否可寫
file.canWrite();
// 判斷文件是否為文件夾
file.isDirectory();
文件屬性的設(shè)置
// 將文件設(shè)定為可寫(傳入?yún)?shù):true 設(shè)定為可寫,false 設(shè)定為不可寫)
file.setWritable(true);
// 將文件設(shè)定為可讀(傳入?yún)?shù):true 設(shè)定為只讀喷橙,false 設(shè)定為不可讀)
file.setReadable(true);
// 將文件設(shè)定為只讀
file.setReadOnly();
遍歷文件夾
public class OutputDirectory {
public static void main(String[] args) {
printFiles(new File("my new folder-new/one/two/three"), 1);
}
public static void pritFiles(File dir, int tab) {
if(dir.isDirectory()) {
// 包含當(dāng)前目錄當(dāng)中所有的子文件夾和子文件
File next[] = dir.listFIles();
for(int i = 0; i < next.length(); i++) {
for(int j = 0; j < tab; j++) {
System.out.print("|--");
}
System.out.println( next[i].getName());
if(next[i].isDirectory()) {
printFIles(next[i], tab++);
}
}
}
}
}
文件的簡單讀寫
public class ReadFile {
public static void main(String[] args) {
File file = new File("test.txt");
if (file.isExits()) {
System.out.println("file is exist");
try {
// 1啥么、創(chuàng)建文件輸入流 獲取文件輸入流
FileInputStream fis = new FileInputStream(file);
// 2、將輸入流包裝成InputStreamReader
// fis是一個(gè)字節(jié)流贰逾,isr是一個(gè)字符流悬荣,在進(jìn)行轉(zhuǎn)換時(shí)容易出現(xiàn)亂碼,所有需要添加utf-8編碼似踱,防止亂碼的出現(xiàn)
InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
// 3隅熙、創(chuàng)建一個(gè)帶有 緩沖區(qū) 的reader
BufferedReader br = new BufferedReader(isr);
// 讀取 文件內(nèi)容并輸出
String line;
while((line = br.readLine()) != null) {
System.out.println(line);
}
br.close();
isr.close();
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
File newFile = new File("newtext.txt");
FileOutputStream fos = new FileOutputStream(newFile);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
BufferedWriter bw = new BufferedWriter(osw);
bw.write("我正在寫出文件內(nèi)容至文件當(dāng)中\(zhòng)n");
bw.writer("我正在書寫內(nèi)容\n");
// 關(guān)閉輸出流, 先打開的后關(guān)閉核芽,后打開的先關(guān)閉
fos.close();
osw.close();
bw.close();
} catch(FileNotFoundException e) {
e.printStackTrace();
} catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}