cancel
Showing results for 
Search instead for 
Did you mean: 

reading text file content

Former Member
0 Kudos

Hi,all

How can I read text file content to be able to search within the file?

Regards,

Michael

Accepted Solutions (0)

Answers (2)

Answers (2)

former_member751941
Active Contributor
0 Kudos

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

Former Member
0 Kudos

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

Former Member
0 Kudos

Michale,

Check this blog on working with Fileupload control.

/people/anilkumar.vippagunta2/blog/2007/02/04/office-control-in-webdynpro-ii

Anilkumar

Former Member
0 Kudos

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

Former Member
0 Kudos

Hi,all

I still can't give the file.There are no exceptions,

just blank.I'm not giving the file content.

Regards,

Michael

null

Former Member
0 Kudos

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.

vijayakhanna_raman
Active Contributor
0 Kudos

Hi,

Use resource bundle for getting the values.

Regards,

Vijai

Former Member
0 Kudos

Hi,Vijai

What I need to do is to read text file content during uploading this file

to KM or after that ,and to insert it's content to specific column in table

in my DataBase.I need to have an option of finding uploaded file content.

Regards,

Michael