20231101 java 一次性将文件内容读到byte[]中,写int[] data数组内容到raw文件

一、读文件内容到byte代码

    public static byte[]  read_byte_data_from_file(String file_name)
    {
        byte[] buffer = new byte[0];
        File file = new File(file_name);
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(file);
                       long fileSize = file.length();
            buffer = new byte[(int) fileSize];
            int bytesRead = inputStream.read(buffer);
            inputStream.close();
            Log.i(TAG, "fileSize=" + fileSize + " buffer.length=" + buffer.length + " bytesRead=" + bytesRead);
                  return buffer;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return buffer;
    }

二、写int[] data 到raw文件里面 

    public  static boolean write_int_data_to_file(String file_name,int[] data,int len) {

        boolean is_success = false;
        OutputStream os = null;
        Log.d(TAG,"file_name:" + file_name);
        byte[] raw_image_buf = new byte[len * 2];
         for(int n = 0;n < len ;n++)
        {
            raw_image_buf[2 * n]     = (byte)(data[n] & 0xFF);
            raw_image_buf[2 * n + 1] = (byte)(data[n] >> 8 & 0xFF);
        }
       try {
            os = new FileOutputStream(new File( file_name ));
            os.write(raw_image_buf, 0, len * 2);
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
            os.close();
                is_success = true;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return is_success;
    }

三、参考文章

java 一次性将文件内容读到byte[]中_mob64ca12eb3858的技术博客_51CTO博客

Android 内部存储于外部存储以及getFilesDir()/getExternalFilesDir() - 灰信网(软件开发博客聚合)

你可能感兴趣的:(Java,Language,java,开发语言)