当前位置:首页 > 编程笔记 > 正文
已解决

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

来自网友在路上 174874提问 提问时间:2023-11-01 18:45:03阅读次数: 74

最佳答案 问答题库748位专家为你答疑解惑

一、读文件内容到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() - 灰信网(软件开发博客聚合)

查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"20231101 java 一次性将文件内容读到byte[]中,写int[] data数组内容到raw文件":http://eshow365.cn/6-29541-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!