com.blankj.utilcode.util.PathUtils Java Examples

The following examples show how to use com.blankj.utilcode.util.PathUtils. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FileOperator.java    From Pixiv-Shaft with MIT License 6 votes vote down vote up
@Override
public <T> T getModel(String key, Class<T> pClass) {
    try {
        File file = new File(PathUtils.getInternalAppCachePath(), key);
        if (!file.exists()) {
            return null;
        }
        FileInputStream is = new FileInputStream(file);
        ObjectInputStream ois = new ObjectInputStream(is);
        T result = (T) ois.readObject();
        ois.close();
        return result;
    } catch (ClassNotFoundException | IOException pE) {
        pE.printStackTrace();
    }
    return null;
}
 
Example #2
Source File: FileOperator.java    From Pixiv-Shaft with MIT License 5 votes vote down vote up
@Override
public <T> void saveModel(String ket, T pT) {
    try {
        File file = new File(PathUtils.getInternalAppCachePath(), ket);
        Log.d("file name ", file.getPath());
        FileOutputStream fileOutputStream = new FileOutputStream(file);
        ObjectOutputStream oos = new ObjectOutputStream(fileOutputStream);
        oos.writeObject(pT);
        oos.flush();
        oos.close();
    } catch (IOException pE) {
        pE.printStackTrace();
    }
}
 
Example #3
Source File: FileOperator.java    From Pixiv-Shaft with MIT License 5 votes vote down vote up
@Override
public void clearAll() {
    if (FileUtils.delete(PathUtils.getInternalAppCachePath())) {
        ToastUtils.showShort("清除成功!");
    } else {
        ToastUtils.showShort("清除失败!");
    }
}
 
Example #4
Source File: FileOperator.java    From Pixiv-Shaft with MIT License 5 votes vote down vote up
@Override
public void clear(String key) {
    File file = new File(PathUtils.getInternalAppCachePath(), key);
    if (!file.exists()) {
        ToastUtils.showShort("文件不存在!");
    } else {
        if (file.delete()) {
            ToastUtils.showShort("清除成功!");
        } else {
            ToastUtils.showShort("清除失败!");
        }
    }
}
 
Example #5
Source File: FileItem.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
private static List<FileItem> getFileItems() {
    List<FileItem> fileItems = new ArrayList<>();
    String internalAppDataPath = PathUtils.getInternalAppDataPath();
    if (!StringUtils.isEmpty(internalAppDataPath)) {
        File internalDataFile = new File(internalAppDataPath);
        if (internalDataFile.exists()) {
            fileItems.add(new FileItem(internalDataFile, "internal"));
        }
    }
    String externalAppDataPath = PathUtils.getExternalAppDataPath();
    if (!StringUtils.isEmpty(externalAppDataPath)) {
        File externalDataFile = new File(externalAppDataPath);
        if (externalDataFile.exists()) {
            fileItems.add(new FileItem(externalDataFile, "external"));
        }
    }
    List<String> mountedSDCardPath = SDCardUtils.getMountedSDCardPath();
    if (!mountedSDCardPath.isEmpty()) {
        for (int i = 0; i < mountedSDCardPath.size(); i++) {
            String path = mountedSDCardPath.get(i);
            File sdPath = new File(path);
            if (sdPath.exists()) {
                fileItems.add(new FileItem(sdPath, "sdcard" + i + "_" + sdPath.getName(), true));
            }
        }
    }
    return fileItems;
}
 
Example #6
Source File: ClearCacheDebug.java    From AndroidUtilCode with Apache License 2.0 5 votes vote down vote up
private ThreadUtils.SimpleTask<Long> createClearCacheTask() {
    return new ThreadUtils.SimpleTask<Long>() {
        @Override
        public Long doInBackground() throws Throwable {
            try {
                long len = 0;
                File appDataDir = new File(PathUtils.getInternalAppDataPath());
                if (appDataDir.exists()) {
                    String[] names = appDataDir.list();
                    for (String name : names) {
                        if (!name.equals("lib")) {
                            File file = new File(appDataDir, name);
                            len += FileUtils.getLength(file);
                            FileUtils.delete(file);
                            LogUtils.i("「" + file + "」 was deleted.");
                        }
                    }
                }
                String externalAppCachePath = PathUtils.getExternalAppCachePath();
                len += FileUtils.getLength(externalAppCachePath);
                FileUtils.delete(externalAppCachePath);
                LogUtils.i("「" + externalAppCachePath + "」 was deleted.");
                return len;
            } catch (Exception e) {
                ToastUtils.showLong(e.toString());
                return -1L;
            }
        }

        @Override
        public void onSuccess(Long result) {
            if (result != -1) {
                ToastUtils.showLong("Clear Cache: " + ConvertUtils.byte2FitMemorySize(result));
            }
        }
    };
}
 
Example #7
Source File: AppStartInfoFragment.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
/**
 * 将启动信息保存到文件并分享
 */
private void export2File(final String info) {
    if (TextUtils.isEmpty(info)) {
        ToastUtils.showShort("启动信息为空");
        return;
    }
    ToastUtils.showShort("启动信息保存中,请稍后...");
    final String logPath = PathUtils.getInternalAppFilesPath() + File.separator + AppUtils.getAppName() + "_" + "app_launch.log";
    final File logFile = new File(logPath);

    ThreadUtils.executeByCpu(new ThreadUtils.Task<Boolean>() {
        @Override
        public Boolean doInBackground() throws Throwable {
            try {

                FileIOUtils.writeFileFromString(logFile, info, false);

                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        public void onSuccess(Boolean result) {
            if (result) {
                ToastUtils.showShort("启动信息文件保存在:" + logPath);
                //分享
                FileUtil.systemShare(DoraemonKit.APPLICATION, logFile);
            }
        }

        @Override
        public void onCancel() {
            if (logFile.exists()) {
                FileUtils.delete(logFile);
            }
            ToastUtils.showShort("启动信息保存失败");
        }

        @Override
        public void onFail(Throwable t) {
            if (logFile.exists()) {
                FileUtils.delete(logFile);
            }
            ToastUtils.showShort("启动信息保存失败");
        }
    });

}
 
Example #8
Source File: LogInfoDokitView.java    From DoraemonKit with Apache License 2.0 4 votes vote down vote up
/**
 * 将日志信息保存到文件
 *
 * @param operateType 100 保存到本地  101 保存到本地并分享
 */
private void export2File(final int operateType) {
    ToastUtils.showShort("日志保存中,请稍后...");
    final String logPath = PathUtils.getInternalAppFilesPath() + File.separator + AppUtils.getAppName() + "_" + TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss")) + ".log";
    final File logFile = new File(logPath);

    ThreadUtils.executeByCpu(new ThreadUtils.Task<Boolean>() {
        @Override
        public Boolean doInBackground() throws Throwable {
            try {
                List<LogLine> logLines = new ArrayList<>(mLogItemAdapter.getTrueValues());
                for (LogLine logLine : logLines) {
                    String strLog = logLine.getProcessId() + "   " + "   " + logLine.getTimestamp() + "   " + logLine.getTag() + "   " + logLine.getLogLevelText() + "   " + logLine.getLogOutput() + "\n";
                    FileIOUtils.writeFileFromString(logFile, strLog, true);
                }
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }

        @Override
        public void onSuccess(Boolean result) {
            if (result) {
                ToastUtils.showShort("文件保存在:" + logPath);
                //分享
                if (operateType == 101) {
                    FileUtil.systemShare(DoraemonKit.APPLICATION, logFile);
                }
            }
        }

        @Override
        public void onCancel() {
            if (logFile.exists()) {
                FileUtils.delete(logFile);
            }
            ToastUtils.showShort("日志保存失败");
        }

        @Override
        public void onFail(Throwable t) {
            if (logFile.exists()) {
                FileUtils.delete(logFile);
            }
            ToastUtils.showShort("日志保存失败");
        }
    });

}