From 2c3b570dada31e8ec43ff35d1b06e24dabfe96a8 Mon Sep 17 00:00:00 2001 From: lijia Date: Tue, 16 Jul 2024 11:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9A=E4=BD=8D=E6=95=B0=E6=8D=AE=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E5=8C=96=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 4 +- .../arpa/hndahesudintocctmsdriver/App.java | 47 +- .../hndahesudintocctmsdriver/FileUtils.java | 1380 +++++++++++++++++ .../service/TrackService.java | 30 +- .../util/FileIOUtils.java | 240 +++ 5 files changed, 1675 insertions(+), 26 deletions(-) create mode 100644 app/src/main/java/com/arpa/hndahesudintocctmsdriver/FileUtils.java create mode 100644 app/src/main/java/com/arpa/hndahesudintocctmsdriver/util/FileIOUtils.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3d7e000..a9e9e00 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -11,6 +11,7 @@ + @@ -268,7 +269,8 @@ android:value="${GAODEKEY}" /> - + getFiledirList(String dirPath) { + if (dirPath == null || !isDir(dirPath)) return null; + List stringList = new ArrayList<>(); + File f = new File(dirPath); + File[] files = f.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.isDirectory()) { + stringList.add(file.getName()); + } + } + } + return stringList; + } + + /** + * 判断文件是否存在 + * + * @param filePath 文件路径 + * @return {@code true}: 存在
{@code false}: 不存在 + */ + public static boolean isFileExists(final String filePath) { + return isFileExists(getFileByPath(filePath)); + } + + /** + * 判断文件是否存在 + * + * @param file 文件 + * @return {@code true}: 存在
{@code false}: 不存在 + */ + public static boolean isFileExists(final File file) { + return file != null && file.exists(); + } + + /** + * 重命名文件 + * + * @param filePath 文件路径 + * @param newName 新名称 + * @return {@code true}: 重命名成功
{@code false}: 重命名失败 + */ + public static boolean rename(final String filePath, final String newName) { + return rename(getFileByPath(filePath), newName); + } + + /** + * 重命名文件 + * + * @param file 文件 + * @param newName 新名称 + * @return {@code true}: 重命名成功 + *
+ * {@code false}: 重命名失败 + */ + public static boolean rename(final File file, final String newName) { + // 文件为空返回false + if (file == null) return false; + // 文件不存在返回false + if (!file.exists()) return false; + // 新的文件名为空返回false + if (isSpace(newName)) return false; + // 如果文件名没有改变返回true + if (newName.equals(file.getName())) return true; + File newFile = new File(file.getParent() + File.separator + newName); + // 如果重命名的文件已存在返回false + return !newFile.exists() + && file.renameTo(newFile); + } + + /** + * 判断是否是目录 + * + * @param dirPath 目录路径 + * @return {@code true}: 是
{@code false}: 否 + */ + public static boolean isDir(final String dirPath) { + return isDir(getFileByPath(dirPath)); + } + + /** + * 判断是否是目录 + * + * @param file 文件 + * @return {@code true}: 是
{@code false}: 否 + */ + public static boolean isDir(final File file) { + return isFileExists(file) && file.isDirectory(); + } + + /** + * 判断是否是文件 + * + * @param filePath 文件路径 + * @return {@code true}: 是
{@code false}: 否 + */ + public static boolean isFile(final String filePath) { + return isFile(getFileByPath(filePath)); + } + + /** + * 判断是否是文件 + * + * @param file 文件 + * @return {@code true}: 是
{@code false}: 否 + */ + public static boolean isFile(final File file) { + return isFileExists(file) && file.isFile(); + } + + /** + * 判断目录是否存在,不存在则判断是否创建成功 + * + * @param dirPath 目录路径 + * @return {@code true}: 存在或创建成功
{@code false}: 不存在或创建失败 + */ + public static boolean createOrExistsDir(final String dirPath) { + return createOrExistsDir(getFileByPath(dirPath)); + } + + /** + * 判断目录是否存在,不存在则判断是否创建成功 + * + * @param file 文件 + * @return {@code true}: 存在或创建成功
{@code false}: 不存在或创建失败 + */ + public static boolean createOrExistsDir(final File file) { + // 如果存在,是目录则返回true,是文件则返回false,不存在则返回是否创建成功 + return file != null && (file.exists() ? file.isDirectory() : file.mkdirs()); + } + + /** + * 判断文件是否存在,不存在则判断是否创建成功 + * + * @param filePath 文件路径 + * @return {@code true}: 存在或创建成功
{@code false}: 不存在或创建失败 + */ + public static boolean createOrExistsFile(final String filePath) { + return createOrExistsFile(getFileByPath(filePath)); + } + + /** + * 判断文件是否存在,不存在则判断是否创建成功 + * + * @param file 文件 + * @return {@code true}: 存在或创建成功
{@code false}: 不存在或创建失败 + */ + public static boolean createOrExistsFile(final File file) { + if (file == null) return false; + // 如果存在,是文件则返回true,是目录则返回false + if (file.exists()) return file.isFile(); + if (!createOrExistsDir(file.getParentFile())) return false; + try { + return file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + + /** + * 判断文件是否存在,存在则在创建之前删除 + * + * @param file 文件 + * @return {@code true}: 创建成功
{@code false}: 创建失败 + */ + public static boolean createFileByDeleteOldFile(final File file) { + if (file == null) return false; + // 文件存在并且删除失败返回false + if (file.exists() && !file.delete()) return false; + // 创建目录失败返回false + if (!createOrExistsDir(file.getParentFile())) return false; + try { + return file.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + return false; + } + } + + /** + * 复制或移动目录 + * + * @param srcDirPath 源目录路径 + * @param destDirPath 目标目录路径 + * @param isMove 是否移动 + * @return {@code true}: 复制或移动成功
{@code false}: 复制或移动失败 + */ +// private static boolean copyOrMoveDir(final String srcDirPath, final String destDirPath, final boolean isMove) { +// return copyOrMoveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath), isMove); +// } + + /** + * 复制或移动目录 + * + * @param srcDir 源目录 + * @param destDir 目标目录 + * @param isMove 是否移动 + * @return {@code true}: 复制或移动成功
{@code false}: 复制或移动失败 + */ +// private static boolean copyOrMoveDir(final File srcDir, final File destDir, final boolean isMove) { +// if (srcDir == null || destDir == null) return false; +// // 如果目标目录在源目录中则返回false,看不懂的话好好想想递归怎么结束 +// // srcPath : F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res +// // destPath: F:\\MyGithub\\AndroidUtilCode\\utilcode\\src\\test\\res1 +// // 为防止以上这种情况出现出现误判,须分别在后面加个路径分隔符 +// String srcPath = srcDir.getPath() + File.separator; +// String destPath = destDir.getPath() + File.separator; +// if (destPath.contains(srcPath)) return false; +// // 源文件不存在或者不是目录则返回false +// if (!srcDir.exists() || !srcDir.isDirectory()) return false; +// // 目标目录不存在返回false +// if (!createOrExistsDir(destDir)) return false; +// File[] files = srcDir.listFiles(); +// for (File file : files) { +// File oneDestFile = new File(destPath + file.getName()); +// if (file.isFile()) { +// // 如果操作失败返回false +// if (!copyOrMoveFile(file, oneDestFile, isMove)) return false; +// } else if (file.isDirectory()) { +// // 如果操作失败返回false +// if (!copyOrMoveDir(file, oneDestFile, isMove)) return false; +// } +// } +// return !isMove || deleteDir(srcDir); +// } + + /** + * 复制或移动文件 + * + * @param srcFilePath 源文件路径 + * @param destFilePath 目标文件路径 + * @param isMove 是否移动 + * @return {@code true}: 复制或移动成功
{@code false}: 复制或移动失败 + */ +// private static boolean copyOrMoveFile(final String srcFilePath, final String destFilePath, final boolean isMove) { +// return copyOrMoveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath), isMove); +// } + + /** + * 复制或移动文件 + * + * @param srcFile 源文件 + * @param destFile 目标文件 + * @param isMove 是否移动 + * @return {@code true}: 复制或移动成功
{@code false}: 复制或移动失败 + */ +// private static boolean copyOrMoveFile(final File srcFile, final File destFile, final boolean isMove) { +// if (srcFile == null || destFile == null) return false; +// // 源文件不存在或者不是文件则返回false +// if (!srcFile.exists() || !srcFile.isFile()) return false; +// // 目标文件存在且是文件则返回false +// if (destFile.exists() && destFile.isFile()) return false; +// // 目标目录不存在返回false +// if (!createOrExistsDir(destFile.getParentFile())) return false; +// try { +// return FileIOUtils.writeFileFromIS(destFile, new FileInputStream(srcFile), false) +// && !(isMove && !deleteFile(srcFile)); +// } catch (FileNotFoundException e) { +// e.printStackTrace(); +// return false; +// } +// } + + /** + * 复制目录 + * + * @param srcDirPath 源目录路径 + * @param destDirPath 目标目录路径 + * @return {@code true}: 复制成功
{@code false}: 复制失败 + */ +// public static boolean copyDir(final String srcDirPath, final String destDirPath) { +// return copyDir(getFileByPath(srcDirPath), getFileByPath(destDirPath)); +// } + + /** + * 复制目录 + * + * @param srcDir 源目录 + * @param destDir 目标目录 + * @return {@code true}: 复制成功
{@code false}: 复制失败 + */ +// public static boolean copyDir(final File srcDir, final File destDir) { +// return copyOrMoveDir(srcDir, destDir, false); +// } + + /** + * 复制文件 + * + * @param srcFilePath 源文件路径 + * @param destFilePath 目标文件路径 + * @return {@code true}: 复制成功
{@code false}: 复制失败 + */ +// public static boolean copyFile(final String srcFilePath, final String destFilePath) { +// return copyFile(getFileByPath(srcFilePath), getFileByPath(destFilePath)); +// } + + /** + * 复制文件 + * + * @param srcFile 源文件 + * @param destFile 目标文件 + * @return {@code true}: 复制成功
{@code false}: 复制失败 + */ +// public static boolean copyFile(final File srcFile, final File destFile) { +// return copyOrMoveFile(srcFile, destFile, false); +// } + + /** + * 移动目录 + * + * @param srcDirPath 源目录路径 + * @param destDirPath 目标目录路径 + * @return {@code true}: 移动成功
{@code false}: 移动失败 + */ +// public static boolean moveDir(final String srcDirPath, final String destDirPath) { +// return moveDir(getFileByPath(srcDirPath), getFileByPath(destDirPath)); +// } + + /** + * 移动目录 + * + * @param srcDir 源目录 + * @param destDir 目标目录 + * @return {@code true}: 移动成功
{@code false}: 移动失败 + */ +// public static boolean moveDir(final File srcDir, final File destDir) { +// return copyOrMoveDir(srcDir, destDir, true); +// } + + /** + * 移动文件 + * + * @param srcFilePath 源文件路径 + * @param destFilePath 目标文件路径 + * @return {@code true}: 移动成功
{@code false}: 移动失败 + */ +// public static boolean moveFile(final String srcFilePath, final String destFilePath) { +// Log.e("xxx", "移动文件" + srcFilePath + "---->" + destFilePath); +// return moveFile(getFileByPath(srcFilePath), getFileByPath(destFilePath)); +// } + + /** + * 移动文件 + * + * @param srcFile 源文件 + * @param destFile 目标文件 + * @return {@code true}: 移动成功
{@code false}: 移动失败 + */ +// public static boolean moveFile(final File srcFile, final File destFile) { +// return copyOrMoveFile(srcFile, destFile, true); +// } + + /** + * 删除目录 + * + * @param dirPath 目录路径 + * @return {@code true}: 删除成功
{@code false}: 删除失败 + */ + public static boolean deleteDir(final String dirPath) { + return deleteDir(getFileByPath(dirPath)); + } + + /** + * 删除文件或目录 + * + * @param file + * @return + */ + public static boolean deleteDirOrFile(File file) { + if (file == null) return false; + if (!file.exists()) return false; + if (file.isFile()) { + return deleteFile(file); + } else { + return deleteDir(file); + } + } + + /** + * 删除文件或目录 + * + * @param path + * @return + */ + public static boolean deleteDirOrFile(String path) { + return deleteDirOrFile(getFileByPath(path)); + } + + /** + * 删除目录 + * + * @param dir 目录 + * @return {@code true}: 删除成功
{@code false}: 删除失败 + */ + public static boolean deleteDir(final File dir) { + if (dir == null) return false; + // 目录不存在返回true + if (!dir.exists()) return true; + // 不是目录返回false + if (!dir.isDirectory()) return false; + // 现在文件存在且是文件夹 + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.isFile()) { + if (!file.delete()) return false; + } else if (file.isDirectory()) { + if (!deleteDir(file)) return false; + } + } + } + return dir.delete(); + } + + /** + * 删除Luban文件集合 以“|” 分割 + * + * @param srcFilePaths + */ + public static void deleteFiles(String srcFilePaths) { + if (TextUtils.isEmpty(srcFilePaths)) { return; } + List list = Arrays.asList(srcFilePaths.split("\\|")); + for (String path : list) { + if (path.contains("luban")) { + deleteFile(path); + } + } + } + + /** + * 删除文件 + * + * @param srcFilePath 文件路径 + * @return {@code true}: 删除成功
{@code false}: 删除失败 + */ + public static boolean deleteFile(final String srcFilePath) { + return deleteFile(getFileByPath(srcFilePath)); + } + + /** + * 删除文件 + * + * @param file 文件 + * @return {@code true}: 删除成功
{@code false}: 删除失败 + */ + public static boolean deleteFile(final File file) { + return file != null && (!file.exists() || file.isFile() && file.delete()); + } + + /** + * 删除目录下的所有文件 + * + * @param dirPath 目录路径 + * @return {@code true}: 删除成功
{@code false}: 删除失败 + */ + public static boolean deleteFilesInDir(final String dirPath) { + return deleteFilesInDir(getFileByPath(dirPath)); + } + + /** + * 删除目录下的所有文件 + * + * @param dir 目录 + * @return {@code true}: 删除成功
{@code false}: 删除失败 + */ + public static boolean deleteFilesInDir(final File dir) { + if (dir == null) return false; + // 目录不存在返回true + if (!dir.exists()) return true; + // 不是目录返回false + if (!dir.isDirectory()) return false; + // 现在文件存在且是文件夹 + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.isFile()) { + if (!file.delete()) return false; + } else if (file.isDirectory()) { + if (!deleteDir(file)) return false; + } + } + } + return true; + } + + /** + * 获取目录下所有文件 + * + * @param dirPath 目录路径 + * @param isRecursive 是否递归进子目录 + * @return 文件链表 + */ + public static List listFilesInDir(final String dirPath, final boolean isRecursive) { + return listFilesInDir(getFileByPath(dirPath), isRecursive); + } + + /** + * 获取目录下所有文件 + * + * @param dir 目录 + * @param isRecursive 是否递归进子目录 + * @return 文件链表 + */ + public static List listFilesInDir(final File dir, final boolean isRecursive) { + if (!isDir(dir)) return null; + if (isRecursive) return listFilesInDir(dir); + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + Collections.addAll(list, files); + } + return list; + } + + /** + * 获取目录下所有文件包括子目录 + * + * @param dirPath 目录路径 + * @return 文件链表 + */ + public static List listFilesInDir(final String dirPath) { + return listFilesInDir(getFileByPath(dirPath)); + } + + /** + * 获取目录下所有文件包括子目录 + * + * @param dir 目录 + * @return 文件链表 + */ + public static List listFilesInDir(final File dir) { + if (!isDir(dir)) return null; + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + list.add(file); + if (file.isDirectory()) { + List fileList = listFilesInDir(file); + if (fileList != null) { + list.addAll(fileList); + } + } + } + } + return list; + } + + /** + * 获取目录下所有后缀名为suffix的文件 + *

大小写忽略

+ * + * @param dirPath 目录路径 + * @param suffix 后缀名 + * @param isRecursive 是否递归进子目录 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final String dirPath, final String suffix, final boolean isRecursive) { + return listFilesInDirWithFilter(getFileByPath(dirPath), suffix, isRecursive); + } + + /** + * 获取目录下所有后缀名为suffix的文件 + *

大小写忽略

+ * + * @param dir 目录 + * @param suffix 后缀名 + * @param isRecursive 是否递归进子目录 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final File dir, final String suffix, final boolean isRecursive) { + if (isRecursive) return listFilesInDirWithFilter(dir, suffix); + if (dir == null || !isDir(dir)) return null; + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.length() > 10) { + if (file.getName().toUpperCase().endsWith(suffix.toUpperCase())) { + list.add(file); + } + } + } + } + return list; + } + + /** + * 获取目录下所有后缀名为suffix的文件包括子目录 + *

大小写忽略

+ * + * @param dirPath 目录路径 + * @param suffix 后缀名 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final String dirPath, final String suffix) { + return listFilesInDirWithFilter(getFileByPath(dirPath), suffix); + } + + /** + * 获取目录下所有后缀名为suffix的文件包括子目录 + *

大小写忽略

+ * + * @param dir 目录 + * @param suffix 后缀名 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final File dir, final String suffix) { + if (dir == null || !isDir(dir)) return null; + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.getName().toUpperCase().endsWith(suffix.toUpperCase())) { + list.add(file); + } + if (file.isDirectory()) { + list.addAll(listFilesInDirWithFilter(file, suffix)); + } + } + } + return list; + } + + /** + * 获取目录下所有符合filter的文件 + * + * @param dirPath 目录路径 + * @param filter 过滤器 + * @param isRecursive 是否递归进子目录 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final String dirPath, final FilenameFilter filter, final boolean isRecursive) { + return listFilesInDirWithFilter(getFileByPath(dirPath), filter, isRecursive); + } + + /** + * 获取目录下所有符合filter的文件 + * + * @param dir 目录 + * @param filter 过滤器 + * @param isRecursive 是否递归进子目录 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final File dir, final FilenameFilter filter, final boolean isRecursive) { + if (isRecursive) return listFilesInDirWithFilter(dir, filter); + if (dir == null || !isDir(dir)) return null; + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (filter.accept(file.getParentFile(), file.getName())) { + list.add(file); + } + } + } + return list; + } + + /** + * 获取目录下所有符合filter的文件包括子目录 + * + * @param dirPath 目录路径 + * @param filter 过滤器 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final String dirPath, final FilenameFilter filter) { + return listFilesInDirWithFilter(getFileByPath(dirPath), filter); + } + + /** + * 获取目录下所有符合filter的文件包括子目录 + * + * @param dir 目录 + * @param filter 过滤器 + * @return 文件链表 + */ + public static List listFilesInDirWithFilter(final File dir, final FilenameFilter filter) { + if (dir == null || !isDir(dir)) return null; + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (filter.accept(file.getParentFile(), file.getName())) { + list.add(file); + } + if (file.isDirectory()) { + list.addAll(listFilesInDirWithFilter(file, filter)); + } + } + } + return list; + } + + /** + * 获取目录下指定文件名的文件包括子目录 + *

大小写忽略

+ * + * @param dirPath 目录路径 + * @param fileName 文件名 + * @return 文件链表 + */ + public static List searchFileInDir(final String dirPath, final String fileName) { + return searchFileInDir(getFileByPath(dirPath), fileName); + } + + /** + * 获取目录下指定文件名的文件包括子目录 + *

大小写忽略

+ * + * @param dir 目录 + * @param fileName 文件名 + * @return 文件链表 + */ + public static List searchFileInDir(final File dir, final String fileName) { + if (dir == null || !isDir(dir)) return null; + List list = new ArrayList<>(); + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.getName().toUpperCase().equals(fileName.toUpperCase())) { + list.add(file); + } + if (file.isDirectory()) { + list.addAll(searchFileInDir(file, fileName)); + } + } + } + return list; + } + + /** + * 获取文件最后修改的毫秒时间戳 + * + * @param filePath 文件路径 + * @return 文件最后修改的毫秒时间戳 + */ + public static long getFileLastModified(final String filePath) { + return getFileLastModified(getFileByPath(filePath)); + } + + /** + * 获取文件最后修改的毫秒时间戳 + * + * @param file 文件 + * @return 文件最后修改的毫秒时间戳 + */ + public static long getFileLastModified(final File file) { + if (file == null) return -1; + return file.lastModified(); + } + + /** + * 简单获取文件编码格式 + * + * @param filePath 文件路径 + * @return 文件编码 + */ + public static String getFileCharsetSimple(final String filePath) { + return getFileCharsetSimple(getFileByPath(filePath)); + } + + /** + * 简单获取文件编码格式 + * + * @param file 文件 + * @return 文件编码 + */ + public static String getFileCharsetSimple(final File file) { + int p = 0; + InputStream is = null; + try { + is = new BufferedInputStream(new FileInputStream(file)); + p = (is.read() << 8) + is.read(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + switch (p) { + case 0xefbb: + return "UTF-8"; + case 0xfffe: + return "Unicode"; + case 0xfeff: + return "UTF-16BE"; + default: + return "GBK"; + } + } + + /** + * 获取文件行数 + * + * @param filePath 文件路径 + * @return 文件行数 + */ + public static int getFileLines(final String filePath) { + return getFileLines(getFileByPath(filePath)); + } + + /** + * 获取文件行数 + *

比readLine要快很多

+ * + * @param file 文件 + * @return 文件行数 + */ + public static int getFileLines(final File file) { + int count = 1; + InputStream is = null; + try { + is = new BufferedInputStream(new FileInputStream(file)); + byte[] buffer = new byte[1024]; + int readChars; + if (LINE_SEP.endsWith("\n")) { + while ((readChars = is.read(buffer, 0, 1024)) != -1) { + for (int i = 0; i < readChars; ++i) { + if (buffer[i] == '\n') ++count; + } + } + } else { + while ((readChars = is.read(buffer, 0, 1024)) != -1) { + for (int i = 0; i < readChars; ++i) { + if (buffer[i] == '\r') ++count; + } + } + } + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + is.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return count; + } + + /** + * 获取目录大小 + * + * @param dirPath 目录路径 + * @return 文件大小 + */ + public static String getDirSize(final String dirPath) { + return getDirSize(getFileByPath(dirPath)); + } + + /** + * 获取目录大小 + * + * @param dir 目录 + * @return 文件大小 + */ + public static String getDirSize(final File dir) { + long len = getDirLength(dir); + return len == -1 ? "" : byte2FitMemorySize(len); + } + + /** + * 获取文件大小 + * + * @param filePath 文件路径 + * @return 文件大小 + */ + public static String getFileSize(final String filePath) { + return getFileSize(getFileByPath(filePath)); + } + + /** + * 获取文件大小 + * + * @param file 文件 + * @return 文件大小 + */ + public static String getFileSize(final File file) { + long len = getFileLength(file); + return len == -1 ? "" : byte2FitMemorySize(len); + } + + /** + * 获取目录长度 + * + * @param dirPath 目录路径 + * @return 目录长度 + */ + public static long getDirLength(final String dirPath) { + return getDirLength(getFileByPath(dirPath)); + } + + /** + * 获取目录长度 + * + * @param dir 目录 + * @return 目录长度 + */ + public static long getDirLength(final File dir) { + if (!isDir(dir)) return -1; + long len = 0; + File[] files = dir.listFiles(); + if (files != null && files.length != 0) { + for (File file : files) { + if (file.isDirectory()) { + len += getDirLength(file); + } else { + len += file.length(); + } + } + } + return len; + } + + /** + * 获取文件长度 + * + * @param filePath 文件路径 + * @return 文件长度 + */ + public static long getFileLength(final String filePath) { + return getFileLength(getFileByPath(filePath)); + } + + /** + * 获取文件长度 + * + * @param file 文件 + * @return 文件长度 + */ + public static long getFileLength(final File file) { + if (!isFile(file)) return -1; + return file.length(); + } + + /** + * 获取文件的MD5校验码 + * + * @param filePath 文件路径 + * @return 文件的MD5校验码 + */ + public static String getFileMD5ToString(final String filePath) { + File file = isSpace(filePath) ? null : new File(filePath); + return getFileMD5ToString(file); + } + + /** + * 获取文件的MD5校验码 + * + * @param filePath 文件路径 + * @return 文件的MD5校验码 + */ + public static byte[] getFileMD5(final String filePath) { + File file = isSpace(filePath) ? null : new File(filePath); + return getFileMD5(file); + } + + /** + * 获取文件的MD5校验码 + * + * @param file 文件 + * @return 文件的MD5校验码 + */ + public static String getFileMD5ToString(final File file) { + return bytes2HexString(getFileMD5(file)); + } + + /** + * 获取文件的MD5校验码 + * + * @param file 文件 + * @return 文件的MD5校验码 + */ + public static byte[] getFileMD5(final File file) { + if (file == null) return null; + DigestInputStream dis = null; + try { + FileInputStream fis = new FileInputStream(file); + MessageDigest md = MessageDigest.getInstance("MD5"); + dis = new DigestInputStream(fis, md); + byte[] buffer = new byte[1024 * 256]; + while (true) { + if (!(dis.read(buffer) > 0)) break; + } + md = dis.getMessageDigest(); + return md.digest(); + } catch (NoSuchAlgorithmException | IOException e) { + e.printStackTrace(); + } finally { + try { + dis.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + return null; + } + + /** + * 获取全路径中的最长目录 + * + * @param file 文件 + * @return filePath最长目录 + */ + public static String getDirName(final File file) { + if (file == null) return null; + return getDirName(file.getPath()); + } + + /** + * 获取全路径中的最长目录 + * + * @param filePath 文件路径 + * @return filePath最长目录 + */ + public static String getDirName(final String filePath) { + if (isSpace(filePath)) return filePath; + int lastSep = filePath.lastIndexOf(File.separator); + return lastSep == -1 ? "" : filePath.substring(0, lastSep + 1); + } + + /** + * 获取全路径中的文件名 + * + * @param file 文件 + * @return 文件名 + */ + public static String getFileName(final File file) { + if (file == null) return null; + return getFileName(file.getPath()); + } + + /** + * 获取全路径中的文件名 + * + * @param filePath 文件路径 + * @return 文件名 + */ + public static String getFileName(final String filePath) { + if (isSpace(filePath)) return filePath; + int lastSep = filePath.lastIndexOf(File.separator); + return lastSep == -1 ? filePath : filePath.substring(lastSep + 1); + } + + /** + * 获取全路径中的不带拓展名的文件名 + * + * @param file 文件 + * @return 不带拓展名的文件名 + */ + public static String getFileNameNoExtension(final File file) { + if (file == null) return null; + return getFileNameNoExtension(file.getPath()); + } + + /** + * 获取全路径中的不带拓展名的文件名 + * + * @param filePath 文件路径 + * @return 不带拓展名的文件名 + */ + public static String getFileNameNoExtension(final String filePath) { + if (isSpace(filePath)) return filePath; + int lastPoi = filePath.lastIndexOf('.'); + int lastSep = filePath.lastIndexOf(File.separator); + if (lastSep == -1) { + return (lastPoi == -1 ? filePath : filePath.substring(0, lastPoi)); + } + if (lastPoi == -1 || lastSep > lastPoi) { + return filePath.substring(lastSep + 1); + } + return filePath.substring(lastSep + 1, lastPoi); + } + + /** + * 获取全路径中的文件拓展名 + * + * @param file 文件 + * @return 文件拓展名 + */ + public static String getFileExtension(final File file) { + if (file == null) return null; + return getFileExtension(file.getPath()); + } + + /** + * 获取全路径中的文件拓展名 + * + * @param filePath 文件路径 + * @return 文件拓展名 + */ + public static String getFileExtension(final String filePath) { + if (isSpace(filePath)) return filePath; + int lastPoi = filePath.lastIndexOf('.'); + int lastSep = filePath.lastIndexOf(File.separator); + if (lastPoi == -1 || lastSep >= lastPoi) return ""; + return filePath.substring(lastPoi + 1); + } + + /** + * 根据文件类型去删除其在系统中对应的Media数据库 + * + * @param file + * @return -1代表不是媒体文件,0表示在数据库中查找不到,1找到数据库中对应的数据,并且删除 + */ + public static int deleteMedia(File file) { + String name = file.getName(); + String path = file.getAbsolutePath(); + if (name.contains(".jpg") || name.contains(".mp4")) { + Uri MEDIA_URI = null; + if (name.contains(".jpg")) { + if (path.contains("mnt/sdcard/")) { + MEDIA_URI = MediaStore.Images.Media.INTERNAL_CONTENT_URI; + path = path.replace("/mnt/sdcard/", "/storage/sdcard0/"); + } else if (file.getAbsolutePath().contains("mnt/sdcard2/")) { + MEDIA_URI = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + path = path.replace("/mnt/sdcard2/", "/storage/sdcard1/"); + } + } else { + if (path.contains("mnt/sdcard/")) { + MEDIA_URI = MediaStore.Video.Media.INTERNAL_CONTENT_URI; + path = path.replace("/mnt/sdcard1/", "/storage/sdcard0/"); + } else if (file.getAbsolutePath().contains("mnt/sdcard2/")) { + MEDIA_URI = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; + path = path.replace("/mnt/sdcard2/", "/storage/sdcard1/"); + } + } + int resultCode = 0; + // resultCode = MyApp.getInstance().getContentResolver().delete(MEDIA_URI, MediaStore.Images.Media.DATA+"="+"'"+path+"'" , null); + return resultCode; + } else { + return -1; + } + } + /// + // copy from ConvertUtils + /// + + private static final char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; + + /** + * byteArr转hexString + *

例如:

+ * bytes2HexString(new byte[] { 0, (byte) 0xa8 }) returns 00A8 + * + * @param bytes 字节数组 + * @return 16进制大写字符串 + */ + private static String bytes2HexString(final byte[] bytes) { + if (bytes == null) return null; + int len = bytes.length; + if (len <= 0) return null; + char[] ret = new char[len << 1]; + for (int i = 0, j = 0; i < len; i++) { + ret[j++] = hexDigits[bytes[i] >>> 4 & 0x0f]; + ret[j++] = hexDigits[bytes[i] & 0x0f]; + } + return new String(ret); + } + + /** + * 字节数转合适内存大小 + *

保留3位小数

+ * + * @param byteNum 字节数 + * @return 合适内存大小 + */ + @SuppressLint("DefaultLocale") + private static String byte2FitMemorySize(final long byteNum) { + if (byteNum < 0) { + return "shouldn't be less than zero!"; + } else if (byteNum < 1024) { + return String.format("%.3fB", (double) byteNum + 0.0005); + } else if (byteNum < 1048576) { + return String.format("%.3fKB", (double) byteNum / 1024 + 0.0005); + } else if (byteNum < 1073741824) { + return String.format("%.3fMB", (double) byteNum / 1048576 + 0.0005); + } else { + return String.format("%.3fGB", (double) byteNum / 1073741824 + 0.0005); + } + } + + private static boolean isSpace(final String s) { + if (s == null) return true; + for (int i = 0, len = s.length(); i < len; ++i) { + if (!Character.isWhitespace(s.charAt(i))) { + return false; + } + } + return true; + } + + // --------------- + + /** + * 在指定的位置创建指定的文件 + * + * @param filePath 完整的文件路径 + * @param mkdir 是否创建相关的文件夹 + * @throws IOException + */ + public static void mkFile(String filePath, boolean mkdir) throws IOException { + File file = new File(filePath); + /** + * mkdirs()创建多层目录,mkdir()创建单层目录 + * writeObject时才创建磁盘文件。 + * 若不创建文件,readObject出错。 + */ + file.getParentFile().mkdirs(); + file.createNewFile(); + file = null; + } + + /** + * 在指定的位置创建文件夹 + * + * @param dirPath 文件夹路径 + * @return 若创建成功,则返回True;反之,则返回False + */ + public static boolean mkDir(String dirPath) { + return new File(dirPath).mkdirs(); + } + + /** + * 删除指定的文件 + * + * @param filePath 文件路径 + * @return 若删除成功,则返回True;反之,则返回False + */ + public static boolean delFile(String filePath) { + return new File(filePath).delete(); + } + + /** + * 删除指定的文件夹 + * + * @param dirPath 文件夹路径 + * @param delFile 文件夹中是否包含文件 + * @return 若删除成功,则返回True;反之,则返回False + */ + public static boolean delDir(String dirPath, boolean delFile) { + if (delFile) { + File file = new File(dirPath); + if (file.isFile()) { + return file.delete(); + } else if (file.isDirectory()) { + if (file.listFiles().length == 0) { + return file.delete(); + } else { + int zFiles = file.listFiles().length; + File[] delfile = file.listFiles(); + for (int i = 0; i < zFiles; i++) { + if (delfile[i].isDirectory()) { + delDir(delfile[i].getAbsolutePath(), true); + } + delfile[i].delete(); + } + return file.delete(); + } + } else { + return false; + } + } else { + return new File(dirPath).delete(); + } + } + + /** + * 复制文件/文件夹 若要进行文件夹复制,请勿将目标文件夹置于源文件夹中 + * + * @param source 源文件(夹) + * @param target 目标文件(夹) + * @param isFolder 若进行文件夹复制,则为True;反之为False + * @throws IOException + */ + public static void copy(String source, String target, boolean isFolder) throws IOException { + if (isFolder) { + new File(target).mkdirs(); + File a = new File(source); + String[] file = a.list(); + File temp = null; + for (int i = 0; i < file.length; i++) { + if (source.endsWith(File.separator)) { + temp = new File(source + file[i]); + } else { + temp = new File(source + File.separator + file[i]); + } + if (temp.isFile()) { + FileInputStream input = new FileInputStream(temp); + FileOutputStream output = new FileOutputStream(target + File.separator + temp.getName().toString()); + byte[] b = new byte[1024]; + int len; + while ((len = input.read(b)) != -1) { + output.write(b, 0, len); + } + output.flush(); + output.close(); + input.close(); + } + if (temp.isDirectory()) { + copy(source + File.separator + file[i], target + File.separator + file[i], true); + } + } + } else { + int byteread = 0; + File oldfile = new File(source); + if (oldfile.exists()) { + InputStream inputStream = new FileInputStream(source); + File file = new File(target); + file.getParentFile().mkdirs(); + file.createNewFile(); + FileOutputStream outputStream = new FileOutputStream(file); + byte[] buffer = new byte[1024]; + while ((byteread = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, byteread); + } + inputStream.close(); + outputStream.close(); + } + } + } + +} diff --git a/app/src/main/java/com/arpa/hndahesudintocctmsdriver/service/TrackService.java b/app/src/main/java/com/arpa/hndahesudintocctmsdriver/service/TrackService.java index 6d71619..69af5d8 100644 --- a/app/src/main/java/com/arpa/hndahesudintocctmsdriver/service/TrackService.java +++ b/app/src/main/java/com/arpa/hndahesudintocctmsdriver/service/TrackService.java @@ -3,6 +3,7 @@ package com.arpa.hndahesudintocctmsdriver.service; import android.app.Service; import android.content.Context; import android.content.Intent; +import android.os.Environment; import android.os.Handler; import android.os.IBinder; import android.util.Log; @@ -10,6 +11,7 @@ import android.util.Log; import androidx.annotation.Nullable; import com.amap.api.location.AMapLocation; +import com.arpa.hndahesudintocctmsdriver.util.FileIOUtils; import com.google.gson.Gson; import com.arpa.hndahesudintocctmsdriver.bean.BaseBean; import com.arpa.hndahesudintocctmsdriver.request.HuoYuanRequset; @@ -19,8 +21,13 @@ import com.arpa.hndahesudintocctmsdriver.util.sp.SPUtil; import com.arpa.hndahesudintocctmsdriver.util.cache.CacheGroup; import com.arpa.hndahesudintocctmsdriver.util.http.RequsetCodeConstants; +import java.io.File; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Date; import java.util.Timer; import java.util.TimerTask; +import java.util.logging.Filter; /** * @author hlh @@ -33,7 +40,7 @@ public class TrackService extends Service { private Context con; private TrackInputBean tib=new TrackInputBean(); private LocationGDUtil l; - private int timeSum=1000*10; + private int timeSum=1000*60; private String snn=""; private Gson gson=new Gson(); private HuoYuanRequset hyr; @@ -54,10 +61,29 @@ public class TrackService extends Service { case LocationGDUtil.RES: if(CacheGroup.cacheList.get("getLocation")!=null){ AMapLocation location = gson.fromJson(CacheGroup.cacheList.get("getLocation"), AMapLocation.class); - genzong(location); + String time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); + String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); + // 当前时间:${time2},经纬度:${it.latitude}:${it.longitude} + FileIOUtils.writeFileFromString( + Environment.getExternalStorageDirectory() + .toString() + "/crashinfo/" + "crash-"+time+".txt", + "当前时间:"+time2+",经纬度:"+location.getLatitude()+":"+location.getLongitude(), + true + ); +// genzong(location); CacheGroup.cacheList.remove("getLocation"); } break; + case 16: + String time2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); + String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date()); + FileIOUtils.writeFileFromString( + Environment.getExternalStorageDirectory() + .toString() + "/crashinfo/" + "crash-"+time+".txt", + "当前时间:"+time2+",定位失败", + true + ); + break; } return false; }); diff --git a/app/src/main/java/com/arpa/hndahesudintocctmsdriver/util/FileIOUtils.java b/app/src/main/java/com/arpa/hndahesudintocctmsdriver/util/FileIOUtils.java new file mode 100644 index 0000000..3ce4b9e --- /dev/null +++ b/app/src/main/java/com/arpa/hndahesudintocctmsdriver/util/FileIOUtils.java @@ -0,0 +1,240 @@ +package com.arpa.hndahesudintocctmsdriver.util; + +import com.arpa.hndahesudintocctmsdriver.FileUtils; + +import java.io.BufferedOutputStream; +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.io.OutputStream; + +/** + * @ClassName FileIOUtils + * @Author john + * @Date 2024/3/29 09:50 + * @Description TODO + */ +public class FileIOUtils { + + private FileIOUtils() { + throw new UnsupportedOperationException("u can't instantiate me..."); + } + + private static final String LINE_SEP = System.getProperty("line.separator"); + + /** + * 将输入流写入文件 + * + * @param filePath 路径 + * @param is 输入流 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromIS(String filePath, final InputStream is) { + return writeFileFromIS(FileUtils.getFileByPath(filePath), is, false); + } + + /** + * 将输入流写入文件 + * + * @param filePath 路径 + * @param is 输入流 + * @param append 是否追加在文件末 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromIS(String filePath, final InputStream is, boolean append) { + return writeFileFromIS(FileUtils.getFileByPath(filePath), is, append); + } + + /** + * 将输入流写入文件 + * + * @param file 文件 + * @param is 输入流 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromIS(File file, final InputStream is) { + return writeFileFromIS(file, is, false); + } + + /** + * 将输入流写入文件 + * + * @param file 文件 + * @param is 输入流 + * @param append 是否追加在文件末 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromIS(File file, final InputStream is, boolean append) { + if (!FileUtils.createOrExistsFile(file) || is == null) return false; + OutputStream os = null; + try { + os = new BufferedOutputStream(new FileOutputStream(file, append)); + byte data[] = new byte[1024]; + int len; + while ((len = is.read(data, 0, 1024)) != -1) { + os.write(data, 0, len); + } + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } finally { + try { + is.close(); + os.close(); + } catch (IOException e) { + e.printStackTrace(); + } + + } + } + + + /** + * 将字符串写入文件 + * + * @param filePath 文件路径 + * @param content 写入内容 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromString(String filePath, String content) { + return writeFileFromString(FileUtils.getFileByPath(filePath), content, false); + } + + /** + * 将字符串写入文件 + * + * @param filePath 文件路径 + * @param content 写入内容 + * @param append 是否追加在文件末 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromString(String filePath, String content, boolean append) { + return writeFileFromString(FileUtils.getFileByPath(filePath), content, append); + } + + /** + * 将字符串写入文件 + * + * @param file 文件 + * @param content 写入内容 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromString(File file, String content) { + return writeFileFromString(file, content, false); + } + + /** + * 将字符串写入文件 + * + * @param file 文件 + * @param content 写入内容 + * @param append 是否追加在文件末 + * @return {@code true}: 写入成功
{@code false}: 写入失败 + */ + public static boolean writeFileFromString(File file, String content, boolean append) { + if (file == null || content == null) return false; + if (!FileUtils.createOrExistsFile(file)) return false; + BufferedWriter bw = null; + try { + bw = new BufferedWriter(new FileWriter(file, append)); + bw.write(content); + bw.newLine(); + return true; + } catch (IOException e) { + e.printStackTrace(); + return false; + } finally { + try { + bw.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + /** + * 读取文件到字符串中 + * + * @param filePath 文件路径 + * @return 字符串 + */ + public static String readFile2String(String filePath) { + return readFile2String(FileUtils.getFileByPath(filePath), null); + } + + /** + * 读取文件到字符串中 + * + * @param filePath 文件路径 + * @param charsetName 编码格式 + * @return 字符串 + */ + public static String readFile2String(String filePath, String charsetName) { + return readFile2String(FileUtils.getFileByPath(filePath), charsetName); + } + + /** + * 读取文件到字符串中 + * + * @param file 文件 + * @return 字符串 + */ + public static String readFile2String(File file) { + return readFile2String(file, null); + } + + /** + * 读取文件到字符串中 + * + * @param file 文件 + * @param charsetName 编码格式 + * @return 字符串 + */ + public static String readFile2String(File file, String charsetName) { + if (!FileUtils.isFileExists(file)) return null; + BufferedReader reader = null; + try { + StringBuilder sb = new StringBuilder(); + if (isSpace(charsetName)) { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + } else { + reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charsetName)); + } + String line; + while ((line = reader.readLine()) != null) { + sb.append(line).append(LINE_SEP); + } + // delete the last line separator + return sb.delete(sb.length() - LINE_SEP.length(), sb.length()).toString(); + } catch (IOException e) { + e.printStackTrace(); + return null; + } finally { + try { + reader.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + + private static boolean isSpace(String s) { + if (s == null) return true; + for (int i = 0, len = s.length(); i < len; ++i) { + if (!Character.isWhitespace(s.charAt(i))) { + return false; + } + } + return true; + } + +}