1 / 35

Chapter 8

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.

odin
Download Presentation

Chapter 8

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 8 Saving and Reading Files

  2. Java I/O

  3. Write a Text File • FileWriter • FileWriter寫入單位為char。產生物件方式如下: • 在FileWriter物件參數當中,第一個為檔案名稱,第二個為寫入模式是否為append • BufferedWriter • 使用Buffer機制來做write()時,會先將要寫入之檔案暫存起來,等到一定的資料量後才寫入磁碟,因此可省下不少I/O所造成的負擔。

  4. Write a Text File The most applied methods of BufferedWriter

  5. Write a Text File Results of the example are shown below:

  6. 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:

  7. File Stream Output • FileOutputStream • 此種方式是以byte為單位對檔案作存取,故通常在使用這種方式來做檔案讀寫時會一起使用其他Object的OutputStream,目的是將我們要儲存的目標檔案自動以byte的形式作儲存

  8. File Stream Output Results of the example are shown below :

  9. 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 :

  10. Read a Text File • FileReader • FileReader讀取單位為char。產生物件方式如下: • BufferedReader

  11. Read a Text File The most applied methods of BufferedWriter

  12. Read a Text File Results of the example are shown below :

  13. 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:

  14. File Stream Input • FileInputStream • 此種方式如同FileOutputStream是以byte為單位,故此方法通常也用於不同物件的讀取

  15. File Stream Input Results of the example are shown below :

  16. 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:

  17. SQLite

  18. SQLite • Features of SQLite : • 不需要一個額外的系統來運行整個資料庫系統 • 寫入或是讀取資料都是直接連結到檔案中 • 由於都以檔案型式存在,所以可以將此資料隨意在大部分平台下使用 • 支援大部分SQL92的語法 • 運行資料庫操作時所佔用的資源較小

  19. Introduction on SQL Language • CREATE TABLE • Create table用來建立表格,而表格分為列(row)和欄(column),而表格當中的資料可以有不同的資料型態

  20. Introduction on SQL Language • CREATE TABLE Example

  21. Introduction on SQL Language Results of CREATE TABLEExample:

  22. Introduction on SQL Language • ALTER TABLE • Alter table用來更改Table,如新增、刪除、更改欄位屬性或名稱,更改指令如下表: • Using method of Alter table is shown below:

  23. Introduction on SQL Language • DROP TABLE • Drop table指令為刪除一個表格,其用法如下:

  24. Introduction on SQL Language • INSERT • Insert指令用於將資料輸入到表格當中,使用方法如下:

  25. Introduction on SQL Language • SELECT • Select指令用於查詢資料庫當中符合條件的資料,使用方法如下:

  26. Introduction on SQL Language • UPDATE • Update指令可修改資料表當中的值,使用方法如下:

  27. Introduction on SQL Language • DELETE • Delete指令可將符合條件的資料給刪除,使用方法如下:

  28. Introduction on SQL Language 使用adb工具進入模擬器中,接著在模擬器當中使用sqlite3這個工具,使用方法為:

  29. 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 } }

  30. 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); }

  31. 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); } }

  32. 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(); } } }

  33. SQLite Development The most used Cursor are listed below:

  34. SQLite Development The most used Cursor are listed below:

  35. Q&A

More Related