i have a text file that is as such:
"name email gender location"
joe,joe@g.com,male,london
fred,fred@g.com,male,new york
I am trying to read this data into a html table using jsp/java. At the moment i can read them all into the table but the whole line appears in one cell under the headings. So at the moment they will all appear under name. how would i split each element of the comma delimited text file so they appear under the correct heading.
NameEmailGenderLocationList list =new ArrayList();
FileInputStream in = new FileInputStream("people.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine ="", tmp;
while ((tmp = br.readLine()) != null){
strLine =tmp+"\n"+strLine;
list.add(tmp);
}
解决方案
Use String.split():
String[] parts = tmp.split(",");
If tmp contained "joe,joe@g.com,male,london" then:
parts[0] = "joe"
parts[1] = "joe@g.com"
parts[2] = "male"
parts[3] = "london"
I am unfamiliar with jsp, but if the objective of the while is to transform each line read from br into a HTML
... and append to list then:while ((tmp = br.readLine()) != null)
{
String[] parts = tmp.split(",");
if (4 == parts.length) // Not sure what validation is required, if any.
{
StringBuilder tr = new StringBuilder("
");for (String s: parts)
{
tr.append("
").append(s)
.append("
");}
tr.append("
")list.add(tr.toString());
}
}