1 import java.io.File; 2 import java.io.FileOutputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5 import java.io.OutputStream; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import android.os.Environment; 10 11 import com.androidmediaplayer.model.Mp3Info; 12 13 public class FileUtil { 14 15 /** 16 * 得到当前外部存储设备的目录 17 */ 18 private static String SDCardRoot = Environment 19 .getExternalStorageDirectory().getAbsolutePath() + File.separator; 20 21 /** 22 * 在SD卡上创建文件 23 * 24 * @throws IOException 25 */ 26 public static File createFileInSDCard(String fileName, String dir) 27 throws IOException { 28 if (isFileExist(fileName, dir)) { 29 deleteFile(dir, fileName); 30 } 31 File file = new File(SDCardRoot + dir + File.separator + fileName); 32 file.createNewFile(); 33 return file; 34 } 35 36 /** 37 * 在SD卡上创建目录 38 * 39 * @param dirName 40 */ 41 public static File creatSDDir(String dir) { 42 File dirFile = new File(SDCardRoot + dir + File.separator); 43 dirFile.mkdirs(); 44 return dirFile; 45 } 46 47 /** 48 * 删除SD卡上的文件 49 * 50 * @param dir 51 * 文件夹目录(已包含根目录) 52 * @param fileName 53 * 文件名 54 * @return 55 * @throws IOException 56 */ 57 public static boolean deleteFile(String dir, String fileName) 58 throws IOException { 59 File dirFile = new File(SDCardRoot + dir + File.separator + fileName); 60 return dirFile.delete(); 61 } 62 63 /** 64 * 删除_data路径的文件 65 * 66 * @param _data 67 * @return 68 */ 69 public static boolean deleteAnotherFile(String _data) throws IOException { 70 File dirFile = new File(_data); 71 return dirFile.delete(); 72 } 73 74 /** 75 * 判断SD卡上的文件是否存在 76 */ 77 public static boolean isFileExist(String fileName, String path) { 78 File file = new File(SDCardRoot + path + File.separator + fileName); 79 return file.exists(); 80 } 81 82 public static boolean _isFileExist(String fileName) { 83 File file = new File(SDCardRoot + "mp3" + File.separator + fileName); 84 return file.exists(); 85 } 86 87 /** 88 * 89 * @param path 路径加文件名加后缀 90 * @return 91 */ 92 public static boolean _isFileExist2(String path){ 93 File file = new File(path); 94 return file.exists(); 95 } 96 97 /** 98 * 判断SD卡上的文件夹是否存在 99 */ 100 public static boolean isFilePathExist(String path) { 101 File file = new File(SDCardRoot + path); 102 return file.exists(); 103 } 104 105 public static String getFilePath(String fileName) { 106 return SDCardRoot + "mp3" + File.separator + fileName; 107 } 108 109 public static void updateFileName(File file, String finalFileName) { 110 String path = file.getAbsolutePath(); 111 String orgFileName = path.substring(0, path.lastIndexOf("/") + 1) 112 + finalFileName; 113 file.renameTo(new File(orgFileName)); 114 } 115 116 /** 117 * 将一个InputStream里面的数据写入到SD卡中 118 */ 119 public static File write2SDFromInput(String path, String fileName, 120 InputStream inPutStream) { 121 File file = null; 122 OutputStream outPutStream = null; 123 try { 124 creatSDDir(path); 125 file = createFileInSDCard(fileName, path); 126 outPutStream = new FileOutputStream(file); 127 byte buffer[] = new byte[64]; 128 int temp; 129 while ((temp = inPutStream.read(buffer)) != -1) { 130 outPutStream.write(buffer, 0, temp); 131 } 132 outPutStream.flush(); 133 } catch (Exception e) { 134 e.printStackTrace(); 135 } finally { 136 try { 137 if (outPutStream != null) { 138 outPutStream.close(); 139 } 140 } catch (Exception e) { 141 e.printStackTrace(); 142 } 143 } 144 return file; 145 } 146 147 /** 148 * 读取目录中的MP3文件的名字和大小 149 */ 150 public static List<Mp3Info> getMp3Files(String path) { 151 ArrayList<Mp3Info> mp3Infos = new ArrayList<Mp3Info>(); 152 File file = new File(SDCardRoot + File.separator + path); 153 File[] files = file.listFiles(); 154 Mp3Info mp3Info = null; 155 int length = files.length; 156 for (int i = 0; i < length; i++) { 157 if (files[i].getName().endsWith("mp3")) { 158 mp3Info = new Mp3Info(); 159 mp3Info.setMp3Name(files[i].getName()); 160 mp3Info.setMp3Size(String.valueOf(files[i].length())); 161 String[] temp = mp3Info.getMp3Name().split("\\."); 162 String lrcName = temp[0] + ".lrc"; 163 mp3Info.setLrcName(lrcName); 164 mp3Infos.add(mp3Info); 165 } 166 } 167 return mp3Infos; 168 } 169 170 }
转载于:https://www.cnblogs.com/jiedi/archive/2012/05/23/2515510.html