Save to Android local file system : File « File « Android






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

1.Read the created file and display to the screen
2.Create a Uri for files on external storage
3.Create a Uri for files on internal storage
4.File read and write
5.View/Listen to media file
6.File Copy Util
7.Copy File Thread
8.Adaptation of the class File to add some usefull functions
9.Using Ini file to manage Preferences
10.File Cache
11.Format File Size
12.File Upload
13.File Name Extension Utils
14.Write and append string to file
15.Transfers required files to the the private file system part in order to be access them from C Code.
16.returns a canonical line of a obj or mtl file.
17.Trims down obj files, so that they may be parsed faster later on.
18.Format File Size:KB, MB, GB
19.Get File Contents as String
20.Read File and return CharSequence
21.Copy File
22.Read/write From FileSystem, browse Account
23.Create Path and file under External Storage
24.Get File Extension Name
25.Extract File Name From String
26.Write String to file
27.Create an output file from raw resources.
28.Copies a directory from a jar file to an external directory.
29.Reads a file from /raw/res/ and returns it as a String
30.Read Config ini file
31.Read Write File Manager
32.Copy two files
33.Get the filename of the media file and use that as the default title
34.Copy File and Directory
35.Determines the MIME type for a given filename.
36.Move a file stored in the cache to the internal storage of the specified context
37.CharSequence from File
38.File Manager
39.Save Exception to a file
40.Delete a file and create directory
41.Get a list of filenames in this folder.
42.Delete Directory
43.Delete Directory 2
44.Delete Directory 3
45.Get readable folder size
46.Copy chars from a large (over 2GB) Reader to a Writer.