java byte【】數組與文件讀寫(增加新功能)


今天在測試直接寫的文章:

java byte【】數組與文件讀寫

時,想調用FileHelper類對字節數組以追加的方式寫文件,結果無論怎樣竟然數據錄入不全,重新看了下文件的追加模式,提供了兩種方式:

方式一:

字節數組寫入文件(不追加)

//將byte數組寫入文件
    public void createFile(String path, byte[] content) throws IOException {

        FileOutputStream fos = new FileOutputStream(path);

        fos.write(content);
        fos.close();
    }

在此基礎上更改為:字節數組寫入文件(追加)

/* 方法1:
     * 將byte數組(追加)寫入文件
     * 
     * */
     public void createFileAdd(String path, byte[] content, boolean Appendable) throws IOException {
//         程序寫好之后每次存儲數據都刷新
         //FileOutputStream fos = new FileOutputStream(path);
//         研究了一下,原來FileOutPutStream也可以設置模式的,只是和openFileOutput不一樣 我這樣寫FileOutputStream fos=new FileOutputStream(_sdpath1,Appendable);就可以實現數據追加功能
         FileOutputStream fos=new FileOutputStream(path,Appendable);
         fos.write(content);
         fos.write("\r\n".getBytes());
         fos.close();
     }

方式二:

直接寫一個方法,該方法中的核心代碼在需要哪種方式(追加或者不追加時,注釋更改即可

/** 
     * 方法二:
     * 根據byte數組,生成文件 
     */  
    public  void writeFile(byte[] bfile, String filePath,String fileName) {  
        BufferedOutputStream bos = null;  

        File file = null;  
        try {  
            File dir = new File(filePath);  
            if(!dir.exists()&&dir.isDirectory()){//判斷文件目錄是否存在  
                dir.mkdirs();  
            } 
            file = new File(filePath+"\\"+fileName); 
           /* 使用以下2行代碼時,不追加方式*/
            /*bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(bfile); */
            
            /* 使用以下3行代碼時,追加方式*/
            bos = new BufferedOutputStream(new FileOutputStream(file, true));
            bos.write(bfile); 
            bos.write("\r\n".getBytes()); 
            
            
            bos.flush();
            
        } catch (Exception e) {  
            e.printStackTrace();  
        } finally {  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e1) {  
                    e1.printStackTrace();  
                }  
            }  
            
        }  
    }  

總之,對字節數組的操作類,提供上來,供以后復習用:

  1 import java.io.BufferedOutputStream;
  2 import java.io.BufferedWriter;
  3 import java.io.ByteArrayOutputStream;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileOutputStream;
  7 import java.io.FileWriter;
  8 import java.io.IOException;
  9 import java.io.OutputStreamWriter;
 10 
 11 public class FileHelper {
 12     //第一種獲取文件內容方式
 13     public byte[] getContent(String filePath) throws IOException {
 14         File file = new File(filePath);
 15 
 16         long fileSize = file.length();
 17         if (fileSize > Integer.MAX_VALUE) {
 18             System.out.println("file too big...");
 19             return null;
 20         }
 21 
 22         FileInputStream fi = new FileInputStream(file);
 23 
 24         byte[] buffer = new byte[(int) fileSize];
 25 
 26         int offset = 0;
 27 
 28         int numRead = 0;
 29 
 30         while (offset < buffer.length
 31 
 32         && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
 33 
 34             offset += numRead;
 35 
 36         }
 37 
 38         // 確保所有數據均被讀取
 39 
 40         if (offset != buffer.length) {
 41 
 42             throw new IOException("Could not completely read file "+ file.getName());
 43 
 44         }
 45 
 46         fi.close();
 47 
 48         return buffer;
 49     }
 50     
 51     //第二種獲取文件內容方式
 52     public byte[] getContent2(String filePath) throws IOException
 53     {
 54         FileInputStream in=new FileInputStream(filePath);
 55         
 56         ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
 57         
 58         System.out.println("bytes available:"+in.available());
 59         
 60         byte[] temp=new byte[1024];
 61         
 62         int size=0;
 63         
 64         while((size=in.read(temp))!=-1)
 65         {
 66             out.write(temp,0,size);
 67         }
 68         
 69         in.close();
 70         
 71         byte[] bytes=out.toByteArray();
 72         System.out.println("bytes size got is:"+bytes.length);
 73         
 74         return bytes;
 75     }
 76     //將byte數組寫入文件
 77     public void createFile(String path, byte[] content) throws IOException {
 78 
 79         FileOutputStream fos = new FileOutputStream(path);
 80 
 81         fos.write(content);
 82         fos.close();
 83     }
 84     /* 方法1:
 85      * 將byte數組(追加)寫入文件
 86      * 
 87      * */
 88      public void createFileAdd(String path, byte[] content, boolean Appendable) throws IOException {
 89 //         程序寫好之后每次存儲數據都刷新
 90          //FileOutputStream fos = new FileOutputStream(path);
 91 //         研究了一下,原來FileOutPutStream也可以設置模式的,只是和openFileOutput不一樣 我這樣寫FileOutputStream fos=new FileOutputStream(_sdpath1,Appendable);就可以實現數據追加功能
 92          FileOutputStream fos=new FileOutputStream(path,Appendable);
 93          fos.write(content);
 94          fos.write("\r\n".getBytes());
 95          fos.close();
 96      }
 97     /** 
 98      * 方法二:
 99      * 根據byte數組,生成文件 
100      */  
101     public  void writeFile(byte[] bfile, String filePath,String fileName) {  
102         BufferedOutputStream bos = null;  
103 
104         File file = null;  
105         try {  
106             File dir = new File(filePath);  
107             if(!dir.exists()&&dir.isDirectory()){//判斷文件目錄是否存在  
108                 dir.mkdirs();  
109             } 
110             file = new File(filePath+"\\"+fileName); 
111            /* 使用以下2行代碼時,不追加方式*/
112             /*bos = new BufferedOutputStream(new FileOutputStream(file));
113             bos.write(bfile); */
114             
115             /* 使用以下3行代碼時,追加方式*/
116             bos = new BufferedOutputStream(new FileOutputStream(file, true));
117             bos.write(bfile); 
118             bos.write("\r\n".getBytes()); 
119             
120             
121             bos.flush();
122             
123         } catch (Exception e) {  
124             e.printStackTrace();  
125         } finally {  
126             if (bos != null) {  
127                 try {  
128                     bos.close();  
129                 } catch (IOException e1) {  
130                     e1.printStackTrace();  
131                 }  
132             }  
133             
134         }  
135     }  
136 }
FileHelper.java

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM