程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> java的io操作(將字符串寫入到txt文件中)

java的io操作(將字符串寫入到txt文件中)

編輯:關於JAVA

java的io操作(將字符串寫入到txt文件中)。本站提示廣大學習愛好者:(java的io操作(將字符串寫入到txt文件中))文章只能為提供參考,不一定能成為您想要的結果。以下是java的io操作(將字符串寫入到txt文件中)正文


本文實例講述了Android操作寄存在assets文件夾下SQLite數據庫的辦法。分享給年夜家供年夜家參考。詳細以下:

由於此次的項目須要自帶數據,所以就就把數據都放到一個SQLite的數據庫文件中了,以後把該文件放到了assets文件夾上面。一開端盤算每次都從assets文件夾上面把該文件夾拷貝得手機的SD卡或許手機本身的存儲上以後再應用,後來斟酌到每次都拷貝的話效力不高,而且假如觸及到對數據庫的修正操作的話拷貝以後數據就被恢復了。

是以就寫了該封裝,該封裝只是在第一次應用數據庫文件的時刻把該文件夾拷貝得手機的/data/data/運用法式報名/database文件夾下,以後就直接從這個處所應用了。而且它許可你直接經由過程assets文件夾下的數據庫稱號來獲得SQLiteDatabase對象,如許就極年夜的便利了你對數據庫的應用。

封裝以下:

package com.sin.android.database; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.InputStream; 
import java.io.OutputStream; 
import java.util.HashMap; 
import java.util.Map; 
import android.content.Context; 
import android.content.SharedPreferences; 
import android.content.res.AssetManager; 
import android.database.sqlite.SQLiteDatabase; 
import android.util.Log; 
/** 
 * This is a Assets Database Manager 
 * Use it, you can use a assets database file in you application 
 * It will copy the database file to "/data/data/[your application package name]/database" when you first time you use it 
 * Then you can get a SQLiteDatabase object by the assets database file
 * @author RobinTang 
 * @time 2012-09-20 
 * 
 * 
 * How to use: 
 * 1. Initialize AssetsDatabaseManager 
 * 2. Get AssetsDatabaseManager 
 * 3. Get a SQLiteDatabase object through database file 
 * 4. Use this database object 
 * 
 * Using example: 
 * AssetsDatabaseManager.initManager(getApplication()); // this method is only need call one time 
 * AssetsDatabaseManager mg = AssetsDatabaseManager.getManager();  // get a AssetsDatabaseManager object 
 * SQLiteDatabase db1 = mg.getDatabase("db1.db");  // get SQLiteDatabase object, db1.db is a file in assets folder 
 * db1.??? // every operate by you want 
 * Of cause, you can use AssetsDatabaseManager.getManager().getDatabase("xx") to get a database when you need use a database 
 */ 
public class AssetsDatabaseManager { 
  private static String tag = "AssetsDatabase"; // for LogCat 
  private static String databasepath = "/data/data/%s/database"; // %s is packageName 
  // A mapping from assets database file to SQLiteDatabase object 
  private Map<String, SQLiteDatabase> databases = new HashMap<String, SQLiteDatabase>(); 
  // Context of application 
  private Context context = null; 
  // Singleton Pattern 
  private static AssetsDatabaseManager mInstance = null; 
  /** 
   * Initialize AssetsDatabaseManager 
   * @param context, context of application 
   */ 
  public static void initManager(Context context){ 
    if(mInstance == null){ 
      mInstance = new AssetsDatabaseManager(context); 
    } 
  } 
  /** 
   * Get a AssetsDatabaseManager object 
   * @return, if success return a AssetsDatabaseManager object, else return null 
   */ 
  public static AssetsDatabaseManager getManager(){ 
    return mInstance; 
  } 
  private AssetsDatabaseManager(Context context){ 
    this.context = context; 
  } 
  /** 
   * Get a assets database, if this database is opened this method is only return a copy of the opened database 
   * @param dbfile, the assets file which will be opened for a database 
   * @return, if success it return a SQLiteDatabase object else return null 
   */ 
  public SQLiteDatabase getDatabase(String dbfile) { 
    if(databases.get(dbfile) != null){ 
      Log.i(tag, String.format("Return a database copy of %s", dbfile)); 
      return (SQLiteDatabase) databases.get(dbfile); 
    } 
    if(context==null) 
      return null; 
    Log.i(tag, String.format("Create database %s", dbfile)); 
    String spath = getDatabaseFilepath(); 
    String sfile = getDatabaseFile(dbfile); 
    File file = new File(sfile); 
    SharedPreferences dbs = context.getSharedPreferences(AssetsDatabaseManager.class.toString(), 0); 
    boolean flag = dbs.getBoolean(dbfile, false); // Get Database file flag, if true means this database file was copied and valid
    if(!flag || !file.exists()){ 
      file = new File(spath); 
      if(!file.exists() && !file.mkdirs()){ 
        Log.i(tag, "Create \""+spath+"\" fail!"); 
        return null; 
      } 
      if(!copyAssetsToFilesystem(dbfile, sfile)){ 
        Log.i(tag, String.format("Copy %s to %s fail!", dbfile, sfile)); 
        return null; 
      } 
      dbs.edit().putBoolean(dbfile, true).commit(); 
    } 
    SQLiteDatabase db = SQLiteDatabase.openDatabase(sfile, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); 
    if(db != null){ 
      databases.put(dbfile, db); 
    } 
    return db; 
  } 
  private String getDatabaseFilepath(){ 
    return String.format(databasepath, context.getApplicationInfo().packageName); 
  } 
  private String getDatabaseFile(String dbfile){ 
    return getDatabaseFilepath()+"/"+dbfile; 
  } 
  private boolean copyAssetsToFilesystem(String assetsSrc, String des){ 
    Log.i(tag, "Copy "+assetsSrc+" to "+des); 
    InputStream istream = null; 
    OutputStream ostream = null; 
    try{ 
      AssetManager am = context.getAssets(); 
      istream = am.open(assetsSrc); 
      ostream = new FileOutputStream(des); 
      byte[] buffer = new byte[1024]; 
      int length; 
      while ((length = istream.read(buffer))>0){ 
        ostream.write(buffer, 0, length); 
      } 
      istream.close(); 
      ostream.close(); 
    } 
    catch(Exception e){ 
      e.printStackTrace(); 
      try{ 
        if(istream!=null) 
          istream.close(); 
        if(ostream!=null) 
          ostream.close(); 
      } 
      catch(Exception ee){ 
        ee.printStackTrace(); 
      } 
      return false; 
    } 
    return true; 
  } 
  /** 
   * Close assets database 
   * @param dbfile, the assets file which will be closed soon 
   * @return, the status of this operating 
   */ 
  public boolean closeDatabase(String dbfile){ 
    if(databases.get(dbfile) != null){ 
      SQLiteDatabase db = (SQLiteDatabase) databases.get(dbfile);
      db.close(); 
      databases.remove(dbfile); 
      return true; 
    } 
    return false; 
  } 
  /** 
   * Close all assets database 
   */ 
  static public void closeAllDatabase(){ 
    Log.i(tag, "closeAllDatabase"); 
    if(mInstance != null){ 
      for(int i=0; i<mInstance.databases.size(); ++i){ 
        if(mInstance.databases.get(i)!=null){ 
          mInstance.databases.get(i).close(); 
        } 
      } 
      mInstance.databases.clear(); 
    } 
  } 
} 

應用的進程也很簡略,運用法式開端的時刻初始化一下,以後便可以在隨意率性處所獲得治理器在經由過程assets文件夾下的數據庫文件名直接獲得SQLiteDatabase對象,以後對數據庫的操作就完整看你了。。。
簡略的應用例子:

// 初始化,只須要挪用一次 
AssetsDatabaseManager.initManager(getApplication()); 
// 獲得治理對象,由於數據庫須要經由過程治理對象能力夠獲得 
AssetsDatabaseManager mg = AssetsDatabaseManager.getManager(); 
// 經由過程治理對象獲得數據庫 
SQLiteDatabase db1 = mg.getDatabase("db1.db"); 
// 對數據庫停止操作 
db1.execSQL("insert into tb([ID],[content]) values(null, 'db1');"); 

須要留意的是獲得數據庫對象的時刻是辨別數據庫文件名的年夜小寫的。

願望本文所述對年夜家的Android法式設計有所贊助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved