本文介绍了如何保存一个ArrayList文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想写ArrayList的一个文件。但我不知道怎么办。
也许你能帮助我。我怎样才能将它保存到一个文件?并且是code好这么远?也许你有足够的时间通过它来看看。
非常感谢,Vinzenz
公共类SongsManager {最后弦乐MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath()+/;
私人的ArrayList<&HashMap的LT;字符串,字符串>> songsList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
私人字符串mp3Pattern =.MP3
私人字符串mp4Pattern =.MP4
私人字符串MP3Pattern =.MP3
私人字符串MP4Pattern =.MP4//构造
公共SongsManager(){}/ **
*功能读取所有MP3文件,并存储在细节
* ArrayList的
* * /
公众的ArrayList<&HashMap的LT;字符串,字符串>> getPlayList(){
的System.out.println(MEDIA_PATH);
如果(MEDIA_PATH!= NULL){
文件首页=新的文件(MEDIA_PATH);
文件[] listFiles = home.listFiles();
如果(listFiles =空&放大器;!&放大器; listFiles.length大于0){
对于(文件文件:listFiles){
的System.out.println(file.getAbsolutePath());
如果(file.isDirectory()){
scanDirectory(文件);
}其他{
addSongToList(文件);
}
}
}
}
//返回歌曲列表数组
返回songsList;
}私人无效scanDirectory(文件目录){
如果(目录!= NULL){
文件[] listFiles = directory.listFiles();
如果(listFiles =空&放大器;!&放大器; listFiles.length大于0){
对于(文件文件:listFiles){
如果(file.isDirectory()){
scanDirectory(文件);
}其他{
addSongToList(文件);
} }
}
}
}私人无效addSongToList(文件歌曲){
如果(song.getName()。的endsWith(mp3Pattern)|| song.getName()。的endsWith(mp4Pattern)|| song.getName()。的endsWith(MP4Pattern)|| song.getName()的endsWith。(MP3Pattern)){
HashMap的<字符串,字符串> songMap =新的HashMap<字符串,字符串>();
songMap.put(SONGTITLE
。song.getName()子串(0,(song.getName()长度() - 4)));
songMap.put(songPath,song.getPath()); //添加每一首歌的SongList
songsList.add(songMap);
}
}
私人无效saveFiletoTXT(){
尝试{
java.io.FileOutputStream中的FOS =新java.io.FileOutputStream中(/ SD卡/);
java.io.ObjectOutputStream中OOS =新java.io.ObjectOutputStream中(FOS); oos.writeObject((java.util.ArrayList中)songsList);
oos.flush();
fos.close();
}
赶上(例外五){}
}
}
解决方案
虽然你可以从字面上连载
的的ArrayList
,我真的不认为你想这样做。我觉得更有意义,使用 android.database.sqlite包描述
的。
I want to write the arraylist to a file. But I don't know how.Maybe you can help me. How can I save it to a File? And is the code good so far? Maybe you've enough time to look through it.Thanks a lot, Vinzenz
public class SongsManager {
final String MEDIA_PATH = Environment.getExternalStorageDirectory()
.getPath() + "/";
private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
private String mp3Pattern = ".mp3";
private String mp4Pattern = ".mp4";
private String MP3Pattern = ".MP3";
private String MP4Pattern = ".MP4";
// Constructor
public SongsManager() {
}
/**
* Function to read all mp3 files and store the details in
* ArrayList
* */
public ArrayList<HashMap<String, String>> getPlayList() {
System.out.println(MEDIA_PATH);
if (MEDIA_PATH != null) {
File home = new File(MEDIA_PATH);
File[] listFiles = home.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
System.out.println(file.getAbsolutePath());
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
// return songs list array
return songsList;
}
private void scanDirectory(File directory) {
if (directory != null) {
File[] listFiles = directory.listFiles();
if (listFiles != null && listFiles.length > 0) {
for (File file : listFiles) {
if (file.isDirectory()) {
scanDirectory(file);
} else {
addSongToList(file);
}
}
}
}
}
private void addSongToList(File song) {
if (song.getName().endsWith(mp3Pattern) || song.getName().endsWith(mp4Pattern) || song.getName().endsWith(MP4Pattern) || song.getName().endsWith(MP3Pattern)) {
HashMap<String, String> songMap = new HashMap<String, String>();
songMap.put("songTitle",
song.getName().substring(0, (song.getName().length() - 4)));
songMap.put("songPath", song.getPath());
// Adding each song to SongList
songsList.add(songMap);
}
}
private void saveFiletoTXT() {
try{
java.io.FileOutputStream fos = new java.io.FileOutputStream("/sdcard/");
java.io.ObjectOutputStream oos = new java.io.ObjectOutputStream(fos);
oos.writeObject((java.util.ArrayList) songsList);
oos.flush();
fos.close();
}
catch(Exception e){}
}
}
解决方案
While you could literally serialize
an ArrayList
, I really don't think you want to do that. I think it makes more sense to store its contents in SQLLite using the android.database.sqlite package
as described here.
这篇关于如何保存一个ArrayList文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!