Created
August 31, 2011 01:08
-
-
Save h-sao/1182570 to your computer and use it in GitHub Desktop.
[iアプリ:Star1.3]スクラッチパッドへの読み書き
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// "MyScr.java" | |
// Docomo iappli Star-1.3 | |
// スクラッチパッド利用サンプル | |
// Last Change:01-Sep-2011. | |
// written by Sao Haruka | |
//==============================================================// | |
// パッケージ名 | |
//==============================================================// | |
package SaoProj; // 自分のプロジェクト名 | |
//==============================================================// | |
// inport | |
//==============================================================// | |
// スクラッチパッドの利用 | |
import javax.microedition.io.*; | |
import java.io.*; | |
//==============================================================// | |
// Programing | |
//==============================================================// | |
// | |
// スクラッチパッドからIDデータ取得 | |
// | |
//==============================================================// | |
public class MyScr | |
{ | |
// public typedef, enum ------------------------------------ | |
// public メンバ ------------------------------------------- | |
// public 関数 --------------------------------------------- | |
// コンストラクタ | |
MyScr(){ | |
m_box = new byte[ 0x20 ]; // 0x20byte使っている予定 | |
m_id = ""; | |
} // MyScr() | |
// protected typedef, enum ---------------------------------- | |
// protected メンバ ----------------------------------------- | |
protected int m_len; // IDの長さ | |
protected String m_id; // ID | |
// protected 関数 ------------------------------------------- | |
//==============================================================// | |
// 初期化処理 | |
//==============================================================// | |
protected void init(){ | |
// ------------------------------------ | |
// スクラッチパッドからの読み込み | |
// このサンプルでは | |
// [IDの長さ] [ID(文字列)] | |
// という書き込み内容にしている | |
// ------------------------------------ | |
int i; | |
InputStream in; | |
try{ | |
// 読み取り用に、ファイルをオープン | |
// ":pos=0" が読み取りオフセット | |
in = Connector.openDataInputStream("scratchpad:///0;pos=0"); | |
m_len = in.read(); // 文字列の長さを取得 | |
for( i = 0; i < m_len; i ++ ){ | |
m_box[ i ] = (byte)( in.read()); // 実際のIDデータコピー | |
} | |
m_id = new String( m_box, 0, m_len ); // ID取得 | |
}catch( Exception e ){ //エラーが発生した場合の処理 | |
System.out.println("Err="+e); | |
}finally{ | |
// finally処理追加! | |
try{ | |
if( in != null ){ | |
in.close(); | |
} | |
}catch( Exception e ){ | |
System.out.println("Err="+e); | |
} | |
} | |
} | |
//==============================================================// | |
// ID情報をスクラッチパッドに書き込む | |
//==============================================================// | |
protected void id_set( String text ){ | |
m_id = "" + text; // 入力された文字をinputTextにセット | |
m_len = text.length(); | |
OutputStream out = null; | |
// -------------------------------- | |
// スクラッチパッドへの書き込み | |
// -------------------------------- | |
try{ | |
int i; | |
byte tmp[] = new byte[ m_len ]; | |
tmp = m_id.getBytes(); // 配列に格納 | |
//書き込みり用に、ファイルをオープン | |
out = Connector.openOutputStream("scratchpad:///0;pos=0"); | |
out.write( m_len ); // IDの文字列の長さを書き込み | |
for( i = 0; i < m_len; i ++ ){ | |
out.write( tmp[i] ); // IDデータの書き込み | |
} | |
out.flush(); // 終了前にflush()を実行 | |
}catch( Exception e ){ // エラーが発生した場合の処理 | |
System.out.println("Err="+e); | |
}finally{ | |
// finally処理追加! | |
try{ | |
if( out != null ){ | |
out.close(); | |
} | |
}catch( Exception e ){ | |
System.out.println("Err="+e); | |
} | |
} | |
} | |
// private typedef, enum ------------------------------------ | |
// private メンバ ------------------------------------------- | |
// private 関数 --------------------------------------------- | |
private byte m_box[]; // 読み込んだコードのバイト列を示す | |
} | |
//==============================================================// | |
// End of File |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment