美文网首页
Android---Java文件存取

Android---Java文件存取

作者: this_is_for_u | 来源:发表于2017-06-17 09:17 被阅读130次

    android需声明文件权限:

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    

    java二进制文件的存储:

        try{
                File file = new File(fileName);
                if(!file.exists()) file.createNewFile();
                FileOutputStream outputStream = new FileOutputStream(file);
                outputStream.write("hello world");
                outputStream.flush();
                outputStream.close();
            }catch (FileNotFoundException e){
                e.printStackTrace();
            }catch (IOException e){
                e.printStackTrace();
            }
    

    注意:输入输出流用完后及时关闭。

    java二进制文件的读取:

    String fileName = "/sdcard/feature.db";
            File file = new File(fileName);
            if(!file.exists()) return;
            try{
                FileInputStream fis = new FileInputStream(file);
                byte[] buffer = new byte[size];
                while(fis.read(buffer) > 0){
                    Sysout(buffer.toString());
                }
                fis.close();
            }catch (Exception e){
                e.printStackTrace();
            }
    

    按行写入数据

                file = new File(ind);
                if (!file.exists()) file.createNewFile();
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file, true)));
                out.println(filePath + "---" + faceimg.width + "---" + faceimg.height);
                out.close();
    

    从文件中按行读取数据:

      File file = new File(path);
            try {
                FileInputStream instream = new FileInputStream(fileName);
                if (instream != null)
                {
                    InputStreamReader inputreader = new InputStreamReader(instream);
                    BufferedReader buffreader = new BufferedReader(inputreader);
                    String line;
                    //分行读取
                    while (( line = buffreader.readLine()) != null) {
                        
                        ///
                    }
                    buffreader.close();
                    inputreader.close();
                    instream.close();
    //注意关闭的顺序,先打开后关闭
                }
            }
            catch (FileNotFoundException e)
            {
                e.printStackTrace();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
    

    使用BufferedWriter写入文件,可以直接写字符串,数组,字符数据保存到文件中:

        try {
                String str = "hello";
                File file = new File(FileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileWriter fw = new FileWriter(file);
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(str);
                bw.close();
                fw.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    

    将NV21格式图片转成jpg压缩并存储:

    //face为NV21格式
          public Bitmap pictureTrans(byte[] face, int width, int height){
            if(face == null) return null;
            YuvImage image = new YuvImage(face, ImageFormat.NV21, width, height, null);
            Rect rect = new Rect(0, 0, width, height);
            ByteArrayOutputStream os = new ByteArrayOutputStream(face.length);
            image.compressToJpeg(rect, 80, os);
            byte[] tmp = os.toByteArray();
            Bitmap bmp = BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
            return bmp;
        }
    ----------------------------------  //文件操作
            File file = new File("/sdcard/myFolder");
            if(!file.exists())
                file.mkdir();
            String filePath = "/sdcard/myFolder/" + "hello.jpg";
            file = new File(filePath);
            try{
                file.createNewFile();
                FileOutputStream fos = new FileOutputStream(file);
                bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
                fos.flush();
                fos.close();
            }catch (IOException e){
                e.printStackTrace();
            }
    

    将图片读取出来

    //android中直接使用BitmapFactory即可
        public Bitmap readImage(String filePath){
            Bitmap bitmap = BitmapFactory.decodeFile(filePath);
            return bitmap;
        }
    

    获取文件的大小:

    //文件大小小于2GB时可用,大文件需用其它方法
        public long getFileSize(File file){
            int size = 0;
            if(file.exists()){
                try{
                    FileInputStream fis = new FileInputStream(file);
                    size = fis.available();
                    fis.close();
                }catch (Exception e){
                    e.printStackTrace();
                }
    
            }
            return size;
        }
    

    判断sd是否存在

    public boolean isSDCardExist() {
            return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
        }
    

    获取sd卡根目录:

    public String getSDCardPath() {
            boolean exist = isSDCardExist();
            String path = "";
            if (exist) {
                path = Environment.getExternalStorageDirectory().getAbsolutePath();
            } else {
                path = "error";
            }
            return path;
        }
    

    !!!用于记录日常工作收获,后续更新及整理
    引用:
    http://blog.csdn.net/ccpat/article/details/45727427
    http://www.cnblogs.com/rossoneri/p/3995096.html

    相关文章

      网友评论

          本文标题:Android---Java文件存取

          本文链接:https://www.haomeiwen.com/subject/mpyvqxtx.html