1、File類
(1)File類的幾種構(gòu)造方法;
//創(chuàng)建文件和文件夾
public static void createFile() throws IOException {
//文件操作
//File file = new File("file.txt");
//創(chuàng)建
//boolean b = file.createNewFile();
//刪除
//boolean b = file.delete();
//是否存在
//boolean b = file.exists();
//創(chuàng)建文件夾
File dir = new File("c:\\abc");
boolean b = dir.mkdir();
System.out.println("b="+b);
}
(2)獲取文件信息(略)
獲取名稱,
獲取路徑,
獲取大小,
獲取時間。
(3)判斷(略)
文件是否可讀;
文件是否可寫;
文件是否可執(zhí)行;
文件是否隱藏;
(4)文件的創(chuàng)建和刪除以及該文件是否存在;
(5)練習(xí)列出子目錄中的內(nèi)容(遞歸);
//遞歸遍歷文件夾
public static void printFiles(File dir,int count){
System.out.println(getSpace(count)+dir.getName());
count++;
File[] files = dir.listFiles();
if(files != null){
for(File file:dir.listFiles()){
if(file.isDirectory()){
printFiles(file,count);
}else{
System.out.println(getSpace(count)+file.getName());
}
}
}
}
//計算空格
public static String getSpace(int count){
StringBuilder builder = new StringBuilder();
for(int i=0;i<count;i++){
builder.append("|--");
}
return builder.toString();
}
(6)遍歷刪除文件夾
//遞歸刪除一個文件夾下所有文件
public static void deleteFile(File dir){
//System.out.println(dir.getAbsolutePath());
File[] files = dir.listFiles();
for(File file:files){
if(file.isDirectory()){
deleteFile(file);
}else{
System.out.println(file.getAbsolutePath()+file.delete());
}
}
System.out.println(dir.getAbsolutePath()+dir.delete());
}
(7)Properties(略)
2、字符流Reader,Writer
(1)FileWriter使用
<1>例子:
FileWriter fw = new FileWriter("demo.txt");
fw.write("abcdef");
fw.flush();
fw.write("haha");
fw.close();
<2>flush()和close()有什么區(qū)別?
flush():僅將緩沖區(qū)中的數(shù)據(jù)刷新到目的地。流對象可以繼續(xù)使用。
close():將緩沖區(qū)中的數(shù)據(jù)刷到目的地后,直接關(guān)閉資源,流無法繼續(xù)使用。
<3>IO異常的基本處理方式:
FileWriter fw = null;
try {
fw = new FileWriter("demo.txt",true);
fw.write("abcdef");
fw.flush();
fw.write("ha\r\nha");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(fw != null){
try {
fw.close();
} catch (IOException e) {
throw new RuntimeException("字符流關(guān)閉失敗");
}
}
}
3、字節(jié)流
InputStream,OutputStream
4、轉(zhuǎn)換流,打印流,對象序列化,編碼等)
(1)PrintStream和PrintWriter:可直接操作流和文件
(2)序列流SequenceInputStream:對多個流進行合并
(3)操作對象
<1>ObjectInputStream與ObjectOutputStream
//被操作的對象需要實現(xiàn)Serializable;
<2>對象的序列化
Serializable使用的時候生成的序列號,建議顯示聲明,防止編譯器不同時,發(fā)生錯誤;
注意:
(1)類中的靜態(tài)成員變量是不能被序列化的,因為靜態(tài)成員變量不再堆中。
(2)如果希望非靜態(tài)數(shù)據(jù)不被初始化,可以使用transient修飾對象。
(4)文件分割
(5)RandomAccessFile
(6)管道流
特點:
<1>單線程容易造成死鎖。
示例:
public class PipedStreamDemo {
public static void main(String[] args) throws IOException {
PipedInputStream inputStream = new PipedInputStream();
PipedOutputStream outputStream = new PipedOutputStream();
inputStream.connect(outputStream);
Input input = new Input(inputStream);
Output output = new Output(outputStream);
new Thread(output).start();
new Thread(input).start();
}
}
class Input implements Runnable{
private PipedInputStream in;
public Input(PipedInputStream in){
super();
this.in = in;
}
@Override
public void run() {
byte[] buf = new byte[1024];
int len = 0;
try {
len = in.read(buf);
String str = new String(buf,0,len);
System.out.println("read:"+str);
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
class Output implements Runnable{
private PipedOutputStream out;
public Output(PipedOutputStream out) {
this.out = out;
}
@Override
public void run() {
try {
out.write("hello piped!".getBytes());
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
(7)操作基本數(shù)據(jù)類型的流
private static void operateData() throws IOException {
// FileOutputStream fos = new FileOutputStream("data.txt");
// DataOutputStream dos = new DataOutputStream(fos);
// dos.writeInt(98);
// dos.writeBoolean(true);
// dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream("data.txt"));
int num = dis.readInt();
boolean b = dis.readBoolean();
System.out.println("num = "+num);
System.out.println("boolean = "+b);
dis.close();
}
(8)操作字節(jié)數(shù)組的流
private static void operateData() {
ByteArrayInputStream bis = new ByteArrayInputStream("abc".getBytes());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
int ch = 0;
while ((ch=bis.read())!=-1){
bos.write(ch);
}
System.out.println(bos.toString());
}
5、IO流使用總結(jié)
(1)明確源和目的。
源:InputStream Reader 一定是被讀取的。
目的:OutputStream Writer 一定是被寫入的。
(2)處理的數(shù)據(jù)是否是基本數(shù)據(jù)?
是:使用字符流。Reader Writer
否:使用字節(jié)流。InputStream OutputStream
(3)明確數(shù)據(jù)所在設(shè)備:
源設(shè)備:
鍵盤(System.in)
硬盤(FileXXX)FileReader FileInputStream
內(nèi)存(數(shù)組)ByteArrayInputStream CharArrayReader StringReader
網(wǎng)絡(luò)(Socket)
目的設(shè)備:
顯示器(控制臺System.out)
硬盤(FileXXX)FileWriter FileOutputStream
內(nèi)存(數(shù)組)ByteArrayOutputStream CharArrayWriter StringWriter
網(wǎng)絡(luò)(Socket)
(4)明確是否需要額外功能?
<1>是否需要高效?緩沖區(qū)Buffered四個。
<2>是否需要轉(zhuǎn)換?轉(zhuǎn)換流 InputStreamReader OutputStreamWriter
<3>是否操作基本數(shù)據(jù)類型?DataInputStream DataOutputStream
<4>是否操作對象(對象序列化)?ObjectInputStream ObjectOutputStream
<5>需要對多個源合并嗎?SequenceInputStream
<6>需要保證數(shù)據(jù)的表現(xiàn)形式到目的嗎?PrintWriter
<7>如果數(shù)據(jù)有規(guī)律,并且源和目的都是file,需要隨機訪問時,可以使用RandomAccessFile工具類。
十七、IO流
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
- 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
推薦閱讀更多精彩內(nèi)容
- 序列流 序列流可以把多個字節(jié)輸入流整合成一個,從序列流中讀取數(shù)據(jù)時,將從被整合的第一個流開始讀,讀完一個讀第二個,...
- 轉(zhuǎn)換流 輸入字節(jié)流的轉(zhuǎn)換流:InputStreamReader 是字節(jié)流通向字符流的橋InputStreamRea...
- 一、IO流的概念 Java的IO流是實現(xiàn)輸入/輸出的基礎(chǔ),它可以方便地實現(xiàn)數(shù)據(jù)的輸入/輸出操作,在Java中把不同...
- Java中是通過流的方式對數(shù)據(jù)進行操作,用于操作流的類都在IO包中,IO流用來處理設(shè)備之間的數(shù)據(jù)傳輸。IO流按照流...