package com.example.datalibrary.utils; import android.annotation.SuppressLint; import android.graphics.Bitmap; import android.os.Environment; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.RandomAccessFile; /** * author : shangrong * date : 2019/5/23 2:05 PM * description :文件工具类 */ public class FileUtils { /** * 读取txt文件的内容 * * @param filePath 想要读取的文件对象 * @return 返回文件内容 */ @SuppressLint("NewApi") public static String txt2String(String filePath) { if (filePath == null){ return ""; } File file = new File(filePath); if (!file.exists()) { return ""; } StringBuilder result = new StringBuilder(); try { // 构造一个BufferedReader类来读取文件 BufferedReader br = new BufferedReader(new FileReader(file)); String s = null; // 使用readLine方法,一次读一行 while ((s = br.readLine()) != null) { result.append(System.lineSeparator() + s); } br.close(); } catch (Exception e) { e.printStackTrace(); } return result.toString(); } /** * 写入TXT文件 */ public static boolean writeTxtFile(String content, String filePath) { File file = new File(filePath); if (!file.exists()) { return false; } RandomAccessFile mm = null; boolean flag = false; FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes("utf-8")); fileOutputStream.close(); flag = true; } catch (Exception e) { e.printStackTrace(); } return flag; } public static String spotString(String str){ int n = 0; String newStr = ""; for (int i = 0 , k = str.length(); i < k ; i++){ char item = str.charAt(i); if (isChinese(item)){ n += 2; }else { n += 1; } if (n > 10){ newStr += "..."; return newStr; } newStr += item; } return newStr; } public static boolean isChinese(char c) { return c >= 0x4E00 && c <= 0x9FA5; // 根据字节码判断 } /** * Checks if is sd card available.检查SD卡是否可用 */ public static boolean isSdCardAvailable() { return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED); } /** * Gets the SD root file.获取SD卡根目录 */ public static File getSDRootFile() { if (isSdCardAvailable()) { return Environment.getExternalStorageDirectory(); } else { return null; } } /** * 获取导入图片文件的目录信息 */ public static File getBatchImportDirectory() { // 获取根目录 File sdRootFile = getSDRootFile(); File file = null; if (sdRootFile != null && sdRootFile.exists()) { file = new File(sdRootFile, "Face-Import"); if (!file.exists()) { file.mkdirs(); } } return file; } /** * 获取导入图片成功的目录信息 */ public static File getBatchImportSuccessDirectory() { File sdRootFile = getSDRootFile(); File file = null; if (sdRootFile != null && sdRootFile.exists()) { file = new File(sdRootFile, "Success-Import"); if (!file.exists()) { file.mkdirs(); } } return file; } /** * 判断文件是否存在 */ public static File isFileExist(String fileDirectory, String fileName) { File file = new File(fileDirectory + "/" + fileName); try { if (!file.exists()) { return null; } } catch (Exception e) { return null; } return file; } /** * 删除文件 */ public static void deleteFile(String filePath) { try { // 找到文件所在的路径并删除该文件 File file = new File(filePath); file.delete(); } catch (Exception e) { e.printStackTrace(); } } /* * 获取不带扩展名的文件名 * */ public static String getFileNameNoEx(String filename) { if ((filename != null) && (filename.length() > 0)) { int dot = filename.lastIndexOf('.'); if ((dot > -1) && (dot < (filename.length()))) { return filename.substring(0, dot); } } return filename; } /** * 保存图片 */ public static boolean saveBitmap(File file, Bitmap bitmap) { FileOutputStream out = null; try { out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); return true; } catch (Exception e) { e.printStackTrace(); } finally { try { if (out != null) { out.close(); } } catch (Exception e) { e.printStackTrace(); } } return false; } public static boolean copyFile(String oldPath, String newPath) { InputStream inStream = null; FileOutputStream fs = null; boolean result = false; try { int bytesum = 0; int byteread = 0; File oldfile = new File(oldPath); // 判断目录是否存在 File newfile = new File(newPath); File newFileDir = new File(newfile.getPath().replace(newfile.getName(), "")); if (!newFileDir.exists()) { newFileDir.mkdirs(); } if (oldfile.exists()) { // 文件存在时 inStream = new FileInputStream(oldPath); // 读入原文件 fs = new FileOutputStream(newPath); byte[] buffer = new byte[1444]; int length; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; // 字节数 文件大小 System.out.println(bytesum); fs.write(buffer, 0, byteread); } result = true; } else { result = false; } } catch (Exception e) { System.out.println("复制单个文件操作出错"); e.printStackTrace(); } finally { if (inStream != null) { try { inStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (fs != null) { try { fs.close(); } catch (IOException e) { e.printStackTrace(); } } } return result; } public static void saveFile(File bitmap, String dirName, String fileName) { String path = Environment.getExternalStorageDirectory().getAbsolutePath(); File dir = new File(path + "/" + dirName); dir.mkdirs(); String name = bitmap.getName(); File jpgFile = new File(dir + File.separator + fileName + name.substring(name.lastIndexOf("."))); try { if (!jpgFile.exists()) { jpgFile.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(jpgFile); fileOutputStream.write(toByteArray(bitmap)); fileOutputStream.flush(); fileOutputStream.close(); // Log.e("Save-Image-tag", "rgb图片保存成功"); } catch (IOException e) { e.printStackTrace(); } } /*读取文件的字节数组*/ public static byte[] toByteArray(File file) throws IOException { File f = file; if (!f.exists()) { throw new FileNotFoundException("file not exists"); } ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length()); BufferedInputStream in = null; try { in = new BufferedInputStream(new FileInputStream(f)); int bufSize = 1024; byte[] buffer = new byte[bufSize]; int len = 0; while (-1 != (len = in.read(buffer, 0, bufSize))) { bos.write(buffer, 0, len); } return bos.toByteArray(); } catch (IOException e) { e.printStackTrace(); throw e; } finally { try { if (in != null) { in.close(); } } catch (IOException e) { e.printStackTrace(); } bos.close(); } } }