亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 編程 > Java > 正文

Java回顧之I/O

2019-11-06 06:45:21
字體:
來源:轉載
供稿:網友

學習java的同學注意了!??! 學習過程中遇到什么問題或者想獲取學習資源的話,歡迎加入Java學習交流群,群號碼:523047986  我們一起學Java!

  我計劃在接下來的幾篇文章中快速回顧一下Java,主要是一些基礎的JDK相關的內容。

  工作后,使用的技術隨著項目的變化而變化,時而C#,時而Java,當然還有其他一些零碎的技術??傮w而言,C#的使用時間要更長一些,其次是Java。我本身對語言沒有什么傾向性,能干活的語言,就是好語言。而且從面向對象的角度來看,我覺得C#和Java對我來說,沒什么區別。

  這篇文章主要回顧Java中和I/O操作相關的內容,I/O也是編程語言的一個基礎特性,Java中的I/O分為兩種類型,一種是順序讀取,一種是隨機讀取。

  我們先來看順序讀取,有兩種方式可以進行順序讀取,一種是InputStream/OutputStream,它是針對字節進行操作的輸入輸出流;另外一種是Reader/Writer,它是針對字符進行操作的輸入輸出流。

  下面我們畫出InputStream的結構

  

FileInputStream:操作文件,經常和BufferedInputStream一起使用P復制代碼
 1 public static byte[] readFileByFileInputStream(File file) throws IOException 2 { 3     ByteArrayOutputStream output = new ByteArrayOutputStream(); 4     FileInputStream fis = null; 5     try 6     { 7         fis = new FileInputStream(file); 8         byte[] buffer = new byte[1024]; 9         int bytesRead = 0;10         while((bytesRead = fis.read(buffer, 0, buffer.length)) != -1)11         {12             output.write(buffer, 0, bytesRead);13         }14     }15     catch(Exception ex)16     {17         System.out.println("Error occurs during reading " + file.getAbsoluteFile());18     }19     finally20     {21         if (fis !=null) fis.close();22         if (output !=null) output.close();23     }24     return output.toByteArray();25 }復制代碼復制代碼
 1 public static byte[] readFileByBufferedInputStream(File file) throws Exception 2 { 3     FileInputStream fis = null; 4     BufferedInputStream bis = null; 5     ByteArrayOutputStream output = new ByteArrayOutputStream(); 6     try 7     { 8         fis = new FileInputStream(file); 9         bis = new BufferedInputStream(fis);10         byte[] buffer = new byte[1024];11         int bytesRead = 0;12         while((bytesRead = bis.read(buffer, 0, buffer.length)) != -1)13         {14             output.write(buffer, 0, bytesRead);15         }16     }17     catch(Exception ex)18     {19         System.out.println("Error occurs during reading " + file.getAbsoluteFile());20     }21     finally22     {23         if (fis != null) fis.close();24         if (bis != null) bis.close();25         if (output != null) output.close();26     }27     return output.toByteArray();28 }復制代碼使用OutputStream復制文件復制代碼
 1 public static void copyFileByFileOutputStream(File file) throws IOException 2 { 3     FileInputStream fis = null; 4     FileOutputStream fos = null; 5     try 6     { 7         fis = new FileInputStream(file); 8         fos = new FileOutputStream(file.getName() + ".bak"); 9         byte[] buffer = new byte[1024];10         int bytesRead = 0;11         while((bytesRead = fis.read(buffer,0,buffer.length)) != -1)12         {13             fos.write(buffer, 0, bytesRead);14         }15         fos.flush();16     }17     catch(Exception ex)18     {19         System.out.println("Error occurs during copying " + file.getAbsoluteFile());20     }21     finally22     {23         if (fis != null) fis.close();24         if (fos != null) fos.close();25     }26 }復制代碼復制代碼
 1 public static void copyFilebyBufferedOutputStream(File file)throws IOException 2 { 3     FileInputStream fis = null; 4     BufferedInputStream bis = null; 5     FileOutputStream fos = null; 6     BufferedOutputStream bos = null; 7     try 8     { 9         fis = new FileInputStream(file);10         bis = new BufferedInputStream(fis);11         fos = new FileOutputStream(file.getName() + ".bak");12         bos = new BufferedOutputStream(fos);13         byte[] buffer = new byte[1024];14         int bytesRead = 0;15         while((bytesRead = bis.read(buffer, 0, buffer.length)) != -1)16         {17             bos.write(buffer, 0, bytesRead);18         }19         bos.flush();20     }21     catch(Exception ex)22     {23         System.out.println("Error occurs during copying " + file.getAbsoluteFile());24     }25     finally26     {27         if (fis != null) fis.close();28         if (bis != null) bis.close();29         if (fos != null) fos.close();30         if (bos != null) bos.close();31     }32 }復制代碼

這里的代碼對異常的處理非常不完整,稍后我們會給出完整嚴謹的代碼。

  下面我們來看Reader的結構

  

  這里的Reader基本上和InputStream能夠對應上?! ?/p>  Writer的結構如下

  

  下面我們來看一些使用Reader或者Writer的例子

使用Reader讀取文件內容復制代碼
 1 public static String readFile(String file)throws IOException 2 { 3     BufferedReader br = null; 4     StringBuffer sb = new StringBuffer(); 5     try 6     { 7         br = new BufferedReader(new FileReader(file)); 8         String line = null; 9         10         while((line = br.readLine()) != null)11         {12             sb.append(line);13         }14     }15     catch(Exception ex)16     {17         System.out.println("Error occurs during reading " + file);18     }19     finally20     {21         if (br != null) br.close();22     }23     return sb.toString();24 }復制代碼使用Writer復制文件復制代碼
 1 public static void copyFile(String file) throws IOException 2 {  3     BufferedReader br = null; 4     BufferedWriter bw = null; 5     try 6     { 7         br = new BufferedReader(new FileReader(file)); 8         bw = new BufferedWriter(new FileWriter(file + ".bak")); 9         String line = null;10         while((line = br.readLine())!= null)11         {12             bw.write(line);13         }14     }15     catch(Exception ex)16     {17         System.out.println("Error occurs during copying " + file);18     }19     finally20     {21         if (br != null) br.close();22         if (bw != null) bw.close();23     }24 }復制代碼

  下面我們來看如何對文件進行隨機訪問,Java中主要使用RandomaccessFile來對文件進行隨機操作。

創建一個大小固定的文件復制代碼
1 public static void createFile(String file, int size) throws IOException2 {3     File temp = new File(file);4     RandomAccessFile raf = new RandomAccessFile(temp, "rw");5     raf.setLength(size);6     raf.close();7 }復制代碼向文件中隨機寫入數據復制代碼
1 public static void writeFile(String file, byte[] content, int startPos, int contentLength) throws IOException2 {3     RandomAccessFile raf = new RandomAccessFile(new File(file), "rw");4     raf.seek(startPos);5     raf.write(content, 0, contentLength);6     raf.close();7 }復制代碼

  接下里,我們來看一些其他的常用操作

移動文件復制代碼
1 public static boolean moveFile(String sourceFile, String destFile)2 {3     File source = new File(sourceFile);4     if (!source.exists()) throw new RuntimeException("source file does not exist.");5     File dest = new File(destFile);6     if (!(new File(dest.getPath()).exists())) new File(dest.getParent()).mkdirs();7     return source.renameTo(dest);8 }復制代碼復制文件復制代碼
 1 public static void copyFile(String sourceFile, String destFile) throws IOException 2 { 3     File source = new File(sourceFile); 4     if (!source.exists()) throw new RuntimeException("File does not exist."); 5     if (!source.isFile()) throw new RuntimeException("It is not file."); 6     if (!source.canRead()) throw new RuntimeException("File cound not be read."); 7     File dest = new File(destFile); 8     if (dest.exists()) 9     {10         if (dest.isDirectory()) throw new RuntimeException("Destination is a folder.");11         else12         {13             dest.delete();14         }15     }16     else17     {18         File parentFolder = new File(dest.getParent());19         if (!parentFolder.exists()) parentFolder.mkdirs();20         if (!parentFolder.canWrite()) throw new RuntimeException("Destination can not be written.");21     }22     FileInputStream fis = null;23     FileOutputStream fos = null;24     try25     {26         fis = new FileInputStream(source);27         fos = new FileOutputStream(dest);28         byte[] buffer = new byte[1024];29         int bytesRead = 0;30         while((bytesRead = fis.read(buffer, 0, buffer.length)) != -1)31         {32             fos.write(buffer, 0, bytesRead);33         }34         fos.flush();35     }36     catch(IOException ex)37     {38         System.out.println("Error occurs during copying " + sourceFile);39     }40     finally41     {42         if (fis != null) fis.close();43         if (fos != null) fos.close();44     }45 }復制代碼復制文件夾復制代碼
 1 public static void copyDir(String sourceDir, String destDir) throws IOException 2 { 3      4     File source = new File(sourceDir); 5     if (!source.exists()) throw new RuntimeException("Source does not exist."); 6     if (!source.canRead()) throw new RuntimeException("Source could not be read."); 7     File dest = new File(destDir); 8     if (!dest.exists()) dest.mkdirs(); 9     10     File[] arrFiles = source.listFiles();11     for(int i = 0; i < arrFiles.length; i++)12     {13         if (arrFiles[i].isFile())14         {15             BufferedReader reader = new BufferedReader(new FileReader(arrFiles[i]));16             BufferedWriter writer = new BufferedWriter(new FileWriter(destDir + "/" + arrFiles[i].getName()));17             String line = null;18             while((line = reader.readLine()) != null) writer.write(line);19             writer.flush();20             reader.close();21             writer.close();22         }23         else24         {25             copyDir(sourceDir + "/" + arrFiles[i].getName(), destDir + "/" + arrFiles[i].getName());26         }27     }28 }復制代碼刪除文件夾復制代碼
 1 public static void del(String filePath) 2 { 3     File file = new File(filePath); 4     if (file == null || !file.exists()) return; 5     if (file.isFile()) 6     { 7         file.delete(); 8     } 9     else10     {11         File[] arrFiles = file.listFiles();12         if (arrFiles.length > 0)13         {14             for(int i = 0; i < arrFiles.length; i++)15             {16                 del(arrFiles[i].getAbsolutePath());17             }18         }19         file.delete();20     }21 }復制代碼獲取文件夾大小復制代碼
 1 public static long getFolderSize(String dir) 2 { 3     long size = 0; 4     File file = new File(dir); 5     if (!file.exists()) throw new RuntimeException("dir does not exist."); 6     if (file.isFile()) return file.length(); 7     else 8     { 9         String[] arrFileName = file.list();10         for (int i = 0; i < arrFileName.length; i++)11         {12             size += getFolderSize(dir + "/" + arrFileName[i]);13         }14     }15     16     return size;17 }復制代碼將大文件切分為多個小文件復制代碼
 1 public static void splitFile(String filePath, long unit) throws IOException 2 { 3     File file = new File(filePath); 4     if (!file.exists()) throw new RuntimeException("file does not exist."); 5     long size = file.length(); 6     if (unit >= size) return; 7     int count = size % unit == 0 ? (int)(size/unit) : (int)(size/unit) + 1; 8     String newFile = null; 9     FileOutputStream fos = null;10     FileInputStream fis =null;11     byte[] buffer = new byte[(int)unit];12     fis = new FileInputStream(file);13     long startPos = 0;14     String countFile = filePath + "_Count";15     PrintWriter writer = new PrintWriter(new FileWriter( new File(countFile)));16     writer.println(filePath + "/t" + size);17     for (int i = 1; i <= count; i++)18     {19         newFile = filePath + "_" + i;20         startPos = (i - 1) * unit;21         System.out.println("Creating " + newFile);22         fos = new FileOutputStream(new File(newFile));23         int bytesRead = fis.read(buffer, 0, buffer.length);24         if (bytesRead != -1)25         {26             fos.write(buffer, 0, bytesRead);27             writer.println(newFile + "/t" + startPos + "/t" + bytesRead);28         }29         fos.flush();30         fos.close();31         System.out.println("StartPos:" + i*unit + "; EndPos:" + (i*unit + bytesRead));32     }33     writer.flush();34     writer.close();35     fis.close();36 }復制代碼將多個小文件合并為一個大文件復制代碼
 1 public static void linkFiles(String countFile) throws IOException 2 { 3     File file = new File(countFile); 4     if (!file.exists()) throw new RuntimeException("Count file does not exist."); 5     BufferedReader reader = new BufferedReader(new FileReader(file)); 6     String line = reader.readLine(); 7     String newFile = line.split("/t")[0]; 8     long size = Long.parseLong(line.split("/t")[1]); 9     RandomAccessFile raf = new RandomAccessFile(newFile, "rw");10     raf.setLength(size);11     FileInputStream fis = null;12     byte[] buffer = null;13     14     while((line = reader.readLine()) != null)15     {16         String[] arrInfo = line.split("/t");17         fis = new FileInputStream(new File(arrInfo[0]));18         buffer = new byte[Integer.parseInt(arrInfo[2])];19         long startPos = Long.parseLong(arrInfo[1]);20         fis.read(buffer, 0, Integer.parseInt(arrInfo[2]));21         raf.seek(startPos);22         raf.write(buffer, 0, Integer.parseInt(arrInfo[2]));23         fis.close();24     }25     raf.close();26 }復制代碼執行外部命令復制代碼
 1 public static void execExternalCommand(String command, String argument) 2 { 3     Process process = null; 4     try 5     { 6         process = Runtime.getRuntime().exec(command + " " + argument); 7         InputStream is = process.getInputStream(); 8         BufferedReader br = new BufferedReader(new InputStreamReader(is)); 9         String line = null;10         while((line = br.readLine()) != null)11         {12             System.out.println(line);13         }14     }15     catch(Exception ex)16     {17         System.err.println(ex.getMessage());18     }19     finally20     {21         if (process != null) process.destroy();22     }23 }復制代碼

學習Java的同學注意了!??! 學習過程中遇到什么問題或者想獲取學習資源的話,歡迎加入Java學習交流群,群號碼:523047986  我們一起學Java!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
欧美午夜丰满在线18影院| 国产亚洲一区二区精品| 欧美裸体xxxx极品少妇软件| xxxxxxxxx欧美| 精品久久久免费| 欧美成人高清视频| 久久69精品久久久久久国产越南| 成人黄色午夜影院| 98精品国产高清在线xxxx天堂| 欧美日韩国产色| 91国内免费在线视频| 亚洲欧美日韩一区在线| 国产精品国产亚洲伊人久久| 久久久久久久999| 亚洲乱码av中文一区二区| 成人高h视频在线| 亚洲综合精品一区二区| 亚洲国产精品热久久| 日韩大片免费观看视频播放| 一区二区三区精品99久久| 亚洲精品狠狠操| 大荫蒂欧美视频另类xxxx| 国产精品福利在线观看网址| 成人黄色片在线| 91精品综合视频| 亚洲一区二区三区视频播放| 国产欧美精品在线| 伊人久久男人天堂| 久久久免费av| 国产精品成人观看视频国产奇米| 久精品免费视频| 欧洲日本亚洲国产区| 国产日韩av在线播放| 日韩美女中文字幕| 国产精品久久二区| 亚洲精品xxx| 欧美日韩国产一区二区三区| 中文字幕亚洲欧美在线| 日韩在线视频一区| 久久噜噜噜精品国产亚洲综合| 欧洲日韩成人av| 久热精品在线视频| 国产精品444| 亚洲国产精品免费| 国产亚洲一级高清| 国产成+人+综合+亚洲欧美丁香花| 热99精品只有里视频精品| 亚洲国语精品自产拍在线观看| 久久亚洲精品中文字幕冲田杏梨| 国产精品尤物福利片在线观看| 九九热最新视频//这里只有精品| 狠狠做深爱婷婷久久综合一区| 久久频这里精品99香蕉| 欧美孕妇孕交黑巨大网站| 精品久久香蕉国产线看观看亚洲| 大荫蒂欧美视频另类xxxx| 亚洲人精选亚洲人成在线| 亚洲成年人在线| 欧美肥老太性生活视频| 日韩一区在线视频| 亚洲成人精品视频在线观看| 国产精品久久综合av爱欲tv| 不卡伊人av在线播放| 中文字幕日韩专区| 韩国视频理论视频久久| 亚洲国产精品热久久| 国产一区二区三区丝袜| 日韩av成人在线| 中文字幕亚洲综合| 欧美专区第一页| 国产一区二区丝袜| 国产免费一区二区三区在线观看| 日韩精品免费电影| 成人激情视频在线播放| 中文字幕视频在线免费欧美日韩综合在线看| 国产精品一区二区久久精品| 欧美日韩一区二区三区| 国产成人jvid在线播放| 亚洲成人aaa| 国产丝袜一区二区| 亚洲国产成人精品一区二区| 97视频国产在线| 91黄色8090| 91精品国产高清自在线| 亚洲成人av片在线观看| 91欧美激情另类亚洲| 日韩欧美在线视频日韩欧美在线视频| 精品欧美国产一区二区三区| 色偷偷9999www| 欧美刺激性大交免费视频| 国产91免费观看| 日韩在线视频国产| 精品久久久香蕉免费精品视频| 欧美影院成年免费版| 国产亚洲一区二区在线| 亚洲欧美在线一区| 欧美黑人国产人伦爽爽爽| 亚洲精品日产aⅴ| 国产成人综合亚洲| 欧美日韩aaaa| 高清欧美性猛交xxxx| 亚洲欧美日韩另类| 国产成人a亚洲精品| 国产精品96久久久久久| 国产综合在线观看视频| 欧美激情亚洲视频| 欧美猛少妇色xxxxx| 久久成人精品视频| 欧美激情xxxx| 国产ts人妖一区二区三区| 亚洲国模精品私拍| 亚洲激情在线观看| 91精品成人久久| 色偷偷偷综合中文字幕;dd| 亚洲国产一区自拍| 青青草原成人在线视频| 国语自产精品视频在线看一大j8| 日韩精品在线电影| 亚洲伊人一本大道中文字幕| 777精品视频| 高清一区二区三区四区五区| 久久久久久久久久久成人| 夜夜狂射影院欧美极品| 国产亚洲成精品久久| 亚洲日本欧美中文幕| 狠狠躁夜夜躁人人爽天天天天97| 欧美最猛性xxxxx亚洲精品| 久久国产精品久久国产精品| 最新国产精品拍自在线播放| 国产成人精品久久二区二区| 午夜精品国产精品大乳美女| 亚洲最大福利视频网| 精品国产欧美成人夜夜嗨| 国产精品电影观看| 亚洲成人黄色在线| 国产成人激情小视频| 5278欧美一区二区三区| 亚洲人成电影网站色www| 亚洲国产女人aaa毛片在线| 国产一区二区美女视频| 国产精品丝袜久久久久久高清| 欧美精品免费看| 精品国产欧美成人夜夜嗨| 中文字幕视频在线免费欧美日韩综合在线看| 日韩成人xxxx| 亚洲亚裔videos黑人hd| 亚洲欧美中文日韩在线| 亚洲最大av网| 色悠悠久久久久| 国产精品自产拍在线观看| 欧美精品18videosex性欧美| 亚洲女人天堂av| 国产一区二区三区中文| 国产精品亚洲网站| 91国产美女视频| 亚洲一品av免费观看| 日韩激情av在线免费观看| 97在线视频免费| 欧美成人sm免费视频| 91精品国产综合久久香蕉的用户体验| 精品丝袜一区二区三区| 丝袜美腿亚洲一区二区| 欧美性生交xxxxxdddd| 日韩福利在线播放|