一、 File類
1.用來将檔案或或者檔案夾封裝成對象
2.友善對檔案與檔案夾的屬性進行操作(流隻能操作資料)
3.File對象可以作為參數傳遞給流的構造函數
4.聊解File類中的常用方法
File類常見方法:
1,建立。
boolean createNewFile():
//在指定位置建立檔案,如果該檔案已經存在,則不建立,傳回false。
//和輸出流不一樣,輸出流對象一建立建立檔案。而且檔案已經存在,會覆寫。
boolean mkdir():建立檔案夾。
boolean mkdirs():建立多級檔案夾。
2,删除。
boolean delete():删除失敗傳回false。如果檔案正在被使用,則删除不了傳回falsel。
void deleteOnExit();在程式退出時删除指定檔案。
3,判斷。
boolean exists() :檔案是否存在.
isFile():
isDirectory();
isHidden();
isAbsolute();
4,擷取資訊。
getName():
getPath():
getParent():
getAbsolutePath()
long lastModified()
long length()
執行個體代碼:
[java] view plaincopy
<span style="font-size:18px;"> public class Iotest
{
public static void main(String[] args) throws IOException
{
method05();
}
public static void method05()
{
File f1 = new File("c:\\Test.java");
File f2 = new File("d:\\hahah.java");
sop("rename:"+f2.renameTo(f1));
}
public static void method04()
File f = new File("file.txt");
sop("path:"+f.getPath());
sop("abspath:"+f.getAbsolutePath());
sop("parent:"+f.getParent());
//該方法傳回的是絕對路徑中的父目錄。如果擷取的是相對路徑,傳回null。
//如果相對路徑中有上一層目錄那麼該目錄就是傳回結果。
}
public static void method03()throws IOException
File f = new File("d:\\java\\test.txt");
//建立檔案
f.createNewFile();
//建立目錄
//f.mkdir();
//記住在判斷檔案對象是否是檔案或者目的時,必須要先判斷該檔案對象封裝的内容是否存在。
//通過exists判斷。
sop("dir:"+f.isDirectory());
sop("file:"+f.isFile());
//判斷是否是絕對路徑
sop(f.isAbsolute());
public static void method02()
File dir = new File("java\\test\\testme");
sop("mkdir:"+dir.mkdirs());
public static void method01()throws IOException
{
File f = new File("file.txt");
}
//建立File對象
public static void consMethod()
//将a.txt封裝成file對象。可以将已有的和為出現的檔案或者檔案夾封裝成對象。
File f1 = new File("a.txt");
//指定目錄下的檔案
File f2 = new File("D:\\abc","b.txt");
File d = new File("D:\\abc");
File f3 = new File(d,"c.txt");
sop("f1:"+f1);
sop("f2:"+f2);
sop("f3:"+f3);
File f4 = new File("c:"+File.separator+"abc"+File.separator+"sss"+File.separator+"a.txt");
//友善列印對象
public static void sop(Object obj)
System.out.println(obj);
}</span>
IO包中的其他類:
*列印流:
PrintWriter與PrintStream
可以直接操作輸入流和檔案
*序列流
sequenceInputStream
對多個流進行合并
*操作對象:
ObjectInputSteam與ObjectOutputStream
被操作的兌現更要實作Serealizable(标記接口)
Serializalble接口:對象序列化即使把一個對象變為二進制的資料流的一種方法,通過對象執行個體化,可以友善地實作獨享的傳輸或存儲。
轉換流的字元編碼:
ASCII:美國标準洗洗交換表用一個位元組的7位表示;
ISO8859-1:拉丁碼表。歐洲碼表用一個位元組的8位表示
GB2312:中國的中文編碼表。
GBK:中國餓中文編碼表更新,融合了更多的中文文字元号。
Unicode:國際标準碼,融合了多種文字,所有文字都用兩個位元組來表示,JAVA語言使用的就是UNICODE
UTF_8:最多用三個位元組來表示一個字元.
位元組流和字元流的轉換:
OutputStreamWriter:是Writer的子類,将輸出的字元流變為位元組流,即将一個字元流的輸出對象變為位元組流的輸出對象。
InputSttreamReader: 是Reader的子類, 将輸入的位元組流變為字元流,即将一個位元組流的輸入對象變為字元流的輸入對象。
如果以檔案的操作為例,則記憶體中的字元流需要通過OutputStreamWriter變為位元組流才能儲存檔案,讀取時需要将讀入的位元組流通過InputSteamReader變為字元流。代碼示例:
<span style="font-size:18px;">public class InputStreamReaderDemo{
public static void main(String args[]) throws Exception{
File f = new File("d:" + File.separator + "test.txt") ;
Reader reader = null ;
reader = new InputStreamReader(new FileInputStream(f)) ; // 将位元組流變為字元流
char c[] = new char[1024] ;
int len = reader.read(c) ; // 讀取
reader.close() ; // 關閉
System.out.println(new String(c,0,len)) ;
}</span>
<span style="font-size:18px;">public class OutputStreamWriterDemo01{
public static void main(String args[]) throws Exception { // 所有異常抛出
File f = new File("d:" + File.separator + "test.txt") ;
Writer out = null ; // 字元輸出流
out = new OutputStreamWriter(new FileOutputStream(f)) ; // 位元組流變為字元流
out.write("hello world!!") ; // 使用字元流輸出
out.close() ;
}
RandromAccessFile類:
該類不算是IO體系中子類,而是繼承自Object。但他是IO包中成員,因為它具備寫和讀的功能。
内部封裝了一個數組,而且通過指針對數組的元素進行操作。
可以通過getFilePointer擷取指針位置。
同時可以通過seek改變指針的位置。
其實完成讀寫的原理即使内部風轉過了位元組流和輸入流
主要操作方法:
read() 從此檔案中讀取一個資料位元組。
readLine() 從此檔案讀取文本的下一行。
close() 關閉此随機通路檔案流并釋放與該流關聯的所有系統資源。
readInt() 從此檔案讀取一個有符号的整數。
write(byte[] b, int off, int len) 将 len 個位元組從指定 byte 數組寫入到此檔案,并從偏移量 off 處開始。
write(int b) 向此檔案寫入指定的位元組。
<span style="font-size:18px;"> public class RandomAccessFileDemo{
public static void main(String args[]) throws Exception{
File f = new File("G:" + File.separator + "test.txt") ;
RandomAccessFile rdf = null ;
rdf = new RandomAccessFile(f,"r") ;// 以隻讀的方式打開檔案
String name = null ;
int age = 0 ;
byte b[] = new byte[12] ;
// 讀取第二個人的資訊,意味着要空出第一個人的資訊
rdf.skipBytes(12) ;
for(int i=0;i<b.length;i++){
b[i] = rdf.readByte() ;
}
name = new String(b) ; // 将讀取出來的byte數組變為字元串
age = rdf.readInt() ;
System.out.println("第二個人的資訊 --> 姓名:" + name + ";年齡:" + age) ;
rdf.seek(0) ;
b[i] = rdf.readByte() ;
}
age = rdf.readInt() ;
System.out.println("第一個人的資訊 --> 姓名:" + name + ";年齡:" + age) ;
rdf.skipBytes(12) ;
for(int i=0;i<b.length;i++){
b[i] = rdf.readByte() ;
age = rdf.readInt() ;
System.out.println("第三個人的資訊 --> 姓名:" + name + ";年齡:" + age) ;
rdf.close() ; // 關閉
}</span>