#### Archived

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

# Reading some text from a file.

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

## 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 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);

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 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?

shmoove

##### 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.

15,13,5,6,23,45,3,2,34,5,32,3,5,32,67,8,5,2,1,6,7

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.

Thanks
B

##### 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.

shmoove

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

##### 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,
Brian

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5

• 14
• 9
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632623
• Total Posts
3007503
• ### Who's Online (See full list)

There are no registered users currently online

×