HongxinLocalSaveUtil
public class HongxinLocalSaveUtil{
private static final String SAVE_URL = "/youban/radio/cache/hongxindata/";
private static final String FILENAME = "record.cfg";
private static HongxinLocalSaveUtilinstance = null;
private static String SDPATH;
public static HongxinLocalSaveUtilgetInstance() {
if (instance == null) {
instance = new HongxinLocalSaveUtil();
}
return instance;
}
private HongxinLocalSaveUtil() {
SDPATH = android.os.Environment.getExternalStorageDirectory()+ "";
}
/**
* 是否能创建和写入文件
*/
public boolean isCanCreateAndWriteFile() {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
return true;
}
return false;
}
// 创建目录
public File createDir(String fileName) throws IOException {
File dir = new File(SDPATH + fileName);
dir.mkdirs();
return dir;
}
// 创建文件
public File createFile(String fileName) throws IOException {
File file = new File(SDPATH + fileName);
file.createNewFile();
return file;
}
public void saveRecord(List<MusicData> musicDatas) {
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
FileOutputStream outStream = null;
DataOutputStream dos = null;
try {
if (!isExist()) {
File file = new File(SDPATH + SAVE_URL + FILENAME);
if (file != null) {
file.delete();
}
}
createDir(SAVE_URL);
File file = createFile(SAVE_URL + FILENAME);
outStream = new FileOutputStream(file);
dos = newDataOutputStream(outStream);
if (musicDatas != null) {
int size = musicDatas.size();
dos.writeInt(size);
for (int i = 0 ; i < size ; i++) {
MusicData musicData = musicDatas.get(i);
if (musicData == null) {
continue;
}
dos.writeUTF(musicData.getImageUrl());
dos.writeUTF(musicData.getMp3Long());
dos.writeUTF(musicData.getMusicName());
dos.writeUTF(musicData.getPlayLink());
dos.writeInt(musicData.getID());
dos.writeUTF(musicData.getLyrics());
}
}else {
dos.writeInt(0);
}
dos.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
} finally {
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
if (outStream != null) {
try {
outStream.close();
} catch (Exception e) {
}
}
}
}
}
// 判断文件是否存在
public boolean isExist() {
File file = new File(SDPATH + SAVE_URL + FILENAME);
return file.exists();
}
public List<MusicData> readData() {
/* sdcard可读状态 */
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
if (!isExist()) {
return null;
}
FileInputStream fis = null;
DataInputStream dis = null;
try {
File file = new File(SDPATH + SAVE_URL + FILENAME);
fis = new FileInputStream(file);
dis = new DataInputStream(fis);
int size = dis.readInt();
if (size == 0) {
return null;
}
List<MusicData> musicDatas = newArrayList<MusicData>();
for (int i = 0 ; i < size ; i++) {
MusicData musicData = new MusicData();
musicData.setImageUrl(dis.readUTF());
musicData.setMp3Long(dis.readUTF());
musicData.setMusicName(dis.readUTF());
musicData.setPlayLink(dis.readUTF());
musicData.setID(dis.readInt());
musicData.setLyrics(dis.readUTF());
musicDatas.add(musicData);
}
return musicDatas;
} catch (Exception e) {
} finally {
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generatedcatch block
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
}
}
}
}
return null;
}
}
需要导入权限
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE" />