標籤:lis des let obj bbb 表示 屬性 參數 tostring
File類
位置於java.io包
構造方法:
File(String parent, String child)
new file(“d:\\”,”a.txt”)
File(String pathname)
new file(“d:\\a.txt”)
File(File parent, String child)
File f = new File(“d:\\”);
File f1=new File(f,”a.txt”)
常用方法:
1.擷取
1)檔案名稱
String getName()
2)檔案路徑
String getAbsolutePath()
3)大小
long length()
3)最後個修改時間
long lastModified()
File file = new File("d:\\aa.txt");
System.out.println(file.getName());
System.out.println(file.getAbsolutePath());
System.out.println(file.lastModified());
long l = file.lastModified();
Date date= new Date(l);
System.out.println(date.toString());
System.out.println(file.length());
2.建立與刪除
1)建立檔案
boolean createNewFile()
2)建立檔案夾(目錄)
boolean mkdir() 單層
boolean mkdirs() 多層
3)刪除檔案和目錄
boolean delete()
File f= new File("d:\\b.txt");
boolean b = f.createNewFile();
System.out.println(b);
f.delete();
File f1= new File("d:\\ch1027");
f1.mkdir();
File f2= new File("d:\\ch1028\\ch1029\\ch1030");
f2.mkdirs();
File f2= new File("d:\\ch1028\\ch1029\\ch1030");
System.out.println(f2.delete());
3.判斷
boolean isDirectory() 測試此抽象路徑名表示的檔案是否是一個目錄。
boolean isFile() 測試此抽象路徑名表示的檔案是否是一個標準檔案。
boolean isHidden() 測試此抽象路徑名指定的檔案是否是一個隱藏檔案。
File f1=new File("d:\\ch1027");
System.out.println(f1.isDirectory());
System.out.println(f1.isFile());
File f= new File("d:\\bb.txt");
boolean b = f.createNewFile();
System.out.println(f.isHidden());
4.重新命名
boolean renameTo(File dest)
1)同目錄 ---- 改名
2)不同目錄 ----- 相當於剪下
File f= new File("f:\\bb6.txt");
File f1= new File("f:\\123\\bb6.txt");
System.out.println(f.renameTo(f1));
5.其它
static File[] listRoots() 列出可用的檔案系統根。
long getFreeSpace() 可用空間
long getTotalSpace() 總容量
String[] list() 列出目錄中的檔案和目錄(同輩目錄)
File[] files = File.listRoots();
for(int i=0;i<files.length;i++){
System.out.println(files[i]);
}
File f= new File("f:\\");
System.out.println(f.getFreeSpace());
System.out.println(f.getTotalSpace());
File f= new File("f:\\123");
String [] strs = f.list();
for(int i=0;i<strs.length;i++){
System.out.println(strs[i]);
}
FileFilter介面
檔案過濾器
例子:顯示出某個目錄下的,非隱藏檔案
File[]listFiles(FileFilter filter)
參數是一個過濾器類
詳見下面程式
public class FilteHidden implements FileFilter{
public boolean accept(File file) {
return !file.isHidden();
}
}
public class Filess {
public static void main(String[] args) {
File file = new File("f:\\123");
File [] s = file.listFiles(new FilteHidden());
for(int i=0;i<s.length;i++){
System.out.println(s[i].getName());
}
}
}
FilenameFilter 介面
檔案名稱過濾器
例子:對檔案名稱進行過濾
File[]listFiles(FilenameFilter filter)
參數是一個過濾器類
詳見下面程式
public class Filename implements FilenameFilter{
private String endstr;
public boolean accept(File dir, String name) {
return name.endsWith(endstr);
}
Filename(String str){
endstr = str;
}
}
public class FilenameDemo {
public static void main(String[] args) {
File file= new File("f:\\123");
File [] files = file.listFiles(new Filename(".txt"));
for(int i=0;i<files.length;i++){
System.out.println(files[i].getName());
}
}
}
File類 得到檔案清單的方法
1)列出所有檔案
File file = new File(“f:\\aa”);
File [] filearr = file.listFiles(); 表示的目錄中的(檔案及目錄)
String [] filearr= file.list(); 表示的目錄中的(檔案及目錄)
2)過濾器
File file = new File(“f:\\aa”);
FilenameFilter介面,用於過濾檔案名稱。
String[] filenamearr= file.list(FilenameFilter filter)
File[] filenamearr = file.listFiles(FilenameFilter filter)
File file = new File(“f:\\aa”);
FileFilter介面,用於過濾檔案。
File[] filearr = file.listFiles(FileFilter filter)
遞迴:自已(方法)調用自已
例子:用遞迴把目錄下所有的目錄及檔案全部顯示出來
public class B {
public static void main(String[] args) {
File file = new File("f:\\123");
listAllFile(file);
}
public static void listAllFile(File file) {
File[] strs = file.listFiles();
for (int i = 0; i < strs.length; i++) {
// 是不是目錄strs[i]
if (strs[i].isDirectory()) {
listAllFile(strs[i]);
System.out.println("目錄="+strs[i].getName());
} else {
System.out.println("檔案名稱="+strs[i].getName());
}
}
}
}
IO流
IO流:輸入(Input)輸出(Output)流
位置於java.io包下
流作用:讀取檔案用的
流分類
1)按流向分(以記憶體為參照物):
輸入資料流 輸出資料流
2)按流的內容分:
位元組流(能讀寫所有檔案),字元流(讀取純文字檔案)
3)按功能分:
節點流 處理流(套在節點流上的)
位元組流,它的子類都是Stream
字元流,它的子類是Writer Reader
FileWriter
檔案字元輸出資料流
構造方法:
注意:1)對象一建立出來就得給檔案路徑。
2)如果檔案存在就覆蓋,不存在則建立
3)不想覆蓋,是用
FileWriter(String fileName, true)
4)寫完後要flush()
5)寫完要關閉流
FileReader
檔案字元輸入資料流
代碼:
1)
FileWriter fw = new FileWriter("d:\\1.txt");//檔案有覆蓋,沒有則建立
fw.write("abc");
fw.flush();
fw.close(); 關閉後不能在寫入
2)
public static void main(String[] args) {
try {
FileReader fr = new FileReader("f:\\1.txt");
int ch=0;
while( (ch=fr.read())!=-1 ){ 讀到最後傳回值為-1
System.out.println((char)ch);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3)
public static void main(String[] args) {
try {
FileReader fr = new FileReader("f:\\1.txt");
char[] buf= new char[3];
int i = fr.read(buf);
System.out.println(new String(buf,0,i));
//new String(buf,0,i) char [] buf,從char數組的第幾個位置開始讀,讀幾個
int i1 = fr.read(buf);
System.out.println(new String(buf,0,i1));
} catch (Exception e) {
e.printStackTrace();
}
}
4)
public class FileWriterDemo {
public static void main(String[] args) {
FileWriter fw = null;
try {
fw = new FileWriter("d:\\1.txt");
fw.write("fff");
fw.write("ggggg");
fw.flush();
int i = 10/0;
} catch (Exception e) {
String str = e.getMessage();
FileWriterDemo d = new FileWriterDemo();
d.xie(str);錯誤資訊寫入另一個檔案中。
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void xie(String str) {
FileWriter fw = null;
try {
fw = new FileWriter("d:\\2.txt", true);
fw.write(str);
fw.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
BufferedWriter 帶緩衝區的檔案字元輸出資料流
特點:1)它是處理流,要包在節點流外面來使用
2)提高寫的效率
3)換行newLine()
建立對象:
1)FileWriter fw = new FileWriter("d:\\cc\\cc.txt");
BufferedWriter bw = new BufferedWriter(fw);
2)沒有捕獲異常時
BufferedWriter bw1 = new BufferedWriter(new FileWriter("d:\\cc\\cc3.txt"));
bw1.write("abcbbbb");
bw1.newLine();
bw1.write("sssss");
bw1.newLine();
bw1.write("defxxx");
bw1.flush();
bw1.close();
BufferedReader 帶緩衝區的檔案字元輸入資料流
特點:1)它是處理流,要包在節點流外面來使用
2)提高讀取的效率
3)讀一行readLine()
建立對象:
try {
BufferedReader br= new BufferedReader(new FileReader("d:\\cc\\cc.txt"));
String str=null;
while( (str=br.readLine()) != null){
System.out.println(str);
}
} catch (Exception e) {
e.printStackTrace();
}
檔案的複製:
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("d:\\cc\\cc.txt")));
bw = new BufferedWriter(new FileWriter("d:\\cc\\cc5.txt"));
String str = null;
while ((str = br.readLine()) != null) {
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileOutputStream 檔案位元組輸出資料流
特點:1)輸出的是位元組
2)不用flush()
建立對象:
public static void main(String[] args) {
FileOutputStream fos=null;
try{
fos = new FileOutputStream("d:\\cc\\cc8.txt");
fos.write("abc".getBytes());
//getBytes()將一個字串轉化為一個位元組數組byte[]的方法
fos.write(97);
}catch (Exception e) {
e.printStackTrace();
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
FileInputStream 檔案位元組輸入資料流
特點:1)輸入的是位元組
2)不用flush()
建立對象:
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("d:\\cc\\cc8.txt");
byte [] b = new byte[2];
int len=0;
while( (len =fis.read(b)) != -1){
System.out.println(new String(b,0,len));
}
} catch (Exception e) {
e.printStackTrace();
}
}
BufferedOutputStream BufferedInputStream
帶緩衝區的位元組輸出(輸入)流
特點:1)輸出(入)的是位元組
2)是個處理流
2)用flush()
建立對象:
public static void main(String[] args) {
try {
BufferedInputStream bis= new BufferedInputStream( new FileInputStream("d:\\cc\\cc8.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("d:\\cc\\cc9.txt"));
int len=0;
byte [] b = new byte[1024];
while( (len = bis.read(b)) !=-1 ){
bos.write(b);
}
bos.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{//加close()方法,請參考(檔案的複製)}
}
System.in
從鍵盤輸入得到一個流InputStream
用InputStream中的方法
public static void main(String[] args) {
InputStream is = System.in;
int ch = 0;
try {
while ((ch = is.read()) != -1) {
System.out.println((char) ch); // \r 13 \n 10
}
} catch (IOException e) {
e.printStackTrace();
}
}
ObjectInputStream ObjectOutputStream
對象的輸入輸出資料流
特點:1)寫入很多資料類型
2)寫入自訂對象
序列化:把對象存入硬碟中(屬性的值)
還原序列化:把對象從硬碟中取出來(屬性的值)
注意: 1)static 修飾的屬性不能存入
2)Transient修飾的屬性不能存入//transient關鍵字的作用:標記的成員變數不參與序列化過程
3)對象對應的類必須要實現一個介面(空介面)Serializable介面
4)不用flush()
5)類中的方法不能被序列化,只能序列化屬性
程式示範:
public static void main(String[] args) {
try {
ObjectOutputStream ous = new ObjectOutputStream(new FileOutputStream(new File("d:\\cc\\Animal.obj")));
Animal a1= new Animal("aa",1);
ous.writeObject(a1);//序列化
ous.close();
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("d:\\cc\\Animal.obj")));
Animal an=(Animal)ois.readObject();//還原序列化
System.out.println(an.getAge()+","+an.getName());
Ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
詳談JAVA中的file類與IO流