14
ブラックベリーアプリケーションのテキストファイルを読み書きするためのサンプルコードを教えてください。文字列のJ2ME/Blackberry - テキストファイルの読み書き方法?
ブラックベリーアプリケーションのテキストファイルを読み書きするためのサンプルコードを教えてください。文字列のJ2ME/Blackberry - テキストファイルの読み書き方法?
ファイルの書き込み/読み込み:
private String readTextFile(String fName) {
String result = null;
FileConnection fconn = null;
DataInputStream is = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
is = fconn.openDataInputStream();
byte[] data = IOUtilities.streamToBytes(is);
result = new String(data);
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != is)
is.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
return result;
}
private void writeTextFile(String fName, String text) {
DataOutputStream os = null;
FileConnection fconn = null;
try {
fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
if (!fconn.exists())
fconn.create();
os = fconn.openDataOutputStream();
os.write(text.getBytes());
} catch (IOException e) {
System.out.println(e.getMessage());
} finally {
try {
if (null != os)
os.close();
if (null != fconn)
fconn.close();
} catch (IOException e) {
System.out.println(e.getMessage());
}
}
}
私のコードスニペットを使用して
は
これは、ではなくConnector.WRITEのConnector.READ_WRITEにする(使用することをお勧めします私の場合は2番目はうまくいかない)。 – Antonio
@Antonioありがとう! –