在J2ME中从文件中读取内容 [英] Reading Content from File in J2ME
问题描述
我试图读取文件的内容,但似乎不工作。我冲浪网,发现不同的实现(read(),read2(),readLine()),但每次运行的代码,他们都给一个NullPointer异常。请问我该如何解决这个问题。
静态字符串文件名;
//实现1
private void readFile(String f){
try {
InputStreamReader reader = new InputStreamReader(getClass()。getResourceAsStream(f));
String line = null; ((line = readLine(reader))!= null){
System.out.println(line);
while
}
reader.close();
} catch(IOException ex){
ex.printStackTrace();
}
}
/ **
*使用指定的阅读器读取一行。
* @throw java.io.IOException如果在读取
*行时发生异常
* /
private String readLine(InputStreamReader reader)throws IOException {
/ /测试文件的结尾是否已经到达。如果是这样,则返回null。
int readChar = reader.read();
if(readChar == -1){
return null;
}
StringBuffer string = new StringBuffer();
//读取直到文件结尾或新行
while(readChar!= -1&& readChar!='\\\
'){
if(readChar!='\\ \\ r'){
string.append((char)readChar);
}
//读取下一个字符
readChar = reader.read();
}
return string.toString();
$ b $ //执行2
private String read(String file)throws IOException {
InputStream is = getClass()。getResourceAsStream(file);
StringBuffer sb = new StringBuffer();
int chars,i = 0; ((chars = is.read())!= -1){
sb.append((char)chars)
;
}
return sb.toString();
//实现3
private String read2(String file)throws IOException {
String content =;
Reader in = new InputStreamReader(this.getClass()。getResourceAsStream(file));
StringBuffer temp = new StringBuffer(1024);
char [] buffer = new char [1024];
int读取; $(read = in.read(buffer,0,buffer.length))!= -1){
temp.append(buffer,0,read);
}
content = temp.toString();
返回内容;
}
public void execute()throws IOException {
folder = System.getProperty(fileconn.dir.photos)+mcast /;
字符串路径=文件夹+文件名+.txt;
FileConnection c =(FileConnection)Connector.open(path,Connector.READ_WRITE);
尝试{
//检查directoy是否存在。如果它不存在,我们创建它。
if(c.exists()){
readFile(path);
// read(path);
// read2(path);
System.out.println(read(path));
} else {
System.out.println(filename +.txt does not exist。Please specify a correct file name);
}
} finally {
c.close();
private String readLine(InputStreamReader reader)抛出IOException {
//测试是否到达文件末尾。如果是这样,则返回null。
int readChar = reader.read();
if(readChar == -1){
return null;
}
StringBuffer string = new StringBuffer();
//读取直到文件结尾或新行
while(readChar!= -1&&read; readChar!='\\\
'){
//将读取的字符追加到字符串。某些操作系统
//例如Microsoft Windows在
//回车符('\r')前加上换行符('\ n')。这是换行符
//的一部分,因此不应将其附加到
//字符串中。
if(readChar!='\r'){
string.append((char)readChar);
}
//读取下一个字符
readChar = reader.read();
}
return string.toString();
$ b
是从二进制包中使用getClass()。getResourceAsStream()从文件中读取文件,并使用FileConnection API从设备的物理内存中读取文件。
您正试图从FileConnection中使用的文件模式创建输入流,因此它将不起作用。因此,为了解决你的问题,你已经取代了 read(...)
, read2(...)
用下面的代码
$ b $ p $ InputStreamReader reader = new InputStreamReader(in); //这里 in
是InputStream类型的输入参数
和 如果您正在测试应用程序在模拟器/设备中 I am trying to read the content of a file but it seems not to work. I surfed the net and found different implementations as shown(read(), read2(), readLine()) but each time a run the codes they all give a NullPointer exception. Please what can I do to rectify this problem.
ISSUE: Referring to file using incorrect method getResourceAsStream(...) is for loading resources from the classpath either from your binary package (.jar) or the classpath directory. So what it essentially means is, to read a file from the binary package use getClass().getResourceAsStream() and to read a file from the device's physical memory use FileConnection API's. You are trying to create the inputstream from the file schema of the type used in FileConnection hence it will not work. So to solve your problem you have replace the inputstream object initialization in the and in . You might also want to consider these if you are testing your application in emulator / device
这篇关于在J2ME中从文件中读取内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! execute(...)
传递文件连接输入流如下
c
是FileConnection类型的对象 b
$ b
System.getProperty(fileconn.dir.photos )
返回
非空值
private String folder;
static String filename;
//IMPLEMENTATION 1
private void readFile(String f) {
try {
InputStreamReader reader = new InputStreamReader(getClass().getResourceAsStream(f));
String line = null;
while ((line = readLine(reader)) != null) {
System.out.println(line);
}
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
/**
* 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') {
if (readChar != '\r') {
string.append((char) readChar);
}
// Read the next character
readChar = reader.read();
}
return string.toString();
}
//IMPLEMENTATION 2
private String read(String file) throws IOException {
InputStream is = getClass().getResourceAsStream(file);
StringBuffer sb = new StringBuffer();
int chars, i = 0;
while ((chars = is.read()) != -1) {
sb.append((char) chars);
}
return sb.toString();
}
//IMPLEMENTATION 3
private String read2(String file) throws IOException {
String content = "";
Reader in = new InputStreamReader(this.getClass().getResourceAsStream(file));
StringBuffer temp = new StringBuffer(1024);
char[] buffer = new char[1024];
int read;
while ((read = in.read(buffer, 0, buffer.length)) != -1) {
temp.append(buffer, 0, read);
}
content = temp.toString();
return content;
}
public void execute() throws IOException {
folder = System.getProperty("fileconn.dir.photos") + "mcast/";
String path = folder + filename + ".txt";
FileConnection c = (FileConnection) Connector.open(path, Connector.READ_WRITE);
try {
// Checking if the directoy exists or not. If it doesn't exist we create it.
if (c.exists()) {
readFile(path);
//read(path);
// read2(path);
System.out.println(read(path));
} else {
System.out.println(filename + ".txt does not exist. Please specify a correct file name");
}
} finally {
c.close();
}
}
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();
}
}
read(...)
, read2(...)
and readFile(...)
with below codeInputStreamReader reader = new InputStreamReader(in);
//here in
is the method's input parameter of type InputStreamexecute(...)
pass the file connection inputstream as belowreadFile(c.openInputStream());
//here c
is object of type FileConnection
System.getProperty("fileconn.dir.photos")
returns
NON NULL value