新手了解java IO基礎(chǔ)知識
java.io.File類:文件和文件目錄路徑的抽象表示形式,與平臺無關(guān) File 能新建、刪除、重命名文件和目錄,但 File 不能訪問文件內(nèi)容本 身。如果需要訪問文件內(nèi)容本身,則需要使用輸入/輸出流。 想要在Java程序中表示一個(gè)真實(shí)存在的文件或目錄,那么必須有一個(gè) File對象。
2、創(chuàng)建方式public File(String pathname);//以pathname為路徑創(chuàng)建File對象,可以是絕對路徑或者相對路徑。 絕對路徑:是一個(gè)固定的路徑,從盤符開始 相對路徑:是相對于某個(gè)位置開始
public File(String parent,String child);//以parent為父路徑,child為子路徑創(chuàng)建File對象。
public File(File parent,String child);//根據(jù)一個(gè)父File對象和子文件路徑創(chuàng)建File對象。3、常用方法
public String getAbsolutePath()//獲取絕對路徑public String getPath() //獲取路徑public String getName() //獲取名稱public String getParent()//獲取上層文件目錄路徑。若無,返回nullpublic long length() //獲取文件長度(即:字節(jié)數(shù))public long lastModified() //獲取最后一次的修改時(shí)間,毫秒值public String[] list() //獲取指定目錄下的所有文件或者文件目錄的名稱數(shù)組public File[] listFiles()//獲取指定目錄下的所有文件或者文件目錄的File數(shù)組public boolean createNewFile()//當(dāng)且僅當(dāng)不存在具有此抽象路徑名指定名稱的文件時(shí),不可分地創(chuàng)建一個(gè)新的空文件。 public boolean delete() //刪除此抽象路徑名表示的文件或目錄。 public boolean exists()//測試此抽象路徑名表示的文件或目錄是否存在。 public String[] list()//返回一個(gè)字符串?dāng)?shù)組,這些字符串指定此抽象路徑名表示的目錄中的文件和目錄。public boolean mkdirs()//創(chuàng)建此抽象路徑名指定的目錄,包括所有必需但不存在的父目錄。 public boolean isDirectory()//判斷是否是文件目錄public boolean isFile()//判斷是否是文件
示例:
public class FileTest { public static void main(String[] args) {//File(String pathname);//以pathname為路徑創(chuàng)建File對象File file = new File('E:aaa');//File(File parent,String child);//根據(jù)一個(gè)父File對象和子文件路徑創(chuàng)建File對象。File file1 = new File(file,'test.txt');//boolean exists()判斷文件或目錄是否存在。 if (!(file.exists())){ // boolean mkdirs()創(chuàng)建此路徑名指定的目錄,包括所有必需但不存在的父目錄。 file.mkdirs();}else { try {//boolean createNewFile()當(dāng)且僅當(dāng)不存在具有此路徑名指定名稱的文件時(shí),創(chuàng)建一個(gè)新的空文件。file1.createNewFile(); } catch (IOException e) {e.printStackTrace(); }}//String getPath()獲取路徑System.out.println(file.getPath());//long length()獲取文件長度(即:字節(jié)數(shù))System.out.println(file.length());//String getName()獲取文件名稱System.out.println(file.getName());//long lastModified()獲取最后一次的修改時(shí)間,毫秒值System.out.println(file.getName());// public boolean isFile() :判斷是否是文件System.out.println(file.isFile()); // delete(file); }//遞歸的方式刪除文件或者文件夾 public static void delete(File file){ //File[] listFiles() 獲取指定目錄下的所有文件或者文件目錄的名稱數(shù)組File[] files = file.listFiles();for (File f : files) { //boolean isDirectory()判斷是否是文件目錄 if (f.isDirectory()){delete(f); } //boolean delete()刪除此路徑名表示的文件或目錄。 f.delete();}file.delete(); }
說明:Java中的刪除不到回收站,要?jiǎng)h除一個(gè)文件目錄,注意該文件目錄內(nèi)不能包含文件或者文件目錄。
二、IO概念 I/O 即輸入Input/ 輸出Output的縮寫,其實(shí)就是計(jì)算機(jī)調(diào)度把各個(gè)存儲中(包括內(nèi)存和外部存儲)的數(shù)據(jù)寫入寫出 java中用“流(stream)”來抽象表示這么一個(gè)寫入寫出的功能,封裝成一個(gè)“類”,都放在java.io這個(gè)包里面。 java.io包下提供了各種“流”類和接口,用以獲取不同種類的數(shù)據(jù),并 通過標(biāo)準(zhǔn)的方法輸入或輸出數(shù)據(jù)1.什么是輸入 程序從內(nèi)存中讀取數(shù)據(jù)叫輸入Input。
2.什么輸出(Output) 程序把數(shù)據(jù)寫入到內(nèi)存中叫輸出Output。
三、流的分類 按操作數(shù)據(jù)單位不同分為:字節(jié)流(8 bit),字符流(16 bit) 按數(shù)據(jù)流的流向不同分為:輸入流,輸出流 按流的角色的不同分為:節(jié)點(diǎn)流,處理流IO流體系
示例:
public static void main(String[] args) { iprt(); } public static void ipst(){InputStream inputStream = null;try { inputStream = new FileInputStream('C:1.txt'); int i; while ( (i = inputStream.read()) != -1){System.out.print((char) i); }} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();} finally { try {if (inputStream != null){ inputStream.close();} } catch (IOException e) {e.printStackTrace(); }} }
說明:使用InputStream向內(nèi)存中讀如文件數(shù)據(jù)。
2、OutputStream(字節(jié)流)示例:
public class ImageCopy { public static void main(String[] args) {try(InputStream inputStream = new FileInputStream('D:KDA.jpg');OutputStream outputStream = new FileOutputStream('E:aaaKDA.jpg') ){ byte[] bytes = new byte[1024]; int i; while ((i = inputStream.read(bytes)) != -1){outputStream.write(bytes,0,i); }} catch (IOException e) { e.printStackTrace();} }}
說明:使用輸入流與輸出流結(jié)合實(shí)現(xiàn)圖片復(fù)制的功能。
3、Reader(字符流)示例:
public static void iprt(){Reader reader = null;try { reader = new FileReader('C:1.txt'); int i ; while ((i = reader.read()) != -1){System.out.print((char) i); }} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();} finally {try { if (reader != null) {reader.close(); }} catch (IOException e) { e.printStackTrace();}} }
說明:使用Reader(字符流)從文件中讀入數(shù)據(jù)。
4、Writer(字符流)public static void iprt(){Reader reader = null;Writer writer = null;try { reader = new FileReader('C:Users52425Desktop1.txt'); writer = new FileWriter('C:2.txt'); int i ; while ((i = reader.read()) != -1){writer.write(i); }} catch (FileNotFoundException e) { e.printStackTrace();} catch (IOException e) { e.printStackTrace();} finally {try {writer.close();reader.close();} catch (IOException e) { e.printStackTrace();}} }
說明:使用字符流實(shí)現(xiàn)文件復(fù)制功能。
四、總結(jié)(1+2)1. File類及方法的使用File是操作文件/目錄的類,可以對文件/目錄進(jìn)行創(chuàng)建,重命名, 刪除等操作。
2.IO流的分類 根據(jù)數(shù)據(jù)大小可分為:字節(jié)流和字符流 根據(jù)流向可分為:輸入流和輸出流 根據(jù)功能可分為:節(jié)點(diǎn)流和處理流3.IO流的四個(gè)基本類 字節(jié)輸入流:InputStream,它的常用子類是FileInputStream 字節(jié)輸出流:OutputStream,它的常用子類是OutputStream 字符輸入流:Reader,它的常用子類是FileReader 字符輸出流:Writer,它的常用子類是FileWriter總結(jié)本篇關(guān)于java IO的文章就到這里了,希望能幫到你,也希望你能夠多多關(guān)注好吧啦網(wǎng)的更多內(nèi)容!
相關(guān)文章:
1. python 如何在 Matplotlib 中繪制垂直線2. bootstrap select2 動(dòng)態(tài)從后臺Ajax動(dòng)態(tài)獲取數(shù)據(jù)的代碼3. ASP常用日期格式化函數(shù) FormatDate()4. python中@contextmanager實(shí)例用法5. html中的form不提交(排除)某些input 原創(chuàng)6. CSS3中Transition屬性詳解以及示例分享7. js select支持手動(dòng)輸入功能實(shí)現(xiàn)代碼8. 如何通過python實(shí)現(xiàn)IOU計(jì)算代碼實(shí)例9. 開發(fā)效率翻倍的Web API使用技巧10. vue使用moment如何將時(shí)間戳轉(zhuǎn)為標(biāo)準(zhǔn)日期時(shí)間格式
