on 05-28-2007 7:55 AM
Hi,all
How can I read text file content to be able to search within the file?
Regards,
Michael
Hi Michael,
Code to read the text file.
required package import java.io.*;
code
-
try {
//use buffering, reading one line at a time
String filePath="C:/temp/data.txt";
StringBuffer contents = new StringBuffer();
BufferedReader input = new BufferedReader( new FileReader(filePath) );
String line = null; //not declared within while loop
/*
readLine is a bit quirky :
it returns the content of a line MINUS the newline.
it returns null only for the END of the stream.
it returns an empty String if two newlines appear in a row.
*/
while (( line = input.readLine()) != null){
contents.append(line);
System.out.println("Line : "+line);
contents.append(System.getProperty("line.separator"));
}
}
catch (FileNotFoundException ex) {
ex.printStackTrace();
}
catch (IOException ex){
ex.printStackTrace();
}
Regards,
Mithu
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,Mithu
I tried your solution but without success.I don't achieve the file,may be
something wrong with filePath?
I tried C:
Temp
<filename> and
C:/temp/<filename>
In principle I need to give it from FileUpload element
(when user choose a file for upload)
by wdcontext.currrentContextElement().getFileName()
where fileName is context attribute of type string,I use it for
my file upload.
Regards,
Michael
Hai,
use this code to search
BufferedReader in = new BufferedReader(new FileReader(filename));
Set data = new HashSet();
String line = in.readLine();
while (line != null) {
data.add(line);
line = in.readLine();
}
if(data.contains(word)){
wdComponentAPI.getMessageManager().reportSuccess("found);
}
Regards,
Naga
Hi,
If you are looking at reading the text file content that has been uploaded thorugh the FileUpload UI element, then you can make use of the "resource" property of the FileUpload UI element. Let's say you have bound the context value attribute "FileResource". Try this:
try{
IWDResource res = wdContext.currentContextElement().getFileResource();
InputStream fileStream = res.read(false));
}
catch(Exception e){
wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(),false);
}
You can then use this "fileStream" read the contents.
Regards,
Satyajit.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
88 | |
23 | |
11 | |
9 | |
8 | |
5 | |
5 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.