cancel
Showing results for 
Search instead for 
Did you mean: 

Read file from server ?

Former Member
0 Kudos

Hi @,

I have a requirement where I need to read some CSV files from the server and then format it and pass it as output. How can we read the file from the server as in Upload UI only client side upload is possible .

Also I need to maintain config file kind of thing from where the details abt the location will be read and then files will be read from this particular location?

Regards

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

You can read file on the server using following code:

try {
		// You need to provide file location to File Object	
		File testFile = new File("/usr/tmp/schedular.log"); 	//for Linux/UNIX
		//File testFile = new File("d:/schedular.log"); 	//for windows	
					
                //This will write content into the file
		FileOutputStream out = new FileOutputStream(file);
		out.println("Writing into the file.");		
		out.close();

                //This will read content from the file
		FileInputStream in = new FileInputStream(file);
                int i = in.read();
                  
                while (i != -1)  // -1 means end of file
                {
                	System.out.print((char) i);
		        i = in.read();  //This will read next character
                } 
		in.close();


	} catch (IOException ioe) 
	{
		System.err.print("Caught an IOException: \n" + ioe.getMessage());
	} catch (Exception e) 
	{
		System.err.print("Caught an Exception: \n" + e.getMessage());
	}	

Now for config files you could use Properties Class. This is a special class contains key value pairs. Their extension could be any thing (i guess) but .properties is used to distinguish them as config files.

Below is the format of properties files, these are pain text files with key=value pairs, like

jco.client.client=100
jco.client.user=devuser
jco.client.passwd=abc123
jco.client.ashost=DEV
jco.client.sysnr=00

You could read these properties into Properties Class object like this:


	Properties props = new Properties();

	InputStream is = <YourClass>.class.getResourceAsStream("/logon.properties");
						
	if (is != null)
		props.load(is);
	else
		throw new IOException("Could not load logon properties.");

<YourClass> should be replaced with the Class Name in which you are using this code. Now comes the tricky part where to store logon.properties file? You must store this file in your class path otherwise you won't get it loaded.

Regards,

Jawed Ali

Former Member
0 Kudos

Hi Jawed,

Thanks for yr detailed response I have to store this file in which folder so that its in deployed jar.

Also will this code work for XML also as I need to read XML file.

Regards

Former Member
0 Kudos

In the web dynpro perspective switch to navigation tab and under your project hirarchy put your logon.properties file on following location:

src>mimes>PORTAL-INF>classes

Hopefully it will load your properties files.

Now reading XML files, I have given you the basic file reading code. There are many other classes you can use for reading files line by line etc. Even you could use XML parsing API(already available in Portal) for parsing XML files.

Regards,

Jawed Ali

Former Member
0 Kudos

Hi Jawed,

Thanks for Yr reply Can I directly save any file there ?

Also do I have to make any classpath changes showing the file as I am getting error after this exercise and not able to read the file .

Regards,

Former Member
0 Kudos

At least give some detail description of the error; I can't figure out what error you are getting?

Lets make it simple, first create a file on your server.

Linux: /usr/tmp/testRead.txt

windows: C:\testRead.txt

put some text in it and use my file read code and display it in your dynpro application. Once its all ok then try to write something in that file and check it manually if it has been written or not.

Once this exercise is complete success fully I will then guide you how make your file path dynamic?

Former Member
0 Kudos

Hi Jawed,

I am able to read the xml file and convert it correctly as per the need the problem I am facing is in the properties file which I am not able to raed fromt he desired location So thats my problem how to read it my application and where to store it so It it can be modified later without changes in the application.

Regards

Former Member
0 Kudos

As I said previously in the web dynpro perspective switch to navigation tab and under your project hierarchy put your logon.properties file on following location:

src

|_mimes

|_PORTAL-INF

|_classes

if you don't find PORTAL-INF create them. Now whether you use the property reading code in your view or component use its name to replace <YourClass> in following code:

	Properties props = new Properties();
 
	InputStream is = <YourClass>.class.getResourceAsStream("/logon.properties");
						
	if (is != null)
		props.load(is);
	else
		throw new IOException("Could not load logon properties.");

I hope this will help you solve the problem. Actually I don't have any server available to test the code i have written give me your email address I will mail it to you.

Answers (1)

Answers (1)

Former Member
0 Kudos

Please go through this link

Regards

Ruturaj