BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   Read String in File text. (http://www.blackberryforums.com/developer-forum/255613-read-string-file-text.html)

bankkab 11-21-2011 09:31 AM

Read String in File text.
 
fconn = (FileConnection) Connector.open("file:///D:/IP.txt", Connector.READ);

in "file:///D:/IP.txt" have String "LocalHost"

How to read string and show message "LocalHost" in my app

please,

Dougsg38p 11-21-2011 02:13 PM

Re: Read String in File text.
 
Call FileConnection.openInputStream() to obtain a stream, then read the stream.

berryapp 11-21-2011 09:27 PM

Re: Read String in File text.
 
Like in the earlier reply, once you have the connection, you could get the InputStream. You can then use the IOUtilities class provided by RIM to read the whole content into an array.

InputStream in = fconn.openInputStream();
byte[] data = IOUtilities.streamToBytes( in );

You could then set the data in a StringBuffer,

StringBuffer sb = new StringBuffer(data.length);
sb.append(data);

For small data file this would be an easier way to go.

berryapp 11-22-2011 11:27 PM

Re: Read String in File text.
 
Quote:

Originally Posted by berryapp (Post 1756134)
Like in the earlier reply, once you have the connection, you could get the InputStream. You can then use the IOUtilities class provided by RIM to read the whole content into an array.

InputStream in = fconn.openInputStream();
byte[] data = IOUtilities.streamToBytes( in );

You could then set the data in a String,

String = new String(data.); // need to use String

For small data file this would be an easier way to go.

String = new String(data.); // need to use String

bankkab 11-23-2011 12:48 AM

Re: Read String in File text.
 
Quote:

Originally Posted by berryapp (Post 1756313)
String = new String(data.); // need to use String

try {
fconn = (FileConnection) Connector.open("file:///D:/IP.txt", Connector.READ);
//is = fconn.openDataInputStream();
// data = IOUtilities.streamToBytes(is);
InputStream in = fconn.openInputStream();
byte[] data = IOUtilities.streamToBytes(in);
String sss = new String(data);
oxi = new LabelField(sss);
}



catch (IOException e)
{
System.out.println(e.getMessage());
}
add(oxi);


Build Complete but....In my App is Error !

Helpme please

berryapp 11-23-2011 12:18 PM

Re: Read String in File text.
 
Quote:

Originally Posted by bankkab (Post 1756320)
try {
fconn = (FileConnection) Connector.open("file:///D:/IP.txt", Connector.READ);
//is = fconn.openDataInputStream();
// data = IOUtilities.streamToBytes(is);
InputStream in = fconn.openInputStream();
byte[] data = IOUtilities.streamToBytes(in);
String sss = new String(data);
oxi = new LabelField(sss);
}



catch (IOException e)
{
System.out.println(e.getMessage());
}
add(oxi);


Build Complete but....In my App is Error !

Helpme please

You need to provide little bit more information. I believe you are saying that you can build fine, but you get error while running the app? What error message you exactly see. You could run your app in debug mode and add break point at certain line based on the error that you see.

bankkab 11-24-2011 08:52 AM

Re: Read String in File text.
 
it shows debug

source code is not available
source code is not available
source code is not available
source code is not available
source code is not available

File system error (1003)

when i click in my app

berryapp 11-24-2011 09:55 AM

Re: Read String in File text.
 
Quote:

Originally Posted by bankkab (Post 1756639)
it shows debug

source code is not available
source code is not available
source code is not available
source code is not available
source code is not available

File system error (1003)

when i click in my app

Did you put break point at some lines within the class file that you created? The debugger should be able to find the source from your class:-o

bankkab 11-24-2011 10:51 AM

Re: Read String in File text.
 
Can you Give me a sample code for

read string in my Text file please :cry:

berryapp 11-24-2011 07:33 PM

Re: Read String in File text.
 
Quote:

Originally Posted by bankkab (Post 1756660)
Can you Give me a sample code for

read string in my Text file please :cry:

Here is a method. After you start the simulator, first save a file temp.txt making sure that there is a file by that name and appropriate location.
---------------------------------------------------------
public String readFile() {
FileConnection fc = null;
String fileContent = "";
try {
fc = (FileConnection)Connector.open("file:///store/home/user/temp.txt");
if ( (fc==null) || !fc.exists()){
return "File does not exist";
}

InputStream in = fc.openInputStream();
byte[] data = IOUtilities.streamToBytes( in );
fileContent = new String(data);

in.close();
fc.close();
} catch (Throwable t) {

}
return fileContent;
}

bankkab 11-25-2011 12:35 AM

Re: Read String in File text.
 
here this code

class Connect extends MainScreen {



Connect() {


FileConnection fc = null;
String fileContent = "";
try {
fc = (FileConnection)Connector.open("file:///D:/IP.txt");
if ( (fc==null) || !fc.exists()){
return "File does not exist";
}

InputStream in = fc.openInputStream();
byte[] data = IOUtilities.streamToBytes(in);
fileContent = new String(data);

in.close();
fc.close();
} catch (Throwable t) {

}
return fileContent;
}

}

it error " cannot return a value from method whose result type is void "

where is a void i don't declare a void function

berryapp 11-25-2011 09:28 AM

Re: Read String in File text.
 
Quote:

Originally Posted by bankkab (Post 1756757)
here this code

class Connect extends MainScreen {



Connect() {


FileConnection fc = null;
String fileContent = "";
try {
fc = (FileConnection)Connector.open("file:///D:/IP.txt");
if ( (fc==null) || !fc.exists()){
return "File does not exist";
}

InputStream in = fc.openInputStream();
byte[] data = IOUtilities.streamToBytes(in);
fileContent = new String(data);

in.close();
fc.close();
} catch (Throwable t) {

}
return fileContent;
}

}

it error " cannot return a value from method whose result type is void "

where is a void i don't declare a void function

Since your method returns String, so you need to have the method declaration accordingly, i.e.,
String Connect() { //instead of Connect() {

hrbuckley 11-25-2011 10:21 AM

Re: Read String in File text.
 
Well, actually, constructors can't return a value at all.

berryapp 11-25-2011 11:33 AM

Re: Read String in File text.
 
Quote:

Originally Posted by hrbuckley (Post 1756802)
Well, actually, constructors can't return a value at all.

:oops: sorry! I didn't notice that. My original suggested method (public String readFile()) was not a constructor, so that was in my mind while I was typing.

berryapp 11-25-2011 11:37 AM

Re: Read String in File text.
 
Quote:

Originally Posted by berryapp (Post 1756793)
Since your method returns String, so you need to have the method declaration accordingly, i.e.,
String Connect() { //instead of Connect() {

Since you placed the code within a constructor, remove the return statement.

bankkab 11-27-2011 11:37 PM

Re: Read String in File text.
 
In netbean i'm use Class Scanner for read string

but In Blackberry Jde not have class Scanner

berryapp 12-07-2011 04:36 PM

Re: Read String in File text.
 
Quote:

Originally Posted by bankkab (Post 1757098)
In netbean i'm use Class Scanner for read string

but In Blackberry Jde not have class Scanner

Please note that, you can't use many of the APIs that are available in the later version of Java. I think anything after jdk 1.3 is a NO NO!


All times are GMT -5. The time now is 01:05 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.