Save to Android local file system
//package com.a4studio.android.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import android.os.Environment;
import android.util.Log;
public class NewsPersistence {
private final static String TAG = NewsPersistence.class.getSimpleName();
private File sdcardStorage;
private String sdcardPath;
private String newsPath;
private final static String PACKAGE_NAME = "news_wmu";
private final static String fileExtensionName = ".wdata";
private int fileNum = 0;
public NewsPersistence() throws Exception
{
if(Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_REMOVED))
{
throw new Exception("no external storage like sdcard");
}
sdcardStorage = Environment.getExternalStorageDirectory();
sdcardPath = sdcardStorage.getParent() + java.io.File.separator + sdcardStorage.getName();
newsPath = sdcardPath+java.io.File.separator + PACKAGE_NAME;
File newsFile = new File(newsPath);
if(!newsFile.exists())
{
newsFile.mkdir();
}
}
public synchronized List<String> getFileList()
{
List<String> files = new ArrayList<String>();
File dir = new File(newsPath);
for(File file : dir.listFiles())
{
if(file.isFile() && file.getName().endsWith(fileExtensionName)){
files.add(file.getName());
}
}
return files;
}
public synchronized void storeNewsItemList(List<NewsItem> items) throws IOException
{
for(NewsItem item : items)
{
fileNum++;
long ticket = System.currentTimeMillis();
String filename = newsPath+java.io.File.separator + ticket+fileNum+fileExtensionName;
File newfile = new File(filename);
newfile.createNewFile();
ObjectOutputStream objOutput = new ObjectOutputStream(new FileOutputStream(newfile));
objOutput.writeObject(item);
}
}
public synchronized void storeNewsItem(NewsItem item) throws IOException
{
Log.d(TAG, "storeNewsItem"+item.getContent());
fileNum++;
long ticket = System.currentTimeMillis();
String filename = newsPath + java.io.File.separator + ticket + fileNum
+ fileExtensionName;
File newfile = new File(filename);
newfile.createNewFile();
ObjectOutputStream objOutput = new ObjectOutputStream(
new FileOutputStream(newfile));
objOutput.writeObject(item);
}
public synchronized List<NewsItem> retrieveNewsItem() throws Exception
{
File dir = new File(newsPath);
List<NewsItem> items = new ArrayList<NewsItem>();
for(File file : dir.listFiles())
{
if(file.isFile() && file.getName().endsWith(fileExtensionName))
{
ObjectInputStream input = new ObjectInputStream(new FileInputStream(file));
NewsItem item = (NewsItem)input.readObject();
Log.d(TAG, "retrieveNewsItem"+file.getName());
items.add(item);
}
}
return items;
}
public synchronized void removeAllNewsItems()
{
File dir = new File(newsPath);
for(File file : dir.listFiles())
{
if(!file.delete())
{
Log.e(TAG, "delete file "+file.getName() + "delete fail");
}
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
class NewsItem implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
public final static String RESULT = "Result";
public final static String TITLE = "Title";
public final static String SUMMARY = "Summary";
public final static String URL = "Url";
public final static String CLICKURL = "ClickUrl";
public final static String NEWSSOURCE = "NewsSource";
public final static String NEWSSOURCEURL = "NewsSourceUrl";
public final static String LANGUAGE = "Language";
public final static String PUBLISHDATE = "PublishDate";
public final static String MODIFICATIONDATE = "ModificationDate";
private String title;
private String summary;
private String url;
private String clickUrl;
private String newsSource;
private String newsSrouceUrl;
private String language;
private long publishDate;
private long modifDate;
private String content;
/**
* @return the title
*/
public String getTitle() {
return title;
}
/**
* @param title
* the title to set
*/
public void setTitle(String title) {
this.title = title;
}
/**
* @return the summary
*/
public String getSummary() {
return summary;
}
/**
* @param summary
* the summary to set
*/
public void setSummary(String summary) {
this.summary = summary;
}
/**
* @return the url
*/
public String getUrl() {
return url;
}
/**
* @param url
* the url to set
*/
public void setUrl(String url) {
this.url = url;
}
/**
* @return the clickUrl
*/
public String getClickUrl() {
return clickUrl;
}
/**
* @param clickUrl
* the clickUrl to set
*/
public void setClickUrl(String clickUrl) {
this.clickUrl = clickUrl;
}
/**
* @return the newsSource
*/
public String getNewsSource() {
return newsSource;
}
/**
* @param newsSource
* the newsSource to set
*/
public void setNewsSource(String newsSource) {
this.newsSource = newsSource;
}
/**
* @return the newsSrouceUrl
*/
public String getNewsSrouceUrl() {
return newsSrouceUrl;
}
/**
* @param newsSrouceUrl
* the newsSrouceUrl to set
*/
public void setNewsSrouceUrl(String newsSrouceUrl) {
this.newsSrouceUrl = newsSrouceUrl;
}
/**
* @return the language
*/
public String getLanguage() {
return language;
}
/**
* @param language
* the language to set
*/
public void setLanguage(String language) {
this.language = language;
}
/**
* @return the publishDate
*/
public long getPublishDate() {
return publishDate;
}
/**
* @param publishDate
* the publishDate to set
*/
public void setPublishDate(long publishDate) {
this.publishDate = publishDate;
}
/**
* @return the modifDate
*/
public long getModifDate() {
return modifDate;
}
/**
* @param modifDate
* the modifDate to set
*/
public void setModifDate(long modifDate) {
this.modifDate = modifDate;
}
/**
* @return the content
*/
public String getContent() {
return content;
}
/**
* @param content the content to set
*/
public void setContent(String content) {
this.content = content;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
// TODO Auto-generated method stub
return this.title+"\n"+
this.summary +"\n"+
this.url+"\n"+
this.clickUrl+"\n"+
this.newsSource+"\n"+
this.newsSrouceUrl+"\n"+
this.language+"\n"+
this.publishDate+"\n"+
this.modifDate+"\n";
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
Related examples in the same category