search data from FileReader in Java
- by maya
hi I'm new in java
how to read and search data from file (txt) and then display the data in TextArea or Jtable.
for example I have file txt contains data and I need to display this data in textarea after I clicked a button, I have used FileReader , and t1 t2 tp are attributes in the file
import java.io.FileReader;
import java.io.IOException;
String t1,t2,tp;
Ffile f1= new Ffile();
FileReader fin = new FileReader("test2.txt");
Scanner src = new Scanner(fin);
while (src.hasNext()) {
t1 = src.next();
textarea.setText(t1);
t2 = src.next();
textarea.setText(t2);
tp = src.next();
textarea.setText(tp);
f1.insert(t1,t2,tp);
}
fin.close();
also I have used the inputstream
DataInputStream dis = null;
String dbRecord = null;
try {
File f = new File("text2.text");
FileInputStream fis = new FileInputStream(f);
BufferedInputStream bis = new BufferedInputStream(fis);
dis = new DataInputStream
while ( (dbRecord = dis.readLine()) != null) {
StringTokenizer st = new StringTokenizer(dbRecord, ":");
String t1 = st.nextToken();
String t2 = st.nextToken();
String tp = st.nextToken();
textarea.setText(textarea.getText()+t1);
textarea.setText(textarea.getText()+t2);
textarea.setText(textarea.getText()+tp);
}
} catch (IOException e) {
// catch io errors from FileInputStream or readLine()
System.out.println("Uh oh, got an IOException error: " + e.getMessage());
} finally {
}
but both of them don't work ,so please any one help me
I want to know how to read data and also search it from file and i need to display the data in textarea .
thanks in advance