PDA

View Full Version : Lettura caratteri in file txt [JAVA]


F@lkland§
01-03-2012, 09:06
Ciao Ragazzi,
sono un newbie in java.
Se ho un file di testo come il seguente:


{
"id": "387539086359_14841350",
"from": {
"name": "Terri Bailey",
"id": "742648297"
},
"message": "\u003Cdiv id=\"fb-root\">\u003C/div>\u003Cscript src=\"http://connect.facebook.net/en_US/all.js#appId=APP_ID&xfbml=1\">\u003C/script>\u003Cfb:comments xid=\"Ms.Sew What\" numposts=\"10\" width=\"425\" publish_feed=\"true\">\u003C/fb:comments>",
"created_time": "2010-11-24T01:59:57+0000"
},


come posso estrapolare i campi "id" e "message"?
ho provato in questo modo ma la lunghezza dei caratteri dei campi ID e MESSAGE puņ essere variabile purtroppo.


try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream("C:/Users/Riccardo Cecere/Desktop/comments");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
int srcBegin=20;
int srcEnd=40;
char[] dst=new char[20];
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
//System.out.println (strLine);


if (strLine.contains("id")){
System.out.println("Lunghezza stringa: "+strLine.length());
for (int j = 0; j < strLine.length(); j++) {
strLine.getChars(srcBegin, srcEnd, dst, 0);
}
System.out.println("Id letto: "+dst.toString()+"\n");
}


Grazie mille a tutti!

!fazz
01-03-2012, 09:31
leggi a riga, fai uno split sul carattere : scorri il vettore e prendi l'elemento successivo a quello che contiene "id" ecc ecc

F@lkland§
01-03-2012, 10:29
forse mi viene meglio con l'espressioni regolari...

gokan
01-03-2012, 12:36
Se non sbaglio il file che hai postato č in formato JSON..piuttosto che leggerlo come stringone, potresti usare una libreria molto semplice come gson di google.