public class LocaHostUtils { //获取当前的时间 public static String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); //这个路径是 当前路径/OPE/ public static String savepath = "/sdcard/a/"; public static String logpath = "/sdcard/b"; public static String imagePath = "/sdcard/c"; public static String imageDtPath = "/sdcard/d"; public static File fileName; public static void saveLogData(String strcontent, String bundName) { //这个路径是 信息 String fileNamee = ".txt"; File file1 = new File(logpath); if (!file1.exists()) { file1.mkdirs(); //创建文夹 } File file2 = new File(file1, bundName + fileNamee); if (!file2.exists()) { try { file2.createNewFile(); //创建文件 } catch (IOException e) { e.printStackTrace(); } } //一行一行的读 FileWriter fw = null; BufferedWriter bw = null; // String datetime = ""; try { fw = new FileWriter(file2, true); //对这个进行扫描 bw = new BufferedWriter(fw); //缓冲区对文字的写入 String myreadline = datetime + "" + strcontent; bw.write(myreadline + "\n"); //往进写的东西 bw.flush(); //刷新 bw.close(); fw.close(); } catch (IOException e) { e.printStackTrace(); } fileName = file2; } //创建文件夹 public static Boolean creatFolder(String foldername) { Boolean f = false; File file1 = new File(foldername); if (!file1.exists()) { f = file1.mkdirs(); //创建文夹 return f; } return f; } //通过路径找到这个文件的名字 public static String getFileName(String pathandname) { int start = pathandname.lastIndexOf("/"); int end = pathandname.lastIndexOf("."); if (start != -1 && end != -1) { return pathandname.substring(start + 1, end); } else { return null; } } //这个是将数据库里面的路径寻找到 public static void getJson(List<String> path) { File file = new File(savepath + timeStamp); if (!file.exists()) { file.mkdirs(); } for (int i = 0; i < path.size(); i++) { char c = (char) (Math.random() * 26 + 97); //随机数 copyFile(path.get(i), file + File.separator + c + ".jpeg"); } } /** * 复制单个文件 * @param oldPath String 原文件路径 如:c:/fqf.txt * @param newPath String 复制后路径 如:f:/fqf.txt */ public static void copyFile(String oldPath, String newPath) { try { int bytesum = 0; int byteread = 0; //Log.e("copyFile1", "oldPath:" + oldPath + "---------->newPath:" + newPath); File oldfile = new File(oldPath); if (oldfile.exists()) { //文件存在时 InputStream inStream = new FileInputStream(oldPath); //读入原文件 FileOutputStream fs = new FileOutputStream(newPath); byte[] buffer = new byte[1024 * 5]; while ((byteread = inStream.read(buffer)) != -1) { bytesum += byteread; //字节数 文件大小 fs.write(buffer, 0, byteread); } fs.flush();//刷新流 fs.close();//关闭 inStream.close();//关闭输入流 } } catch (Exception e) { e.printStackTrace(); } } //将bitmap 类型的图片保存到本地 public static String saceBitmap(Bitmap bitmap, String name, String projectname, String mfloorname) { File file = new File(imagePath); if (!file.exists()) { //如果不存在的话、就让他进行创建文件 file.mkdir(); //创建文件 } File file1 = new File(file + File.separator, timeStamp + "_" + name + ".jpeg"); try { OutputStream os = new FileOutputStream(file1); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.close(); os.flush(); } catch (IOException e) { e.printStackTrace(); Log.e("----", e.toString()); return null; } return file1.getAbsolutePath(); } //将bitmap 类型的图片保存到本地 public static String saceDtBitmap(Bitmap bitmap, String name) { File file = new File(imageDtPath); if (!file.exists()) { //如果不存在的话、就让他进行创建文件 file.mkdir(); //创建文件 } File file1 = new File(file + File.separator, timeStamp + "_" + name + ".jpeg"); try { OutputStream os = new FileOutputStream(file1); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os); os.close(); os.flush(); } catch (IOException e) { e.printStackTrace(); Log.e("----", e.toString()); return null; } return file1.getAbsolutePath(); } //删除本地文件 public static boolean deleteFile(String filePath) { File file = new File(filePath); if (!file.exists()) { return false; } if (!file.isFile()) { return false; } return file.delete(); } //判断文件夹是不是空的 public static boolean folderisBlank(String foldername) { File file = new File(foldername); // 当前目录下的 testdir目录 if (file.isDirectory()) { if (file.list().length > 0) { return false; } else { return true; } } return false; } }
本地文件操作类
猜你喜欢
转载自blog.csdn.net/qq_35700935/article/details/80703868
今日推荐
周排行