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

首頁 > 編程 > Java > 正文

基于Java回顧之I/O的使用詳解

2019-11-26 16:07:36
字體:
來源:轉載
供稿:網友

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

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

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

  下面我們畫出InputStream的結構

    FileInputStream:操作文件,經常和BufferedInputStream一起使用
    PipedInputStream:可用于線程間通信
    ObjectInputStream:可用于對象序列化
    ByteArrayInputStream:用于處理字節數組的輸入
    LineNumberInputStream:可輸出當前行數,并且可以在程序中進行修改

  下面是OutputStream的結構

    PrintStream:提供了類似print和println的接口去輸出數據

  下面我們來看如何使用Stream的方式來操作輸入輸出

使用InputStream讀取文件

復制代碼 代碼如下:

使用FileInputStream讀取文件信息
 public static byte[] readFileByFileInputStream(File file) throws IOException
 {
     ByteArrayOutputStream output = new ByteArrayOutputStream();
     FileInputStream fis = null;
     try
     {
         fis = new FileInputStream(file);
         byte[] buffer = new byte[1024];
         int bytesRead = 0;
         while((bytesRead = fis.read(buffer, 0, buffer.length)) != -1)
         {
             output.write(buffer, 0, bytesRead);
         }
     }
     catch(Exception ex)
     {
         System.out.println("Error occurs during reading " + file.getAbsoluteFile());
     }
     finally
     {
         if (fis !=null) fis.close();
         if (output !=null) output.close();
     }
     return output.toByteArray();
 }

使用BufferedInputStream讀取文件
復制代碼 代碼如下:

 public static byte[] readFileByBufferedInputStream(File file) throws Exception
 {
     FileInputStream fis = null;
     BufferedInputStream bis = null;
     ByteArrayOutputStream output = new ByteArrayOutputStream();
     try
     {
         fis = new FileInputStream(file);
         bis = new BufferedInputStream(fis);
         byte[] buffer = new byte[1024];
         int bytesRead = 0;
         while((bytesRead = bis.read(buffer, 0, buffer.length)) != -1)
         {
             output.write(buffer, 0, bytesRead);
         }
     }
     catch(Exception ex)
     {
         System.out.println("Error occurs during reading " + file.getAbsoluteFile());
     }
     finally
     {
         if (fis != null) fis.close();
         if (bis != null) bis.close();
         if (output != null) output.close();
     }
     return output.toByteArray();
 }

使用OutputStream復制文件
復制代碼 代碼如下:

使用FileOutputStream復制文件
 public static void copyFileByFileOutputStream(File file) throws IOException
 {
     FileInputStream fis = null;
     FileOutputStream fos = null;
     try
     {
         fis = new FileInputStream(file);
         fos = new FileOutputStream(file.getName() + ".bak");
         byte[] buffer = new byte[1024];
         int bytesRead = 0;
         while((bytesRead = fis.read(buffer,0,buffer.length)) != -1)
         {
             fos.write(buffer, 0, bytesRead);
         }
         fos.flush();
     }
     catch(Exception ex)
     {
         System.out.println("Error occurs during copying " + file.getAbsoluteFile());
     }
     finally
     {
         if (fis != null) fis.close();
         if (fos != null) fos.close();
     }
 }

復制代碼 代碼如下:

使用BufferedOutputStream復制文件
 public static void copyFilebyBufferedOutputStream(File file)throws IOException
 {
     FileInputStream fis = null;
     BufferedInputStream bis = null;
     FileOutputStream fos = null;
     BufferedOutputStream bos = null;
     try
     {
         fis = new FileInputStream(file);
         bis = new BufferedInputStream(fis);
         fos = new FileOutputStream(file.getName() + ".bak");
         bos = new BufferedOutputStream(fos);
         byte[] buffer = new byte[1024];
         int bytesRead = 0;
         while((bytesRead = bis.read(buffer, 0, buffer.length)) != -1)
         {
             bos.write(buffer, 0, bytesRead);
         }
         bos.flush();
     }
     catch(Exception ex)
     {
         System.out.println("Error occurs during copying " + file.getAbsoluteFile());
     }
     finally
     {
         if (fis != null) fis.close();
         if (bis != null) bis.close();
         if (fos != null) fos.close();
         if (bos != null) bos.close();
     }
 }

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

  下面我們來看Reader的結構

這里的Reader基本上和InputStream能夠對應上?! ?/P>

  Writer的結構如下

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

    使用Reader讀取文件內容

復制代碼 代碼如下:

使用BufferedReader讀取文件內容
 public static String readFile(String file)throws IOException
 {
     BufferedReader br = null;
     StringBuffer sb = new StringBuffer();
     try
     {
         br = new BufferedReader(new FileReader(file));
         String line = null;

         while((line = br.readLine()) != null)
         {
             sb.append(line);
         }
     }
     catch(Exception ex)
     {
         System.out.println("Error occurs during reading " + file);
     }
     finally
     {
         if (br != null) br.close();
     }
     return sb.toString();
 }

使用Writer復制文件
復制代碼 代碼如下:

使用BufferedWriter復制文件
 public static void copyFile(String file) throws IOException
 {
     BufferedReader br = null;
     BufferedWriter bw = null;
     try
     {
         br = new BufferedReader(new FileReader(file));
         bw = new BufferedWriter(new FileWriter(file + ".bak"));
         String line = null;
         while((line = br.readLine())!= null)
         {
             bw.write(line);
         }
     }
     catch(Exception ex)
     {
         System.out.println("Error occurs during copying " + file);
     }
     finally
     {
         if (br != null) br.close();
         if (bw != null) bw.close();
     }
 }

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

    創建一個大小固定的文件

復制代碼 代碼如下:

創建大小固定的文件
 public static void createFile(String file, int size) throws IOException
 {
     File temp = new File(file);
     RandomAccessFile raf = new RandomAccessFile(temp, "rw");
     raf.setLength(size);
     raf.close();
 }

向文件中隨機寫入數據
復制代碼 代碼如下:

向文件中隨機插入數據
 public static void writeFile(String file, byte[] content, int startPos, int contentLength) throws IOException
 {
     RandomAccessFile raf = new RandomAccessFile(new File(file), "rw");
     raf.seek(startPos);
     raf.write(content, 0, contentLength);
     raf.close();
 }

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

    移動文件

復制代碼 代碼如下:

移動文件
 public static boolean moveFile(String sourceFile, String destFile)
 {
     File source = new File(sourceFile);
     if (!source.exists()) throw new RuntimeException("source file does not exist.");
     File dest = new File(destFile);
     if (!(new File(dest.getPath()).exists())) new File(dest.getParent()).mkdirs();
     return source.renameTo(dest);
 }

復制文件
復制代碼 代碼如下:

復制文件
 public static void copyFile(String sourceFile, String destFile) throws IOException
 {
     File source = new File(sourceFile);
     if (!source.exists()) throw new RuntimeException("File does not exist.");
     if (!source.isFile()) throw new RuntimeException("It is not file.");
     if (!source.canRead()) throw new RuntimeException("File cound not be read.");
     File dest = new File(destFile);
     if (dest.exists())
     {
         if (dest.isDirectory()) throw new RuntimeException("Destination is a folder.");
         else
         {
             dest.delete();
         }
     }
     else
     {
         File parentFolder = new File(dest.getParent());
         if (!parentFolder.exists()) parentFolder.mkdirs();
         if (!parentFolder.canWrite()) throw new RuntimeException("Destination can not be written.");
     }
     FileInputStream fis = null;
     FileOutputStream fos = null;
     try
     {
         fis = new FileInputStream(source);
         fos = new FileOutputStream(dest);
         byte[] buffer = new byte[1024];
         int bytesRead = 0;
         while((bytesRead = fis.read(buffer, 0, buffer.length)) != -1)
         {
             fos.write(buffer, 0, bytesRead);
         }
         fos.flush();
     }
     catch(IOException ex)
     {
         System.out.println("Error occurs during copying " + sourceFile);
     }
     finally
     {
         if (fis != null) fis.close();
         if (fos != null) fos.close();
     }
 }

復制文件夾
復制代碼 代碼如下:

復制文件夾
 public static void copyDir(String sourceDir, String destDir) throws IOException
 {

     File source = new File(sourceDir);
     if (!source.exists()) throw new RuntimeException("Source does not exist.");
     if (!source.canRead()) throw new RuntimeException("Source could not be read.");
     File dest = new File(destDir);
     if (!dest.exists()) dest.mkdirs();

     File[] arrFiles = source.listFiles();
     for(int i = 0; i < arrFiles.length; i++)
     {
         if (arrFiles[i].isFile())
         {
             BufferedReader reader = new BufferedReader(new FileReader(arrFiles[i]));
             BufferedWriter writer = new BufferedWriter(new FileWriter(destDir + "/" + arrFiles[i].getName()));
             String line = null;
             while((line = reader.readLine()) != null) writer.write(line);
             writer.flush();
             reader.close();
             writer.close();
         }
         else
         {
             copyDir(sourceDir + "/" + arrFiles[i].getName(), destDir + "/" + arrFiles[i].getName());
         }
     }
 }

刪除文件夾
復制代碼 代碼如下:

刪除文件夾
 public static void del(String filePath)
 {
     File file = new File(filePath);
     if (file == null || !file.exists()) return;
     if (file.isFile())
     {
         file.delete();
     }
     else
     {
         File[] arrFiles = file.listFiles();
         if (arrFiles.length > 0)
         {
             for(int i = 0; i < arrFiles.length; i++)
             {
                 del(arrFiles[i].getAbsolutePath());
             }
         }
         file.delete();
     }
 }

獲取文件夾大小
復制代碼 代碼如下:

獲取文件夾大小
 public static long getFolderSize(String dir)
 {
     long size = 0;
     File file = new File(dir);
     if (!file.exists()) throw new RuntimeException("dir does not exist.");
     if (file.isFile()) return file.length();
     else
     {
         String[] arrFileName = file.list();
         for (int i = 0; i < arrFileName.length; i++)
         {
             size += getFolderSize(dir + "/" + arrFileName[i]);
         }
     }

     return size;
 }

將大文件切分為多個小文件
復制代碼 代碼如下:

將大文件切分成多個小文件
 public static void splitFile(String filePath, long unit) throws IOException
 {
     File file = new File(filePath);
     if (!file.exists()) throw new RuntimeException("file does not exist.");
     long size = file.length();
     if (unit >= size) return;
     int count = size % unit == 0 ? (int)(size/unit) : (int)(size/unit) + 1;
     String newFile = null;
     FileOutputStream fos = null;
     FileInputStream fis =null;
     byte[] buffer = new byte[(int)unit];
     fis = new FileInputStream(file);
     long startPos = 0;
     String countFile = filePath + "_Count";
     PrintWriter writer = new PrintWriter(new FileWriter( new File(countFile)));
     writer.println(filePath + "/t" + size);
     for (int i = 1; i <= count; i++)
     {
         newFile = filePath + "_" + i;
         startPos = (i - 1) * unit;
         System.out.println("Creating " + newFile);
         fos = new FileOutputStream(new File(newFile));
         int bytesRead = fis.read(buffer, 0, buffer.length);
         if (bytesRead != -1)
         {
             fos.write(buffer, 0, bytesRead);
             writer.println(newFile + "/t" + startPos + "/t" + bytesRead);
         }
         fos.flush();
         fos.close();
         System.out.println("StartPos:" + i*unit + "; EndPos:" + (i*unit + bytesRead));
     }
     writer.flush();
     writer.close();
     fis.close();
 }

將多個小文件合并為一個大文件
復制代碼 代碼如下:

將多個小文件合并成一個大文件
 public static void linkFiles(String countFile) throws IOException
 {
     File file = new File(countFile);
     if (!file.exists()) throw new RuntimeException("Count file does not exist.");
     BufferedReader reader = new BufferedReader(new FileReader(file));
     String line = reader.readLine();
     String newFile = line.split("/t")[0];
     long size = Long.parseLong(line.split("/t")[1]);
     RandomAccessFile raf = new RandomAccessFile(newFile, "rw");
     raf.setLength(size);
     FileInputStream fis = null;
     byte[] buffer = null;

     while((line = reader.readLine()) != null)
     {
         String[] arrInfo = line.split("/t");
         fis = new FileInputStream(new File(arrInfo[0]));
         buffer = new byte[Integer.parseInt(arrInfo[2])];
         long startPos = Long.parseLong(arrInfo[1]);
         fis.read(buffer, 0, Integer.parseInt(arrInfo[2]));
         raf.seek(startPos);
         raf.write(buffer, 0, Integer.parseInt(arrInfo[2]));
         fis.close();
     }
     raf.close();
 }

執行外部命令
復制代碼 代碼如下:

執行外部命令
 public static void execExternalCommand(String command, String argument)
 {
     Process process = null;
     try
     {
         process = Runtime.getRuntime().exec(command + " " + argument);
         InputStream is = process.getInputStream();
         BufferedReader br = new BufferedReader(new InputStreamReader(is));
         String line = null;
         while((line = br.readLine()) != null)
         {
             System.out.println(line);
         }
     }
     catch(Exception ex)
     {
         System.err.println(ex.getMessage());
     }
     finally
     {
         if (process != null) process.destroy();
     }
 }

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产免费亚洲高清| 国产精品一香蕉国产线看观看| 国产精品揄拍500视频| 92看片淫黄大片欧美看国产片| 日韩av快播网址| 日本精品久久中文字幕佐佐木| 亚洲一区二区三区视频播放| 欧美成人精品在线播放| 久久精品亚洲热| 中文精品99久久国产香蕉| 奇米成人av国产一区二区三区| 成人性生交大片免费看小说| 欧美激情性做爰免费视频| 国产婷婷成人久久av免费高清| 欧美成人亚洲成人日韩成人| 国产精品极品美女粉嫩高清在线| 欧美性猛交xxxxx免费看| 国产精品久久久久久一区二区| 亚洲最大成人免费视频| 国产精品久久网| 久久精品99无色码中文字幕| 欧美日韩国产一中文字不卡| 色久欧美在线视频观看| 26uuu久久噜噜噜噜| 国产精品无av码在线观看| 中文字幕亚洲一区二区三区| 亚洲最大福利网站| 国产一区二区在线免费视频| 尤物九九久久国产精品的特点| 中文字幕日韩av电影| 亚洲精品免费一区二区三区| 91九色视频在线| 精品久久久久久久久久ntr影视| 日韩视频免费大全中文字幕| 精品国产网站地址| 欧美激情日韩图片| 国产91精品久久久久久久| 亚洲一区美女视频在线观看免费| 久久精品国产久精国产一老狼| 色综合老司机第九色激情| 欧美日韩亚洲成人| 亚洲成色777777女色窝| 成人网页在线免费观看| 欧美国产亚洲视频| 亚洲国内精品在线| 中文字幕欧美日韩在线| 亚洲性视频网址| 裸体女人亚洲精品一区| 亚洲成av人乱码色午夜| 成人福利在线视频| 欧美国产精品va在线观看| 91丨九色丨国产在线| 日韩中文字幕在线播放| 成人免费大片黄在线播放| 亚洲精品电影在线| 亚洲精品视频中文字幕| 亚洲精品久久久一区二区三区| 日产精品久久久一区二区福利| 这里只有精品视频| 亚洲免费人成在线视频观看| 欧美性高潮床叫视频| 国产精品黄页免费高清在线观看| 国产suv精品一区二区三区88区| 欧美日韩在线另类| 一本色道久久88精品综合| 一本色道久久88精品综合| 中文字幕日本欧美| 日av在线播放中文不卡| 亚洲午夜久久久久久久| 国产精品wwwwww| 成人午夜在线影院| 亚洲欧洲av一区二区| 日韩精品视频免费在线观看| 亚洲一区二区三区777| 国产精品678| 亚洲xxxx妇黄裸体| 国产热re99久久6国产精品| 欧美成人午夜剧场免费观看| 久久亚洲欧美日韩精品专区| 亚洲日本成人女熟在线观看| 97精品久久久中文字幕免费| 色哟哟网站入口亚洲精品| 国产伦精品一区二区三区精品视频| 91网在线免费观看| 不用播放器成人网| 欧美日韩亚洲视频| 亚洲性线免费观看视频成熟| 日韩免费av在线| 中文字幕一区日韩电影| 精品中文字幕视频| 国内精品小视频| 久久好看免费视频| 国产精品福利网站| 欧美日韩一区二区三区在线免费观看| 欧美高跟鞋交xxxxxhd| 高清亚洲成在人网站天堂| 国产乱肥老妇国产一区二| 午夜精品久久久久久久99黑人| 久久视频国产精品免费视频在线| 国产精品欧美亚洲777777| 中文字幕在线国产精品| 国产91在线视频| 青青久久aⅴ北条麻妃| 欧美专区中文字幕| 欧洲一区二区视频| 久久福利网址导航| 欧美与欧洲交xxxx免费观看| 成人免费观看49www在线观看| 夜夜躁日日躁狠狠久久88av| 欧美中文在线免费| 欧美黄色片在线观看| 亚洲一区二区三区在线免费观看| 国产精品久久999| 中国人与牲禽动交精品| 亚洲在线免费视频| 日韩电影免费观看在线| 亚洲视频免费一区| 亚洲а∨天堂久久精品9966| 性色av一区二区咪爱| 日韩美女在线观看| 成人精品福利视频| 国产精品久久久av| 久久成人免费视频| 91黑丝在线观看| 色婷婷**av毛片一区| 北条麻妃一区二区三区中文字幕| 日本一区二区三区四区视频| 日韩中文字幕国产精品| 国产日韩欧美日韩大片| 91在线观看免费网站| 国产精品久久久久91| 亚洲综合日韩中文字幕v在线| 欧美性猛交丰臀xxxxx网站| 97精品一区二区视频在线观看| 欧美激情图片区| 久久夜色精品国产| 成人在线激情视频| 美女性感视频久久久| 欧美激情一区二区三区高清视频| 欧美视频中文在线看| 国内精品美女av在线播放| 欧美日韩在线免费观看| 2019日本中文字幕| 亚洲丁香久久久| 欧美激情欧美激情| 国产日韩欧美电影在线观看| 中文字幕不卡av| 2023亚洲男人天堂| 国产精品一区二区性色av| 欧美激情乱人伦| 日本亚洲欧美成人| 亚洲精品国产精品国自产在线| 久久久久久久一区二区| 久久久久久久久中文字幕| 久久久国产91| 亚洲新声在线观看| 欧美激情a在线| 欧美高跟鞋交xxxxxhd| 日韩动漫免费观看电视剧高清| 日韩在线免费av| 久久久久久中文字幕| 中文字幕9999| 国产欧美日韩91| 国产精品日本精品|