国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Java回顧之I/O

2019-11-06 06:45:21
字體:
供稿:網(wǎng)友

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

  我計(jì)劃在接下來的幾篇文章中快速回顧一下Java,主要是一些基礎(chǔ)的JDK相關(guān)的內(nèi)容。

  工作后,使用的技術(shù)隨著項(xiàng)目的變化而變化,時(shí)而C#,時(shí)而Java,當(dāng)然還有其他一些零碎的技術(shù)。總體而言,C#的使用時(shí)間要更長(zhǎng)一些,其次是Java。我本身對(duì)語(yǔ)言沒有什么傾向性,能干活的語(yǔ)言,就是好語(yǔ)言。而且從面向?qū)ο蟮慕嵌葋砜矗矣X得C#和Java對(duì)我來說,沒什么區(qū)別。

  這篇文章主要回顧Java中和I/O操作相關(guān)的內(nèi)容,I/O也是編程語(yǔ)言的一個(gè)基礎(chǔ)特性,Java中的I/O分為兩種類型,一種是順序讀取,一種是隨機(jī)讀取。

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

  下面我們畫出InputStream的結(jié)構(gòu)

  

FileInputStream:操作文件,經(jīng)常和BufferedInputStream一起使用P復(fù)制代碼
 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 }復(fù)制代碼復(fù)制代碼
 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 }復(fù)制代碼使用OutputStream復(fù)制文件復(fù)制代碼
 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 }復(fù)制代碼復(fù)制代碼
 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 }復(fù)制代碼

這里的代碼對(duì)異常的處理非常不完整,稍后我們會(huì)給出完整嚴(yán)謹(jǐn)?shù)拇a。

  下面我們來看Reader的結(jié)構(gòu)

  

  這里的Reader基本上和InputStream能夠?qū)?yīng)上。  

  Writer的結(jié)構(gòu)如下

  

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

使用Reader讀取文件內(nèi)容復(fù)制代碼
 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 }復(fù)制代碼使用Writer復(fù)制文件復(fù)制代碼
 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 }復(fù)制代碼

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

創(chuàng)建一個(gè)大小固定的文件復(fù)制代碼
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 }復(fù)制代碼向文件中隨機(jī)寫入數(shù)據(jù)復(fù)制代碼
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 }復(fù)制代碼

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

移動(dòng)文件復(fù)制代碼
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 }復(fù)制代碼復(fù)制文件復(fù)制代碼
 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 }復(fù)制代碼復(fù)制文件夾復(fù)制代碼
 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 }復(fù)制代碼刪除文件夾復(fù)制代碼
 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 }復(fù)制代碼獲取文件夾大小復(fù)制代碼
 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 }復(fù)制代碼將大文件切分為多個(gè)小文件復(fù)制代碼
 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 }復(fù)制代碼將多個(gè)小文件合并為一個(gè)大文件復(fù)制代碼
 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 }復(fù)制代碼執(zhí)行外部命令復(fù)制代碼
 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 }復(fù)制代碼

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


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长宁县| 年辖:市辖区| 长汀县| 商都县| 南平市| 峡江县| 自治县| 合肥市| 四平市| 车致| 连平县| 岳西县| 五寨县| 廉江市| 开鲁县| 柳河县| 旬邑县| 闸北区| 天门市| 会东县| 忻城县| 林芝县| 达尔| 柯坪县| 德令哈市| 仲巴县| 昂仁县| 永嘉县| 阿克| 韶关市| 天镇县| 蓝田县| 虞城县| 彝良县| 固阳县| 木兰县| 开鲁县| 松原市| 黄陵县| 新建县| 宣化县|