We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Reading data from TXT files
Page Index Toggle Pages: 1
Reading data from TXT files (Read 1467 times)
Reading data from TXT files
Jun 5th, 2008, 7:58am
 
The Project im working on is about the visualisating of SMS Text messages. All the information is saved in TXT files. Each SMS is a textfile wich contains all the information needed.

I made some tests with the method explained in the "LoadFile 1 tutorial" in the Learning Section. It works pretty good. But there i need all the information in one large file, every sms information in one line, seperated by a seperator.

Are there different ways to get the specific part of the file then "split(lines[index], '\t');" ?

For example this is how a file looks like:

Quote:
From: 01746655485
To:
Subject:   HELLO WORLD! THIS IS THE TEXT...
2007.11.24 16:21:19:00
2007.11.24 16:27:55:00
Size: 18 bytes


So is there a way to say, get data after "FROM:"
or get data between " "Size:" and "bytes" ?

Like i said i could find any references or information on how to get data out of files expect the "loadfile 1" example.

If there is no way, is there a way or software to konvert all the data out of the files into one single file to one line per File and seperate them the way i want. Or how do you edit data to work the way you need it.

Thank you for helping!
Re: Reading data from TXT files
Reply #1 - Jun 5th, 2008, 10:32am
 
Sure, you can do it easily using String functions, especially substring().

Have a look to the java doc for more info :
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#substring(int,%20i...

Say you have this string :
Size: 18 bytes
and you want to extract the number.

Your string has this structure :
[6 characters for 'Size: '] - the number you need to extract - [6 more characters for ' bytes'].

Code:
println(s.substring(6, s.length() - 6)); 



You can also use regex (regular expressions) - which is more powerful but not so easy to understand.
Re: Reading data from TXT files
Reply #2 - Jun 5th, 2008, 1:42pm
 
loadString() loads the whole file at once and splits it by lines. It is OK if the file isn't huge (below memory allocated to Java...), otherwise you might need to use Java's line per line reading.
Say loadString is what you need.

You make a loop to scan all the lines in the returned array.
You can take the first n character of the line (using substring as shown) and compare to known prefixes (From:, To:, Subject:, etc.).
You can also split the line on ' ' instead of '\t' and check the first returned string. Slightly less efficient but simpler.
Re: Reading data from TXT files
Reply #3 - Jun 7th, 2008, 2:13am
 
Ok, i made some tests so far and reading the file is no problem. For example,

println(sms);  // Prints "Size: 18 bytes " to the console
but as soon as i try to generate a substring with

sms.substring(6, sms.length() - 6)
or something similar i get this error message :

Quote:
Semantic Error: No accessible method with signature "substring(int)" was found in type "java.lang.String[]"..


and that only happens if i get the string out of a file.
if i write into the code:

String sms = "Size: 16 bytes";

getting the substring isnt a problem and i get the right output if i use. "sms.substring(6, sms.length() - 6)"
i guess its a common problem, but i just cant figure out whats wrong.

Thanks


Re: Reading data from TXT files
Reply #4 - Jun 7th, 2008, 10:09am
 
Your sms object is an array of strings, not just a single string so you need to specify which line of the sms array you're trying to work on.

e.g.
Code:
String size=sms[0].substring(6,sms[0].length-6);
Re: Reading data from TXT files
Reply #5 - Jun 7th, 2008, 11:25am
 
Oh ok,  i forgot about that, because there is just online line. Thank you!
Re: Reading data from TXT files
Reply #6 - Jun 7th, 2008, 4:23pm
 
sometimes it's also better to use indexOf() and match() too, depending on what you're trying to do.

for lines that begin with something followed by a colon (an attribute followed by a value), you can use:

Code:
int colon = lines[i].indexOf(':');
String attr = lines[i].substring(0, colon);
String value = lines[i].substring(colon + 1);


then to use the information:
Code:
if (attr.equals("From")) {
// the 'value' is the from line
}


this way you don't have to hardwire things for number of letters and all that.

or to get into trickier matching methods, you can use the match() function:
http://processing.org/reference/match_.html

this breaks the line into a two piece String array:
Code:
String line = "From: Jim Bob Jones <jim.bob@the-hills.net>";
String[] pieces = match(line, "(.*):(.*)");
println(pieces);

(more about the syntax for match is in the reference)
Page Index Toggle Pages: 1