1、文件對象
文件和文件夾都是用File代表
①使用絕對路徑或者相對路徑創建File對象
public class Test01 {
public static void main(String[] args) {
// 絕對路徑
File f1 = new File("d:/LOLFolder");
System.out.println("f1的絕對路徑:" + f1.getAbsolutePath());
// 相對路徑,相對於工作目錄,如果在eclipse中,就是項目目錄
File f2 = new File("LOL.exe");
System.out.println("f2的絕對路徑:" + f2.getAbsolutePath());
// 把f1作為父目錄創建文件對象
File f3 = new File(f1, "LOL.exe");
System.out.println("f3的絕對路徑:" + f3.getAbsolutePath());
}
}
運行:
f1的絕對路徑:d:\LOLFolder
f2的絕對路徑:E:\project\Study\LOL.exe
f3的絕對路徑:d:\LOLFolder\LOL.exe
②文件常用方法:
訪問文件名或路徑:
1)String getName() 返回File對象所表示的文件名或文件路徑
2)String getPath() 返回File對象所對應的相對路徑名。
3)File getAbsoluteFile() 返回File對象的絕對路徑文件
4)String getAbsolutePath() 返回File對象所對應的絕對路徑名
5)String getParent() 返回File對象所對應目錄的父目錄
6) boolean renameTo(File dest) 重命名File對象的文件或目錄
文件檢測:
1boolean exists() 判斷File對象的文件或目錄是否存在
2)bool canWrite() 判斷File對象是否可寫
3)boolean canRead()判斷File對象是否可讀
4)boolean isDirectory() 判斷File對象是否是目錄
5)boolean isFile() 判斷File對象是否是文件
6)boolean isAbsolute() 判斷File對象是否采用絕對路徑
文件信息:
1)long length() ; File對象對應文件的長度
2)long lastNodified() File對象最后修改的時間
文件操作:
1)boolean createNewFile() ; 檢查文件是否存在,當文件不存在時創建一個新的文件
2) boolean delete() 刪除File對象所對應的文件或目錄
目錄操作:
1)boolean mkdir() 創建文件夾,如果父文件夾skin不存在,創建就無效
2)boolean f.mkdirs(); 創建文件夾,如果父文件夾skin不存在,就會創建父文件夾
3)String[] list() 列出File對象所有的子文件名和路徑名
4)File[] listFile() 列出File對象的所有子文件或路徑
5)static File[] listRoots() 列出系統所有的根路徑
2、字節流、字符流
OutputStream字節輸出流,抽象類
FileOutputStream 是OutputStream子類
InputStream字節輸入流,抽象類
FileInputStream是InputStream子類
Reader字符輸入流 ,抽象類
Writer字符輸出流 ,抽象類
FileReader 是Reader子類
FileWriter 是Writer子類
簡單的輸入、輸出操作:
public class Test02 {
public static void main(String[] args) {
// TODO Auto-generated method stub
FileReader fr = null;
FileWriter fw = null;
try {
fr = new FileReader(new File("d:\\text\\st01.txt"));
fw = new FileWriter(new File("d:\\text\\st02.txt"));
char[] c = new char[2];
int n = 0;
while ((n = fr.read(c)) != -1) {
String s = new String(c, 0, n);
System.out.println(s);
fw.write(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (fr != null)
fr.close();
if(fw!=null)
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
標准的關閉流方式:
File f = new File("d:/lol.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(f);
byte[] all = new byte[(int) f.length()];
fis.read(all);
for (byte b : all) {
System.out.println(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 在finally 里關閉流
if (null != fis)
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
3、緩沖流
緩沖流在寫入數據的時候,會先把數據寫入到緩存區,直到緩存區達到一定的量(或關閉流),才把這些數據,一起寫入到硬盤中去。
按照這種操作模式,就不會像字節流,字符流那樣每寫一個字節都訪問硬盤,從而減少了IO操作
緩存流必須建立在一個存在的流的基礎上
緩存字符輸入流 BufferedReader 可以一次讀取一行數據 String readLine()
PrintWriter 緩存字符輸出流, 可以一次寫出一行數據 viod println()
有的時候,需要立即把數據寫入到硬盤,而不是等緩存滿了才寫出去。 這時候就需要用到void flush ()方法
public class Test03 {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
PrintWriter pw = null;
try {
// 准備文件lol.txt其中的內容是
// garen kill teemo
// teemo revive after 1 minutes
// teemo try to garen, but killed again
br = new BufferedReader(new FileReader(new File("d:\\text\\Student.txt")));
pw = new PrintWriter(new BufferedWriter(new FileWriter("d:\\text\\st02.txt")));
String s = "";
//每次讀取一行 String readLine()
while ((s = br.readLine()) != null) {
System.out.println(s);
//每次寫入一行 viod println()
pw.println(s);
//強制把緩存中的數據寫入硬盤,無論緩存是否已滿
pw.flush();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
//判斷並關閉流
if(br!=null)
br.close();
if(pw!=null)
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
運行:
garen kill teemo
teemo revive after 1 minutes
teemo try to garen, but killed again