Androidローカルファイル、フォルダ操作

6079 ワード

  • 文字列をテキストファイル(自動生成可能ファイル)
  • に書き込む.
  • ファイルの内容を読み取る
  • 生成ファイル
  • 生成フォルダ
  • ファイルまたはフォルダを削除する
  • ファイルまたはフォルダの名前
  • 文字列をテキストファイルに書き込む(自動的にファイルを生成可能)
     
        /**
         *             
         *   makeFilePath  
         */
        public static void writeTxtToFile(String strcontent, String filePath, String fileName) {
            //       ,     ,     
            makeFilePath(filePath, fileName);
    
            String strFilePath = filePath + fileName;
            //      ,    
            String strContent = strcontent + "\r
    "; try { File file = new File(strFilePath); if (!file.exists()) { Log.d("TestFile", "Create the file:" + strFilePath); file.getParentFile().mkdirs(); file.createNewFile(); } RandomAccessFile raf = new RandomAccessFile(file, "rwd"); raf.seek(file.length()); raf.write(strContent.getBytes()); raf.close(); } catch (Exception e) { Log.e("TestFile", "Error on write File:" + e); } }

    ファイルの内容を読み込む
        /**
         *          
         * @return   
         */
        public static String getFile(String filePath, String fileName) {
            try {
                //     
                File file = new File(filePath + "/", fileName);
                //   FileInputStream  
                FileInputStream fis = new FileInputStream(file);
                //            1M
                byte[] b = new byte[1024];
                int len = 0;//     1024    ,       -1.
                //   ByteArrayOutputStream  
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                //     1024   ,                
                while ((len = fis.read(b)) != -1) {
                    baos.write(b, 0, len);
                }
                //               
                byte[] data = baos.toByteArray();
                //        
                baos.close();
                //        
                fis.close();
                //        
                return new String(data);
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }

    ファイルの生成
        /**
         *     
         *   makeRootDirectory  
         *      filePath        
         */
        public static File makeFilePath(String filePath, String fileName) {
            File file = null;
            makeRootDirectory(filePath);
            try {
                file = new File(filePath + "/" + fileName);
                if (!file.exists()) {
                    file.createNewFile();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return file;
        }
    

    フォルダの生成
        //      
        public static void makeRootDirectory(String filePath) {
            File file = null;
            try {
                file = new File(filePath);
                if (!file.exists()) {
                    file.mkdir();
                }
            } catch (Exception e) {
                Log.i("error:", e+"");
            }
        }

    ファイルまたはフォルダの削除
        /**
         *             
         */
        public static void deletefile(String filePath, String fileName) {
            try {
                //                
                File file = new File(filePath + "/", fileName);
                if (file.isFile()) {
                    file.delete();
                } else {
                    deleteDirectory(filePath + "/" + fileName);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private static boolean deleteDirectory(String dir) {
            //   dir         ,         
            if (!dir.endsWith(File.separator))
                dir = dir + File.separator;
            File dirFile = new File(dir);
            //   dir        ,        ,   
            if ((!dirFile.exists()) || (!dirFile.isDirectory())) {
                System.out.println("      :" + dir + "   !");
                return false;
            }
            boolean flag = true;
            //                 
            File[] files = dirFile.listFiles();
            for (int i = 0; i < files.length; i++) {
                //      
                if (files[i].isFile()) {
                    flag = deleteFile(files[i].getAbsolutePath());
                    if (!flag)
                        break;
                }
                //      
                else if (files[i].isDirectory()) {
                    flag = deleteDirectory(files[i]
                            .getAbsolutePath());
                    if (!flag)
                        break;
                }
            }
            if (!flag) {
                System.out.println("      !");
                return false;
            }
            //       
            if (dirFile.delete()) {
                System.out.println("    " + dir + "  !");
                return true;
            } else {
                return false;
            }
        }
    
    
        public static boolean deleteFile(String fileName) {
            File file = new File(fileName);
            //               ,       ,     
            if (file.exists() && file.isFile()) {
                if (file.delete()) {
                    System.out.println("      " + fileName + "  !");
                    return true;
                } else {
                    System.out.println("      " + fileName + "  !");
                    return false;
                }
            } else {
                System.out.println("        :" + fileName + "   !");
                return false;
            }
        }

    ファイルまたはフォルダの重複名
       /**
         *          
         * oldPath   newPath            
         * */
        public static void renameFile(String oldPath, String newPath) {
    
            if(TextUtils.isEmpty(newPath)) {
                File file = new File(newPath);
                try{
                    file.createNewFile();
                }catch (IOException e) {
                    e.printStackTrace();
                }
            }
    
            File file = new File(oldPath);
            file.renameTo(new File(newPath));
        }