Android:java byte数组文件读写学习实例

Android:java byte数组文件读写学习实例


一、该实例把byte写入一个image_data.txt文件,然后再用两种方法读取image_data.txt文件里面的byte内容然后分别写入image_read_and_write_data.txt、image_read_and_write_data_2.txt文件。

二、实例学习
    1、java代码java_write_read_byte.java
import java.io.ByteArrayOutputStream;  
import java.io.File;  
import java.io.FileInputStream;  
import java.io.FileOutputStream;  
import java.io.IOException;  
public class java_write_read_byte
{   
    public static void main(String[] args) 
	{  
	   byte[] image = new byte[50];
	   byte[] image_read_buffer;
	   for(byte i=0;i<50;i++)
	   {
  		   image[i]=(byte)('a'+i%25);//+(byte)i%24;
	   }
	   FileHelper mFileHelper = new FileHelper();
	   System.out.println("java_write_read_byte program starting!");
	   try 
	    {  
		    System.out.println("write byte data to image_data.txt");
            mFileHelper.createFile("image_data.txt", image); 
        } catch (Exception e) {  
            System.err.println(e.getMessage());  
            e.printStackTrace();  
        } 
		try 
	    {  
		    System.out.println("use getContent to read the byte data from image_data.txt,then write data to image_read_and_write_data.txt");
			mFileHelper.createFile("image_read_and_write_data.txt", mFileHelper.getContent("image_data.txt"));
            	
        } catch (Exception e) {  
            System.err.println(e.getMessage());  
            e.printStackTrace();  
        } 
       	
        try 
	    {  
		    System.out.println("use getContent2 to read the byte data from image_data.txt,then write data to image_read_and_write_data.txt");
            mFileHelper.createFile("image_read_and_write_data_2.txt", mFileHelper.getContent2("image_data.txt")); 
        } catch (Exception e) {  
            System.err.println(e.getMessage());  
            e.printStackTrace();  
        } 
    } 

}  
class FileHelper {  
    public byte[] getContent(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[] getContent2(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 createFile(String path, byte[] content) throws IOException {  
  
        FileOutputStream fos = new FileOutputStream(path);  
  
        fos.write(content);  
        fos.close();  
    }  
  
} 	  

  
  
 
    2、编译&执行

   
3、查看image_data.txt、image_read_and_write_data.txt、image_read_and_write_data_2.txt文件的内容

    4、结果显示3个文件的内容都是一模一样,说明读写成功了,恭喜你。
发布了112 篇原创文章 · 获赞 77 · 访问量 36万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览