• <sub id="h4knl"><ol id="h4knl"></ol></sub>
    <sup id="h4knl"></sup>
      <sub id="h4knl"></sub>

      <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
      1. <strong id="h4knl"></strong>

      2. Java如何讀取文件內容再編輯

        時間:2024-10-16 15:58:27 SUN認證 我要投稿
        • 相關推薦

        Java如何讀取文件內容再編輯

          有時候,我們需要將讀取文件的內容到一個byte[] 數組中,然后對這個數組進行一些修改,這時,我們可以借助于ByteArrayOutputStream 這個類來實現。

        Java如何讀取文件內容再編輯

          ByteArrayOutputStream,顧名思義,同樣是一個OutputStream,那么,對于它的寫入操作,和其他的 OutputStream應該是沒有什么兩樣,寫入代碼可以說是隨手拈來的,與其他輸出流的不同之處在于,ByteArrayOutputStream寫入到內存中,并提供一個 toByteArray() 方法返回我們所需要的byte[]。

          Java代碼

          //保存上傳的附件信息

          String filename = files[0];

          String filepath = "upload/append/"+files[1];

          BufferedInputStream in = new BufferedInputStream(new FileInputStream(filepath));

          ByteArrayOutputStream out = new ByteArrayOutputStream(2048);

          System.out.println("available bytes"+in.available());

          byte[] temp = new byte[2048];

          int size =0;

          while((size = in.read(temp))!=-1){ out.write(temp,0,size);

          }

          in.close();

          byte[] content = out.toByteArray();

          String stream = new BaseCode64().EncodeBase64(content);

          System.out.println("content" +content.length);

          effect += insertAttachmentInfo(conn,attNo,processId,filename,stream);

          這里把讀取的流以base64保存,使用了類BaseCode64()的方法EncodeBase64。

          代碼如下:

          Java代碼

          package com.ving.xzfw.util;

          import java.io.ByteArrayOutputStream;

          public class BaseCode64 {

          private String TableBase64;

          private String FError;

          public BaseCode64()

          {

          TableBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

          FError = new String();

          FError = "";

          }

          public String DecodeBase64(byte [] Value){

          ByteArrayOutputStream o = new ByteArrayOutputStream();

          byte d[] = new byte[4];

          try

          {

          int count = 0;

          byte x[] = Value;

          do

          {

          if(count >= x.length)

          {

          break;

          }

          for(int n = 0; n <= 3; n++)

          {

          if(count >= x.length)

          {

          d[n] = 64;

          } else

          {

          int y = TableBase64.indexOf(x[count]);

          if(y < 0)

          {

          y = 65;

          }

          d[n] = (byte)y;

          }

          count++;

          }

          o.write((byte)(((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));

          if(d[2] != 64)

          {

          o.write((byte)(((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));

          if(d[3] != 64)

          {

          o.write((byte)(((d[2] & 3) << 6) + (d[3] & 0x3f)));

          }

          }

          } while(true);

          }

          catch(StringIndexOutOfBoundsException e)

          {

          FError = String.valueOf(String.valueOf(FError)) + String.valueOf(String.valueOf(e.toString()));

          System.out.println(e.toString());

          }

          return o.toString();

          }

          public String EncodeBase64(byte [] Value){

          ByteArrayOutputStream o = new ByteArrayOutputStream();

          byte d[] = new byte[4];

          try

          {

          int count = 0;

          for(byte x[] = Value; count < x.length;)

          {

          byte c = x[count];

          count++;

          d[0] = (byte)((c & 0xfc) >> 2);

          d[1] = (byte)((c & 3) << 4);

          if(count < x.length)

          {

          c = x[count];

          count++;

          d[1] = (byte)(d[1] + (byte)((c & 0xf0) >> 4));

          d[2] = (byte)((c & 0xf) << 2);

          if(count < x.length)

          {

          c = x[count];

          count++;

          d[2] = (byte)(d[2] + ((c & 0xc0) >> 6));

          d[3] = (byte)(c & 0x3f);

          } else

          {

          d[3] = 64;

          }

          } else

          {

          d[2] = 64;

          d[3] = 64;

          }

          int n = 0;

          while(n <= 3)

          {

          o.write(TableBase64.charAt(d[n]));

          n++;

          }

          }

          }

          catch(StringIndexOutOfBoundsException e)

          {

          FError = String.valueOf(String.valueOf(FError)) + String.valueOf(String.valueOf(e.toString()));

          System.out.println(e.toString());

          }

          return o.toString();

          }

          }

        《&.doc》
        将本文的Word文档下载到电脑,方便收藏和打印
        推荐度:
        点击下载文档

        【Java如何讀取文件內容再編輯】相關文章:

        Android讀取本地json文件的方法07-06

        Java文件解壓縮示例08-21

        新建立的網站如何編輯用戶喜歡的內容06-19

        php使用fgetcsv讀取csv文件出現亂碼怎么辦08-16

        java認證考試培訓內容06-25

        Java文件解壓縮實例詳解201607-26

        如何編譯java程序09-28

        用PHP腳本在Linux系統上讀取輸入和對文件進行操作10-28

        TTF字體文件如何安裝11-03

        Excel文件如何設置密碼08-25

        国产高潮无套免费视频_久久九九兔免费精品6_99精品热6080YY久久_国产91久久久久久无码
      3. <sub id="h4knl"><ol id="h4knl"></ol></sub>
        <sup id="h4knl"></sup>
          <sub id="h4knl"></sub>

          <sub id="h4knl"><ol id="h4knl"><em id="h4knl"></em></ol></sub><s id="h4knl"></s>
          1. <strong id="h4knl"></strong>

          2. 亚洲视频i在线 | 亚洲国产99精品国自产 | 在线观看亚洲一级影视 | 日本一高清二区视频久而久二 | 精品国产经典三级在线看 | 亚洲色精品Aⅴ一区区三区 亚洲1级欧美高清 |

            Java如何讀取文件內容再編輯

              有時候,我們需要將讀取文件的內容到一個byte[] 數組中,然后對這個數組進行一些修改,這時,我們可以借助于ByteArrayOutputStream 這個類來實現。

            Java如何讀取文件內容再編輯

              ByteArrayOutputStream,顧名思義,同樣是一個OutputStream,那么,對于它的寫入操作,和其他的 OutputStream應該是沒有什么兩樣,寫入代碼可以說是隨手拈來的,與其他輸出流的不同之處在于,ByteArrayOutputStream寫入到內存中,并提供一個 toByteArray() 方法返回我們所需要的byte[]。

              Java代碼

              //保存上傳的附件信息

              String filename = files[0];

              String filepath = "upload/append/"+files[1];

              BufferedInputStream in = new BufferedInputStream(new FileInputStream(filepath));

              ByteArrayOutputStream out = new ByteArrayOutputStream(2048);

              System.out.println("available bytes"+in.available());

              byte[] temp = new byte[2048];

              int size =0;

              while((size = in.read(temp))!=-1){ out.write(temp,0,size);

              }

              in.close();

              byte[] content = out.toByteArray();

              String stream = new BaseCode64().EncodeBase64(content);

              System.out.println("content" +content.length);

              effect += insertAttachmentInfo(conn,attNo,processId,filename,stream);

              這里把讀取的流以base64保存,使用了類BaseCode64()的方法EncodeBase64。

              代碼如下:

              Java代碼

              package com.ving.xzfw.util;

              import java.io.ByteArrayOutputStream;

              public class BaseCode64 {

              private String TableBase64;

              private String FError;

              public BaseCode64()

              {

              TableBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

              FError = new String();

              FError = "";

              }

              public String DecodeBase64(byte [] Value){

              ByteArrayOutputStream o = new ByteArrayOutputStream();

              byte d[] = new byte[4];

              try

              {

              int count = 0;

              byte x[] = Value;

              do

              {

              if(count >= x.length)

              {

              break;

              }

              for(int n = 0; n <= 3; n++)

              {

              if(count >= x.length)

              {

              d[n] = 64;

              } else

              {

              int y = TableBase64.indexOf(x[count]);

              if(y < 0)

              {

              y = 65;

              }

              d[n] = (byte)y;

              }

              count++;

              }

              o.write((byte)(((d[0] & 0x3f) << 2) + ((d[1] & 0x30) >> 4)));

              if(d[2] != 64)

              {

              o.write((byte)(((d[1] & 0xf) << 4) + ((d[2] & 0x3c) >> 2)));

              if(d[3] != 64)

              {

              o.write((byte)(((d[2] & 3) << 6) + (d[3] & 0x3f)));

              }

              }

              } while(true);

              }

              catch(StringIndexOutOfBoundsException e)

              {

              FError = String.valueOf(String.valueOf(FError)) + String.valueOf(String.valueOf(e.toString()));

              System.out.println(e.toString());

              }

              return o.toString();

              }

              public String EncodeBase64(byte [] Value){

              ByteArrayOutputStream o = new ByteArrayOutputStream();

              byte d[] = new byte[4];

              try

              {

              int count = 0;

              for(byte x[] = Value; count < x.length;)

              {

              byte c = x[count];

              count++;

              d[0] = (byte)((c & 0xfc) >> 2);

              d[1] = (byte)((c & 3) << 4);

              if(count < x.length)

              {

              c = x[count];

              count++;

              d[1] = (byte)(d[1] + (byte)((c & 0xf0) >> 4));

              d[2] = (byte)((c & 0xf) << 2);

              if(count < x.length)

              {

              c = x[count];

              count++;

              d[2] = (byte)(d[2] + ((c & 0xc0) >> 6));

              d[3] = (byte)(c & 0x3f);

              } else

              {

              d[3] = 64;

              }

              } else

              {

              d[2] = 64;

              d[3] = 64;

              }

              int n = 0;

              while(n <= 3)

              {

              o.write(TableBase64.charAt(d[n]));

              n++;

              }

              }

              }

              catch(StringIndexOutOfBoundsException e)

              {

              FError = String.valueOf(String.valueOf(FError)) + String.valueOf(String.valueOf(e.toString()));

              System.out.println(e.toString());

              }

              return o.toString();

              }

              }