Java読み書きbyte[]クラス

3670 ワード

package tools;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;

public class FileHelper {
    //           
    public byte[] readByte(String filePath) throws IOException {
        File file = new File(filePath);
        long fileSize = file.length();
        if (fileSize > Integer.MAX_VALUE) {
            System.out.println("file too big...");
            return null;
        }
        FileInputStream fi = new FileInputStream(file);
        byte[] buffer = new byte[(int) fileSize];
        int offset = 0;
        int numRead = 0;
        while (offset < buffer.length
                && (numRead = fi.read(buffer, offset, buffer.length - offset)) >= 0) {
            offset += numRead;
        }
        //           
        if (offset != buffer.length) {
            throw new IOException("Could not completely read file "+ file.getName());
        }
        fi.close();
        return buffer;
    }

    //           
    public byte[] readByte2(String filePath) throws IOException
    {
        FileInputStream in=new FileInputStream(filePath);
        ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
        System.out.println("bytes available:"+in.available());
        byte[] temp=new byte[1024];
        int size=0;
        while((size=in.read(temp))!=-1)
        {
            out.write(temp,0,size);
        }
        in.close();
        byte[] bytes=out.toByteArray();
        System.out.println("bytes size got is:"+bytes.length);
        return bytes;
    }
    // byte      
    public void WriteByteToFile(String path, byte[] content) throws IOException {
        FileOutputStream fos = new FileOutputStream(path);
        fos.write(content);
        fos.close();
    }
    /*   1:
     *  byte  (  )    
     *
     * */
    public void AppendByteToFile(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
".getBytes()); fos.close(); } /** * : * byte , */ public void AppendByteToFile(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
".getBytes()); bos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } } }