java io 相關常用操作詳解

2021-07-24 23:45:12 字數 2275 閱讀 4320

1.檔案和資料夾的建立

2.檔案的讀取

3.檔案的寫入

4.檔案的複製(字元流、位元組流、處理流)

相關函式

(boolean) mkdir() 建立此抽象路徑名指定的目錄

(boolean) mkdirs() 建立此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。

(boolean) delete() 刪除此抽象路徑名表示的檔案或目錄

(boolean) createnewfile() 當不存在此路徑名指定名稱的檔案時,建立乙個新的空檔案。

建立檔案

public static void newfile(string pathstring) 

} catch (exception e)

} else

}

建立資料夾

public static void newfilebox(string pathstring) 

} else

}

應用:

public static void main(string args)

用filewriter寫入檔案

public  static void forfilewriter(string string,string filename)  catch (exception e) 

}

用bufferedwriter寫入檔案

public static void forbufferedwriter(string string,string desfile)  catch (exception e) 

}

應用:

public static void main(string args)

用filereader讀取檔案

public static void testreadbyreader(string filename) catch (exception e) 

}

用fileinputstream讀取檔案

public static void testreadbyinputstream(string filename) catch (exception e) 

}

用bufferedreader讀取檔案

public static void testreadbybufferedreader(string filename) 

} catch (exception e)

system.out.println(filename+"中按bufferedreader讀取的檔案內容是:\n"+buffer.tostring());

}

應用:

public static void main(string args)

字元流複製

public static void filecopy1(string readfile,string writefile) 			

input.close();

output.close();

} catch (ioexception e)

}

位元組流複製

public static void filecopy2(string readfile,string writefile) 			

input.close();

output.close();

} catch (ioexception e)

}

處理流複製

public static void filecopy3(string readfile,string writefile) 

bwriter.close();

breader.close();

} catch (exception e)

}

應用:

public static void main(string args)

Java IO流相關知識

一 io流的分類 reader inputstreamreader 節點流 bufferedreader 處理流 writer outputstreamwriter 節點流 bufferedwriter 處理流 printwriter inputstream fileinputstream 節點流 ...

java IO 檔案流詳解

本篇博文學習了一下內容 fileinputstream 位元組輸入流 構造方法 讀取檔案 fileoutputstream 位元組輸出流 構造方法 寫入檔案 filerearder 字元輸入流 構造方法 讀取檔案 filewriter 字元輸出流 構造方法 寫出檔案 位元組檔案和字元檔案 總結 構造...

Java IO實戰操作 二

向檔案中追加新內容 throws ioexception public void newinsernum throws ioexception out.close 獲取準確大小空間讀取檔案 throws ioexception public void selectfile throws ioexce...