成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Java回顧之I/O

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

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

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

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

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

    • FileInputStream:操作文件,經(jīng)常和BufferedInputStream一起使用
    • PipedInputStream:可用于線程間通信
    • ObjectInputStream:可用于對象序列化
    • ByteArrayInputStream:用于處理字節(jié)數(shù)組的輸入
    • LineNumberInputStream:可輸出當(dāng)前行數(shù),并且可以在程序中進(jìn)行修改

  下面是OutputStream的結(jié)構(gòu)

  

    • PrintStream:提供了類似print和println的接口去輸出數(shù)據(jù)

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

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

 

 #p#

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

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

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

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

  

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

  1. 使用BufferedReader讀取文件內(nèi)容 
  2.  public static String readFile(String file)throws IOException 
  3.  { 
  4.      BufferedReader br = null
  5.      StringBuffer sb = new StringBuffer(); 
  6.      try 
  7.      { 
  8.          br = new BufferedReader(new FileReader(file)); 
  9.          String line = null
  10.           
  11.          while((line = br.readLine()) != null
  12.          { 
  13.              sb.append(line); 
  14.          } 
  15.      } 
  16.      catch(Exception ex) 
  17.      { 
  18.          System.out.println("Error occurs during reading " + file); 
  19.      } 
  20.      finally 
  21.      { 
  22.          if (br != null) br.close(); 
  23.      } 
  24.      return sb.toString(); 
  25.  } 

 

 

  1. 使用BufferedWriter復(fù)制文件 
  2.  public static void copyFile(String file) throws IOException 
  3.  {  
  4.      BufferedReader br = null
  5.      BufferedWriter bw = null
  6.      try 
  7.      { 
  8.          br = new BufferedReader(new FileReader(file)); 
  9.          bw = new BufferedWriter(new FileWriter(file + ".bak")); 
  10.          String line = null
  11.          while((line = br.readLine())!= null
  12.          { 
  13.              bw.write(line); 
  14.          } 
  15.      } 
  16.      catch(Exception ex) 
  17.      { 
  18.          System.out.println("Error occurs during copying " + file); 
  19.      } 
  20.      finally 
  21.      { 
  22.          if (br != null) br.close(); 
  23.          if (bw != null) bw.close(); 
  24.      } 
  25.  } 

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

 

  1. 創(chuàng)建大小固定的文件 
  2.  public static void createFile(String file, int size) throws IOException 
  3.  { 
  4.      File temp = new File(file); 
  5.      RandomAccessFile raf = new RandomAccessFile(temp, "rw"); 
  6.      raf.setLength(size); 
  7.      raf.close(); 
  8.  } 

 

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

 

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

 

 

  1. 復(fù)制文件 
  2.  public static void copyFile(String sourceFile, String destFile) throws IOException 
  3.  { 
  4.      File source = new File(sourceFile); 
  5.      if (!source.exists()) throw new RuntimeException("File does not exist."); 
  6.      if (!source.isFile()) throw new RuntimeException("It is not file."); 
  7.      if (!source.canRead()) throw new RuntimeException("File cound not be read."); 
  8.      File dest = new File(destFile); 
  9.      if (dest.exists()) 
  10.      { 
  11.          if (dest.isDirectory()) throw new RuntimeException("Destination is a folder."); 
  12.          else 
  13.          { 
  14.              dest.delete(); 
  15.          } 
  16.      } 
  17.      else 
  18.      { 
  19.          File parentFolder = new File(dest.getParent()); 
  20.          if (!parentFolder.exists()) parentFolder.mkdirs(); 
  21.          if (!parentFolder.canWrite()) throw new RuntimeException("Destination can not be written."); 
  22.      } 
  23.      FileInputStream fis = null
  24.      FileOutputStream fos = null
  25.      try 
  26.      { 
  27.          fis = new FileInputStream(source); 
  28.          fos = new FileOutputStream(dest); 
  29.          byte[] buffer = new byte[1024]; 
  30.          int bytesRead = 0
  31.          while((bytesRead = fis.read(buffer, 0, buffer.length)) != -1
  32.          { 
  33.              fos.write(buffer, 0, bytesRead); 
  34.          } 
  35.          fos.flush(); 
  36.      } 
  37.      catch(IOException ex) 
  38.      { 
  39.          System.out.println("Error occurs during copying " + sourceFile); 
  40.      } 
  41.      finally 
  42.      { 
  43.          if (fis != null) fis.close(); 
  44.          if (fos != null) fos.close(); 
  45.      } 
  46.  } 

 

 

  1. 復(fù)制文件夾 
  2.  public static void copyDir(String sourceDir, String destDir) throws IOException 
  3.  { 
  4.       
  5.      File source = new File(sourceDir); 
  6.      if (!source.exists()) throw new RuntimeException("Source does not exist."); 
  7.      if (!source.canRead()) throw new RuntimeException("Source could not be read."); 
  8.      File dest = new File(destDir); 
  9.      if (!dest.exists()) dest.mkdirs(); 
  10.       
  11.      File[] arrFiles = source.listFiles(); 
  12.      for(int i = 0; i < arrFiles.length; i++) 
  13.      { 
  14.          if (arrFiles[i].isFile()) 
  15.          { 
  16.              BufferedReader reader = new BufferedReader(new FileReader(arrFiles[i])); 
  17.              BufferedWriter writer = new BufferedWriter(new FileWriter(destDir + "/" + arrFiles[i].getName())); 
  18.              String line = null
  19.              while((line = reader.readLine()) != null) writer.write(line); 
  20.              writer.flush(); 
  21.              reader.close(); 
  22.              writer.close(); 
  23.          } 
  24.          else 
  25.          { 
  26.              copyDir(sourceDir + "/" + arrFiles[i].getName(), destDir + "/" + arrFiles[i].getName()); 
  27.          } 
  28.      } 
  29.  } 

 

 

  1. 刪除文件夾 
  2.  public static void del(String filePath) 
  3.  { 
  4.      File file = new File(filePath); 
  5.      if (file == null || !file.exists()) return
  6.      if (file.isFile()) 
  7.      { 
  8.          file.delete(); 
  9.      } 
  10.      else 
  11.      { 
  12.          File[] arrFiles = file.listFiles(); 
  13.          if (arrFiles.length > 0
  14.          { 
  15.              for(int i = 0; i < arrFiles.length; i++) 
  16.              { 
  17.                  del(arrFiles[i].getAbsolutePath()); 
  18.              } 
  19.          } 
  20.          file.delete(); 
  21.      } 
  22.  } 

 

 

  1. 將大文件切分成多個(gè)小文件 
  2.  public static void splitFile(String filePath, long unit) throws IOException 
  3.  { 
  4.      File file = new File(filePath); 
  5.      if (!file.exists()) throw new RuntimeException("file does not exist."); 
  6.      long size = file.length(); 
  7.      if (unit >= size) return
  8.      int count = size % unit == 0 ? (int)(size/unit) : (int)(size/unit) + 1
  9.      String newFile = null
  10.      FileOutputStream fos = null
  11.      FileInputStream fis =null
  12.      byte[] buffer = new byte[(int)unit]; 
  13.      fis = new FileInputStream(file); 
  14.      long startPos = 0
  15.      String countFile = filePath + "_Count"
  16.      PrintWriter writer = new PrintWriter(new FileWriter( new File(countFile))); 
  17.      writer.println(filePath + "\t" + size); 
  18.      for (int i = 1; i <= count; i++) 
  19.      { 
  20.          newFile = filePath + "_" + i; 
  21.          startPos = (i - 1) * unit; 
  22.          System.out.println("Creating " + newFile); 
  23.          fos = new FileOutputStream(new File(newFile)); 
  24.          int bytesRead = fis.read(buffer, 0, buffer.length); 
  25.          if (bytesRead != -1
  26.          { 
  27.              fos.write(buffer, 0, bytesRead); 
  28.              writer.println(newFile + "\t" + startPos + "\t" + bytesRead); 
  29.          } 
  30.          fos.flush(); 
  31.          fos.close(); 
  32.          System.out.println("StartPos:" + i*unit + "; EndPos:" + (i*unit + bytesRead)); 
  33.      } 
  34.      writer.flush(); 
  35.      writer.close(); 
  36.      fis.close(); 
  37.  } 

 

 

原文鏈接:http://www.cnblogs.com/wing011203/archive/2013/05/03/3056535.html

 

責(zé)任編輯:陳四芳 來源: 博客園
相關(guān)推薦

2013-09-16 16:07:38

Java基礎(chǔ)IO

2022-12-08 09:10:11

I/O模型Java

2010-06-25 09:47:29

Linux系統(tǒng)監(jiān)控

2014-06-25 15:56:10

GoogleIO大會開發(fā)者

2013-05-23 14:25:44

JDBC

2013-09-17 13:43:51

IO

2013-09-17 15:13:28

IO

2011-12-19 14:05:01

JavaIO

2015-08-10 14:39:46

Java 操作建議

2011-05-15 15:13:14

2014-06-26 10:28:44

Google IO 2014

2018-11-05 11:20:54

緩沖IO

2011-01-14 09:25:28

LinuxIO機(jī)制

2020-06-03 17:30:42

LinuxIO

2020-12-11 11:04:07

NettyIO

2020-08-07 08:03:37

IONetty

2023-07-12 08:24:19

Java NIO通道

2021-02-10 08:09:48

Netty網(wǎng)絡(luò)多路復(fù)用

2017-01-19 19:14:20

Linux重定向命令

2020-06-10 08:28:51

Kata容器I
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號

主站蜘蛛池模板: 伊人久久国产 | 欧美日韩淫片 | 天天草视频 | 一级做a爰片性色毛片16 | 亚洲综合一区二区三区 | 久久久久国产一区二区三区不卡 | 国产精品久久久久久久久免费丝袜 | 中文字幕一区二区三区在线观看 | 狠狠av | 伦理午夜电影免费观看 | 亚洲视频一区在线观看 | 色婷婷久久 | 国产精品久久久久久久久久久免费看 | 91精品福利 | 91精品国产一区二区三区 | 97超碰免费 | 欧美a级成人淫片免费看 | 亚洲网站在线 | 搞黄网站在线观看 | 红色av社区 | 欧美日韩亚 | 偷偷操视频 | 视频第一区 | 欧产日产国产精品国产 | av成年人网站 | 欧美激情区 | 国产一区二区在线播放 | 中文字幕视频在线看5 | 91影院在线观看 | 成人av播放 | 欧美激情精品久久久久久 | 99免费视频 | 国产精品久久久久aaaa九色 | 一区二区三区亚洲 | 欧美色性 | 日本欧美国产在线观看 | 秋霞性生活 | 日本不卡免费新一二三区 | 国产精品精品视频 | 国产在线精品一区二区 | 日韩在线视频一区二区三区 |