cancel
Showing results for 
Search instead for 
Did you mean: 

How to read text file from Client?

Former Member
0 Kudos

Hi Friends

I have a requirement where in I have to read the contents of a text file which is placed at the user's desktop (Client System).

I am not able to find out, how to do that.

I am using an UploadFile UI control and trying the following code,

File myFile = new File(element.getFileResource());
String file_name = myFile.getAbsolutePath();
BufferedReader reader = new BufferedReader(new FileReader(file_name));
String line = bufferedReader.readLine()

But the above code throws an exception that

<b><i>"The file specified by path <---path selected by uploadFile control-> could not be found"</i></b>

because it tries to find the file in the application server where the application is deployed,

But I want to read the file from my Client System.

Any help/idea would be highly appreciated.

Thanks

Deepak Gupta

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

See this <a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/00062266-3aa9-2910-d485-f1088c3a4d71">tutorial</a>.

Armin

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Deepak,

Did you fing any solution for this or any other alternative? If yes, can you share your thought.... I am also in the same situation where I have to read text file from client machine...

Can somebody help? In my case, the text file will have client data and the appplication will behave depending on the content of the text file...

Thanks,

Dileep

Former Member
0 Kudos

Dileep,

No, I did not find the solution,

But there is one workaround I have found,

that is "To read a file from Client"

First load that file to the application server at any location

and then read the new file

this is how I am doing, if somebody else has some other better idea,

please share....

Regards

Deepak

Former Member
0 Kudos

Hi,

I found the solution as how to read file from the client.

<b>[A]use a fileUpload UI element</b>

create 2 context attributes

1. file_data (type: binary)

2. file_name (type: string)

<b><b> in wdDoInit() method write the following code</b>


//Set the type of FileData as BinaryModifiable so that the content of the file can be read in 'Binary' Mode
	    IWDAttributeInfo l_info = wdContext.getNodeInfo().getAttribute(wdContext.currentContextElement().FILE_DATA);
		IWDModifiableBinaryType l_type = (IWDModifiableBinaryType)l_info.getModifiableSimpleType();

<b>[C] write the following code on the upload event (on click of some button)</b>


 String line_of_file = null;
 byte[] file_data_in_bytes = null;
//Get the selected file's contents in a byte array. returns null if no file is selected.	  	
file_data_in_bytes = wdContext.currentContextElement().getCtx_va_FileData();
if(file_data_in_bytes != null)
{
	//create Input stream for the byte array of file data
	ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(l_byt_data);
    	//Convert the InputStream to a reader
    	Reader reader = new InputStreamReader(byteArrayInputStream);
	//Assign the reader to a buffered reader so that data can be read line by line    	  	
    	BufferedReader bufferedReader = new BufferedReader(reader);
	//Read the data line by line
	while((line_of_file = bufferedReader.readLine()) == null)
	{
		//line_of_file gives you data in text mode
		//you can also use other methods to read data in binary mode
	}
}

former_member751941
Active Contributor
0 Kudos

Hi Deepak,

Check this code,

import java.io.*;

class ReadTextFile

{

public static void main(String args[])

{

try {

String filePath="C:/temp/data.txt";

StringBuffer contents = new StringBuffer();

BufferedReader input = new BufferedReader( new FileReader(filePath) );

String line = null;

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

Thanks for the reply...

<b>Armin</b>

The link you have given is for NW04s, but I am wrking on 04,

Please tell me if there is workaround or tutorial is available for NW04

<b>Mithu,</b>

This code is same what I am using as of now,

I want to read the file from the Client machine. where the browser is running the application,

But It tries to find out the file to be read in the application server where it is deployed,

though I select the file path from the client machine using FIleUpload UI Control.

I hope my query/problem is clear.

Let me know if you have any question on this.

Regards

Deepak