zoukankan      html  css  js  c++  java
  • 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("
    ".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("
    ".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("
    ".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("
    ".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
  • 相关阅读:
    java 可变參数列表
    Java -Xms -Xmx -Xss -XX:MaxNewSize -XX:MaxPermSize含义记录
    hdu 4939
    什么是堆和栈,它们在哪儿?
    PPAPI插件与浏览器的通信
    Java&amp;Xml教程(十一)JAXB实现XML与Java对象转换
    Heavy Transportation
    Python学习笔记-小记
    C/C++知识要点5——智能指针原理及自己定义实现
    小米2S电池电量用尽充电无法开机解决方法
  • 原文地址:https://www.cnblogs.com/shuqingstudy/p/5135851.html
Copyright © 2011-2022 走看看