J2ME逐行读取文本文件

J2ME逐行读取文本文件

 

http://www.j2megame.com/html/xwzx/ty/421.html

 

Reading a text file line by line。

在J2ME中没有提供类似J2EE中 BufferedReader 的函数去逐行读取文本,以下代码增加了这个功能。

ID CS001006   
Platform S60 3rd Edition, FP1 Tested on devices Nokia N95 8GB
Category Java ME Subcategory Files/Data

 

Keywords (APIs, classes, methods, functions) : java.io.InputStreamReader, java.lang.StringBuffer, java.io.IOException, java.lang.Class, java.io.InputStreamReader.read(), java.lang.StringBuffer.append(), java.lang.Class.getResourceAsStream(), java.io.InputStreamReader.close()

Source

import

java.io.IOException
;


import
java.io.InputStreamReader
;


import
javax.microedition.lcdui.Command
;


import
javax.microedition.lcdui.CommandListener
;


import
javax.microedition.lcdui.Display
;


import
javax.microedition.lcdui.Displayable
;


import
javax.microedition.lcdui.Form
;


import
javax.microedition.midlet.MIDlet
;
public

class
ReaderMIDlet extends
MIDlet implements
CommandListener {


private
Form mainForm;


private
Command exitCommand;


 

/**

* Constructor. Constructs the object and initializes displayables.

*/



public
ReaderMIDlet(
)
{


mainForm =
new
Form(
"Example MIDlet"
)
;


 

exitCommand =
new
Command(
"Exit"
, Command.EXIT
, 1)
;


mainForm.addCommand
(
exitCommand)
;


mainForm.setCommandListener
(
this
)
;


 

try
{


readFile(
"text.txt"
)
;


}
catch
(
IOException

ex)
{


// TODO: Exception handling


}


}


 

/**

* Outputs the specified file onto the form.

* @throws java.io.IOException if an exception occurs when reading the

* file

*/



private
void
readFile(
String

filename)
throws
IOException

{


InputStreamReader

reader =
new
InputStreamReader

(


getClass(
)
.getResourceAsStream
(
filename)
)
;


String

line =
null
;


// Read a single line from the file. null represents the EOF.


while
(
(
line =
readLine(
reader)
)
!=
null
)
{


// Append the read line to the main form with a linefeed ('/n')


mainForm.append
(
line +
"/n
"

)
;


}


reader.close
(
)
;


}


 

/**

* Reads a single line using the specified reader.

* @throws java.io.IOException if an exception occurs when reading the

* line

*/



private
String

readLine(
InputStreamReader

reader)
throws
IOException

{


// Test whether the end of file has been reached. If so, return null.


int
readChar =
reader.read
(
)
;


if
(
readChar ==
-
1)
{


return
null
;


}


StringBuffer

string =
new
StringBuffer

(
""
)
;


// Read until end of file or new line


while
(
readChar !=
-
1
&&
readChar !=
'/n
'

)
{


// Append the read character to the string. Some operating systems


// such as Microsoft Windows prepend newline character ('/n') with


// carriage return ('/r'). This is part of the newline character


// and therefore an exception that should not be appended to the


// string.


if
(
readChar !=
'/r
'

)
{


string.append
(
(
char
)
readChar)
;


}


// Read the next character


readChar =
reader.read
(
)
;


}


return
string.toString
(
)
;


}


 

/**

* From MIDlet.

* Called when the MIDlet is started.

*/



public
void
startApp(
)
{


// The initial display is the first form


Display.getDisplay
(
this
)
.setCurrent
(
mainForm)
;


}


 

/**

* From MIDlet.

* Called to signal the MIDlet to enter the Paused state.

*/



public
void
pauseApp(
)
{


// No implementation required


}


 

/**

* From MIDlet.

* Called to signal the MIDlet to terminate.

* @param unconditional whether the MIDlet has to be unconditionally

* terminated

*/



public
void
destroyApp(
boolean
unconditional)
{


// No implementation required


}


 

/**

* From CommandListener.

* Called by the system to indicate that a command has been invoked on a

* particular displayable.

* @param command the command that was invoked

* @param displayable the displayable where the command was invoked

*/



public
void
commandAction(
Command command, Displayable displayable)
{


if
(
command ==
exitCommand)
{


// Exit the MIDlet


notifyDestroyed(
)
;


}


}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

蜡台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值