標籤:direct bsp byte 檔案名稱 rect 面向 中文 anr 建立
看檔案的屬性:
System.out.println(f.getName());//檔案名稱
System.out.println(f.length());//位元組大小
System.out.println(f.getAbsolutePath());//絕對路徑
System.out.println(f.canRead());//檔案是否可讀
System.out.println(f.canWrite());//是否可寫
System.out.println(f.isDirectory()); //是否是檔案夾
檔案相關知識:
File f = new File("d:/java/aaa.txt"); //找到目標檔案
//建立檔案讀取對象
//針對於檔案的 輸入位元組流
FileInputStream fis = new FileInputStream(f);
//讀取檔案
//byte[] b = new byte[(int)f.length()];
//如果讀到最後 -1表示無法再去讀取
//普通寫法
//int i = 0;
//while((i = fis.read())!=-1){
//System.out.println((char)i);
//}
//進階寫法
byte[] b = new byte[(int)f.length()];
//講檔案中所有的內容 讀取到byte數組裡
fis.read(b, 0, b.length);
for(int i = 0; i < b.length;i++){
System.out.println((char)b[i]);
}
//寫
//針對於檔案的輸出位元組流
FileOutputStream fos = new FileOutputStream("c:/bbb.txt");
//輸入
fos.write(b);
fis.close();
//將記憶體清空
fos.flush();
fos.close();
//將位元組流封裝成字元流
//字元集 美國字元集ISO-8859-1
//賦予一個編碼格式 中文的字元集utf-8
InputStreamReader isr = new InputStreamReader(fis,"utf-8");
使用Java實現物件導向編程(10)