Java文件操作實例詳解
更新時間:2022年04月25日 17:20:46 作者:寶貝垚
這篇文章主要為大家詳細介紹了Java文件操作實例,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java文件操作的具體代碼,供大家參考,具體內(nèi)容如下
簡介
本程序主要采用了FileInputStream和FileOutputStream兩類對文件進行操作。具體包括通過相對路徑打開文件,三種方法讀取文件,查看文件屬性,追加文件數(shù)據(jù)等。
效果圖:
完整代碼:
package Code.a; import java.io.*; public class FileInputStreamDemo { ?? ? ?? ?public static void main(String[] args) { ?? ??? ?//獲取當前目錄; ?? ??? ?File f = new File("."); ?? ??? ?System.out.print("absolute path:"+f.getAbsolutePath()+"\n"); ?? ??? ?while(true) ?? ??? ?{ ?? ??? ??? ?try { ?? ??? ??? ??? ?//輸入命令; ?? ??? ??? ??? ?System.out.print("Please input your order:"); ?? ??? ??? ??? ?BufferedReader stdinBufferedReader; ?? ??? ??? ??? ?String str1 = null; ?? ??? ??? ??? ?stdinBufferedReader = new BufferedReader(new InputStreamReader(System.in)); ?? ??? ??? ??? ?str1 = stdinBufferedReader.readLine(); ?? ??? ??? ??? ?//相對路徑打開文件; ?? ??? ??? ??? ?File file2 = new File(".\\src\\Code\\a\\Exception.java"); ?? ??? ??? ??? ?FileInputStream fis2 = new FileInputStream(file2); ?? ??? ??? ??? ? ?? ??? ??? ??? ?根據(jù)不同的命令,執(zhí)行不同操作; ?? ??? ??? ??? ?//一次性讀取全部數(shù)據(jù) ?? ??? ??? ??? ?if(str1.equals("一次性讀取全部數(shù)據(jù)")) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?byte[] buf = new byte[(int)(file2.length())]; ?? ??? ??? ??? ??? ?fis2.read(buf); ?? ??? ??? ??? ??? ?String str = new String(buf); ?? ??? ??? ??? ??? ?System.out.print(str); ?? ??? ??? ??? ??? ?System.out.print("\n"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//分塊讀取 ?? ??? ??? ??? ?else if(str1.equals("分塊讀取")) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?int n = 1024,count; ?? ??? ??? ??? ??? ?byte[] buf = new byte[n]; ?? ??? ??? ??? ??? ?while((count = fis2.read(buf)) != -1) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?System.out.print(new String(buf,0,count)); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?System.out.print("\n"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//逐字讀取數(shù)據(jù) ?? ??? ??? ??? ?else if(str1.equals("逐字讀取數(shù)據(jù)")) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?for(int i = 0; i < file2.length(); i++) ?? ??? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ??? ?char ch = (char)(fis2.read()); ?? ??? ??? ??? ??? ??? ?System.out.print(ch); ?? ??? ??? ??? ??? ?} ?? ??? ??? ??? ??? ?System.out.print("\n"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//退出 ?? ??? ??? ??? ?else if(str1.equals("退出")) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?System.out.print("已退出\n"); ?? ??? ??? ??? ??? ?break; ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//查看文件屬性 ?? ??? ??? ??? ?else if(str1.equals("查看文件屬性")) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?System.out.print("If the file or catalog exists:"+file2.exists()+"\n"); ?? ??? ??? ??? ??? ?System.out.print("If is it a file:"+file2.isFile()+"\n"); ?? ??? ??? ??? ??? ?System.out.print("If is it a catalog:"+file2.isDirectory()+"\n"); ?? ??? ??? ??? ??? ?System.out.print("FileName:"+file2.getName()+"\n"); ?? ??? ??? ??? ??? ?System.out.print("absolute path:"+file2.getAbsolutePath()+"\n"); ?? ??? ??? ??? ??? ?System.out.print("The last time that the file was changed:"+file2.lastModified()+"\n"); ?? ??? ??? ??? ??? ?System.out.print("The size of the file:"+file2.length()+" bites\n"); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//向文件追加數(shù)據(jù) ?? ??? ??? ??? ?else if(str1.equals("文件追加數(shù)據(jù)")) ?? ??? ??? ??? ?{ ?? ??? ??? ??? ??? ?FileOutputStream fos2 = new FileOutputStream(file2,true); ?? ??? ??? ??? ??? ?System.out.println("Please input the content: "); ?? ??? ??? ??? ??? ?BufferedReader ContentReader; ?? ??? ??? ??? ??? ?String str2 = null; ?? ??? ??? ??? ??? ?ContentReader = new BufferedReader(new InputStreamReader(System.in)); ?? ??? ??? ??? ??? ?str2 = ContentReader.readLine(); ?? ??? ??? ??? ??? ?fos2.write(str2.getBytes()); ?? ??? ??? ??? ??? ?fos2.close(); ?? ??? ??? ??? ?} ?? ??? ??? ??? ?//關(guān)閉流對象; ?? ??? ??? ??? ?fis2.close(); ?? ??? ??? ?} ?? ??? ??? ?//處理異常; ?? ??? ??? ?catch(FileNotFoundException fnfe) { ?? ??? ??? ??? ?System.out.print("The file open unsuccessfully."); ?? ??? ??? ?}catch(IOException ioe) { ?? ??? ??? ??? ?ioe.printStackTrace(); ?? ??? ??? ?} ?? ??? ?} ?? ?} ? ?? }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java實現(xiàn)PDF轉(zhuǎn)HTML/Word/Excel/PPT/PNG的示例代碼
這篇文章主要為大家介紹了如何利用Java語言是PDF轉(zhuǎn)HTML、Word、Excel、PPT和PNG功能,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2022-05-05基于springboot微信公眾號開發(fā)(微信自動回復)
這篇文章主要介紹了基于springboot微信公眾號開發(fā)(微信自動回復),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-11-11spring-data-elasticsearch @Field注解無效的完美解決方案
這篇文章主要介紹了spring-data-elasticsearch @Field注解無效的完美解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07