350 likes | 510 Views
Chapter 8. Saving and Reading Files. Java I/O. Write a Text File. FileWriter FileWriter 寫入單ä½ç‚º char 。產生物件方å¼å¦‚下: 在 FileWriter 物件åƒæ•¸ç•¶ä¸ï¼Œç¬¬ä¸€å€‹ç‚ºæª”案å稱,第二個為寫入模å¼æ˜¯å¦ç‚º append BufferedWriter 使用 Buffer æ©Ÿåˆ¶ä¾†åš write() 時,會先將è¦å¯«å…¥ä¹‹æª”案暫å˜èµ·ä¾†ï¼Œç‰åˆ°ä¸€å®šçš„資料é‡å¾Œæ‰å¯«å…¥ç£ç¢Ÿï¼Œå› æ¤å¯çœä¸‹ä¸å°‘ I/O æ‰€é€ æˆçš„è² æ“”ã€‚. Write a Text File.
E N D
Chapter 8 Saving and Reading Files
Write a Text File • FileWriter • FileWriter寫入單位為char。產生物件方式如下: • 在FileWriter物件參數當中,第一個為檔案名稱,第二個為寫入模式是否為append • BufferedWriter • 使用Buffer機制來做write()時,會先將要寫入之檔案暫存起來,等到一定的資料量後才寫入磁碟,因此可省下不少I/O所造成的負擔。
Write a Text File The most applied methods of BufferedWriter
Write a Text File Results of the example are shown below:
Write a Text File public class JavaWriteData extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { // 建立FileWriter物件,並將寫入位置設定為SD卡中的output.txt FileWriter fw = new FileWriter( "/sdcard/output.txt", false ); // 建立fw的Output Buffer BufferedWriter bw = new BufferedWriter( fw ); bw.write("Hello, Android"); bw.newLine(); bw.close(); } catch (IOException e) { e.printStackTrace(); } } } Codes are as below:
File Stream Output • FileOutputStream • 此種方式是以byte為單位對檔案作存取,故通常在使用這種方式來做檔案讀寫時會一起使用其他Object的OutputStream,目的是將我們要儲存的目標檔案自動以byte的形式作儲存
File Stream Output Results of the example are shown below :
File Stream Output public class JavaOutputStream extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { String data1 = "This is OutputStream data"; String data2 = "\n"; FileOutputStream output = new FileOutputStream("/sdcard/output.txt"); output.write(data1.getBytes()); output.write(data2.getBytes()); output.close(); } catch (Exception e) { e.printStackTrace(); } } } Codes are as below :
Read a Text File • FileReader • FileReader讀取單位為char。產生物件方式如下: • BufferedReader
Read a Text File The most applied methods of BufferedWriter
Read a Text File Results of the example are shown below :
Read a Text File public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { // 建立FileReader物件,設定讀取的檔案為SD卡中的output.txt FileReader fr = new FileReader( "/sdcard/output.txt" ); // 建立fr的Input Buffer BufferedReader br = new BufferedReader( fr ); String readData = ""; String temp = br.readLine(); while( temp != null ) { readData += temp; temp = br.readLine(); } Context context = getApplicationContext(); int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context, readData, duration); toast.show(); } catch (Exception e) { e.printStackTrace(); } } Codes are as below:
File Stream Input • FileInputStream • 此種方式如同FileOutputStream是以byte為單位,故此方法通常也用於不同物件的讀取
File Stream Input Results of the example are shown below :
File Stream Input public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); try { FileInputStream input = new FileInputStream("/sdcard/output.txt"); String data = ""; while (input.available() > 0) { byte [] b = new byte[10]; if ( input.read(b) != -1 ) data += new String(b); else break; } Context context = getApplicationContext(); int duration = Toast.LENGTH_LONG; Toast toast = Toast.makeText(context, data, duration); toast.show(); } catch (Exception e) { e.printStackTrace(); } } Codes are as below:
SQLite • Features of SQLite : • 不需要一個額外的系統來運行整個資料庫系統 • 寫入或是讀取資料都是直接連結到檔案中 • 由於都以檔案型式存在,所以可以將此資料隨意在大部分平台下使用 • 支援大部分SQL92的語法 • 運行資料庫操作時所佔用的資源較小
Introduction on SQL Language • CREATE TABLE • Create table用來建立表格,而表格分為列(row)和欄(column),而表格當中的資料可以有不同的資料型態
Introduction on SQL Language • CREATE TABLE Example
Introduction on SQL Language Results of CREATE TABLEExample:
Introduction on SQL Language • ALTER TABLE • Alter table用來更改Table,如新增、刪除、更改欄位屬性或名稱,更改指令如下表: • Using method of Alter table is shown below:
Introduction on SQL Language • DROP TABLE • Drop table指令為刪除一個表格,其用法如下:
Introduction on SQL Language • INSERT • Insert指令用於將資料輸入到表格當中,使用方法如下:
Introduction on SQL Language • SELECT • Select指令用於查詢資料庫當中符合條件的資料,使用方法如下:
Introduction on SQL Language • UPDATE • Update指令可修改資料表當中的值,使用方法如下:
Introduction on SQL Language • DELETE • Delete指令可將符合條件的資料給刪除,使用方法如下:
Introduction on SQL Language 使用adb工具進入模擬器中,接著在模擬器當中使用sqlite3這個工具,使用方法為:
SQLite Development–EX1 src/ncu/bnlab/MyDatabase.java public class MyDatabase extends SQLiteOpenHelper { public MyDatabase(Context context, String name, CursorFactory factory,int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { // TODO Auto-generated method stub } }
SQLite Development–EX1 src/ncu/bnlab/sqliteExample.java public class sqliteExample extends Activity { // 設定DATABASE檔案名稱 private final String DATABASE_NAME = "example1"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyDatabase myDBHelper = new MyDatabase(this, DATABASE_NAME, null, 3); }
SQLite Development–EX2 src/ncu/bnlab/MyDatabase.java public class MyDatabase extends SQLiteOpenHelper{ public String DATABASE_TABLE = "STUDENTINFO"; public final String DB_CREATE_TABLE = "CREATE TABLE " + DATABASE_TABLE + "( " + "ID INTEGER NOT NULL," + "NAME CHAR(20) NOT NULL," + "PHONE CHAR(20)," + "CLASS CHAR(50)," + "PRIMARY KEY(ID) );"; public MyDatabase(Context context, String name, CursorFactory factory, int version) { super(context, name, factory, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(DB_CREATE_TABLE); } @Override public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) { // TODO Auto-generated method stub db.execSQL("DROP IF TABLE EXISTS " + DATABASE_TABLE); onCreate(db); } }
SQLite Development–EX4 src/ncu/bnlab/sqliteExample.java public class sqliteExample extends Activity { private final String DATABASE_NAME = "example1"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); MyDatabase myDBHelper = new MyDatabase(this, DATABASE_NAME, null, 3); SQLiteDatabase db = myDBHelper.getWritableDatabase(); // 搜尋STUDENTINFO資料表中的資料 String cmd = "SELECT * FROM STUDENTINFO;"; Cursor result = db.rawQuery(cmd, null); result.moveToNext(); // 將結果以AlertDialog的方式輸出 for( int i = 0; i < result.getColumnCount() ;i++ ) { String data = result.getString(i); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage(data); builder.show(); } } }
SQLite Development The most used Cursor are listed below:
SQLite Development The most used Cursor are listed below: