Jump to content
  • Advertisement


This topic is now archived and is closed to further replies.

Woody FX

Reading some text from a file.

This topic is 5276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

have been trying to read numbers from a txt file. But i cannot even get the first number at the moment. What i am doing is this
        int d;
	InputStream input_stream = null;
	StreamConnection conn = null;
        conn = (StreamConnection)Connector.open("file:/test.txt");
	input_stream = conn.openInputStream();
	d = input_stream.read();
d is always 0 when i print it out. In should be 5 as thats the first value in the text file. I tried another way also using
InputStream input_stream=getClass().getResourceAsStream(resource_filename);
DataInputStream	data_input = new DataInputStream(input_stream);
dataint_value = data.readInt ();
but that is not for reading in text from a .txt file i think but just tried it out of despiration. Can anybody point out the proper way to do this? Thanks B [edited by - Woody FX on June 7, 2004 1:53:27 PM]

Share this post

Link to post
Share on other sites
""file://test.txt"" and not "file:/test.txt".
And then of course, the emulator/phone has to support the file:// protocol (I haven''t heard of a phone that does). But from the second code example I gather the file is packaged in the jar? If so then you get the InputStream with getResourceAsStream() and not Connector open.

Another thing: if you''re reading from a text file then you''ll be reading the character encodings of the contents, so you''ll have to convert them to the values you want.

So in short:
Where is the file? On the phone''s filesystem or in the jar?
How is the text file saved? ASCII, UTF, some other encoding?
What is the data in the text file? Is it really text, or numbers?


Share this post

Link to post
Share on other sites
Hey Shmoove

The file will be in the Jar.
It will contain only numbers and commas.
The file is saved as the standard format that notepad uses so i i believe it is ASCII

Well only numbers is not correct.

The contents of the file look like this.


So i need to read in char by char and check to see if it is a comma and if not take in the next number ie 1 and then 5 so i have 15 etc.

This is not something i have tackled before, i did something a few years ago in C++ but thats a distant memory.

What are the basic steps ? I should be able to piece it all together to get a fuctioning file reader that i need.


Share this post

Link to post
Share on other sites
OK then.
To open the file you'd use getResourceAsStream.
Then as you read the contents, you have to understand that the bytes you're reading are ASCII values. So you would need to do some parsing in order to get numbers from that. For example:

InputStream input_stream=getClass().getResourceAsStream(resource_filename);
int i;
StringBuffer sb = new StringBuffer();
do {
char c = (char)i;
if (c == ',' || i == -1) {
// we've reached a delimiter (or EOF) so let's see what number we have.

try {
int number_read = Integer.parseInt(sb.toString());
catch (NumberFormatException e) {
System.out.println("Oops! That wasn't a number.");
sb.setLength(0); // clear the buffer

else {
sb.append(c); // add the character we read to the buffer

while (i != -1);

But a much better method would be not to use a text file. Using a binary file and the DataInputStream methods will be much more effective (you wouldn't even need to use delimiters, because you know how many bytes each value takes). To create the binary file you could also use DataInputStream on a small J2SE app.


[edited by - shmoove on June 7, 2004 4:40:27 PM]

Share this post

Link to post
Share on other sites
Shmoove thanks a million,

I will get busy getting this implemented now.

i agree a Binary file would be a better way but this project is small so i would like to get it finished ASAP.

I will look in to changing to a binary file in the future for future projects. I know once i do it once i can recycle if for other projects.

Thanks again,

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!