It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming Java Forum

Problem of writing data in to a file

Problem of writing data in to a file

Postby thimali on Tue Apr 17, 2007 7:30 am

Hi All;

I have a problem of writing data in to a file in the tomcat server.

In that case i have tried with several ways to write but the only way which was successfull as follows.

static public final void WriteFile(String strFile, byte[] pData)
throws IOException {

String filePathe = "C:\\Documents and Settings\\Plateau\\WebRoot\\WEB-INF\\classes\\nsclient.properties";

BufferedOutputStream outStream = new BufferedOutputStream(
new FileOutputStream(filePathe), 32768);
if (pData.length > 0)
{
outStream.write(pData, 0, pData.length);
outStream.close();
}


But i want to get the relative path of the file with out giving the full path of it.For that i have tried with the following code to get file path.

String filePathe = URLDecoder.decode(ClassLoader.getSystemResource(
"test.txt").getPath(), "UTF-8");

But it works only if i run the code as java application(with out deploying and running inside the server)

Once i use this way to write file in the server it doesn't work.
What would be the problem?Any idea?

Thanks,
Thimali
thimali
 
Posts: 3
Joined: Tue Apr 17, 2007 6:56 am

Postby ggomez on Tue Apr 17, 2007 9:52 pm

mmm

if you are using a servlet you can send an instruction thru the method doGet()
and then pass it to another mwthod that will write the data in the file.
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby thimali on Wed Apr 18, 2007 3:43 am

Hi;

thanks for the reply.

But in this case im using jsp and im calling a java method via my jsp page.Here i want to get some text field values from the user and i want to write this value to a file which is in the server.

Any idea to do it in this way?

Thanks.
thimali
 
Posts: 3
Joined: Tue Apr 17, 2007 6:56 am

Postby ggomez on Wed Apr 18, 2007 6:08 am

OOOO ok

thats not so hard

i dont remember how is the coding but html code have a way to put a text field that send the info thru a POST method or a GET method.

then your jsp using the doGET() method can receive that information, then you have the data inside the server memory and you can do whatever you want with that.

if i find some example code i´ll post it

good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Example

Postby ggomez on Wed Apr 18, 2007 6:39 am

This is a good example


Code: Select all
1 <?xml version = "1.0"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
45
<!-- Fig. 10.4: welcome.jsp -->
6 <!-- JSP that processes a "get" request containing data. -->
78
<html xmlns = "http://www.w3.org/1999/xhtml">
9
10 <!-- head section of document -->
11 <head>
12 <title>Processing "get" requests with data</title>
13 </head>
14
Fig. 10.4 Scripting a JavaServer Page—welcome.jsp (part 1 of 3).
602 JavaServer Pages (JSP) Chapter 10
15 <!-- body section of document -->
16 <body>
17 <% // begin scriptlet
18
19 String name = request.getParameter( "firstName" );
20
21 if ( name != null ) {
22
23 %> <%-- end scriptlet to insert fixed template data --%>
24
25 <h1>
26 Hello <%= name %>, <br />
27 Welcome to JavaServer Pages!
28 </h1>
29
30 <% // continue scriptlet
31
32 } // end if
33 else {
34
35 %> <%-- end scriptlet to insert fixed template data --%>
36
37 <form action = "welcome.jsp" method = "get">
38 <p>Type your first name and press Submit</p>
39
40 <p><input type = "text" name = "firstName" />
41 <input type = "submit" value = "Submit" />
42 </p>
43 </form>
44
45 <% // continue scriptlet
46
47 } // end else
48
49 %> <%-- end scriptlet --%>
50 </body>
51
52 </html>
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡

Postby thimali on Thu Apr 19, 2007 3:12 am

Hi ;

thanks a lot for the interest to reply.

I have alredy done as the way u have mentioned.What i want to do is the edited values shoud be wriiten in a .propreties file in my tomcat server.

When im reading the file in the server i had to use a code as follows.

String strFile = "/nsclient.properties";
InputStream inStream = PropertiesFileEditor.class.getClassLoader().getResourceAsStream(strFile);

The real path of this properties file is C:\Program Files\Apache Software Foundation\Tomcat 5.5\webapps\Plateau\WEB-INF\classes\nsclient.properties

this works fine with reading the file.
Is theire any similar way which i can use to wirte in to the file giving the path as String strFile = "/nsclient.properties" with out giving the full path?Because i have to deploy it later as a war file.

Thanks
Thimali
thimali
 
Posts: 3
Joined: Tue Apr 17, 2007 6:56 am

Ok

Postby ggomez on Fri Apr 20, 2007 4:26 am

MMMMMM....

if the file is in the same directory of the jsp, you dont need the fullpath

but if you need it, you can browse your directories using the File class

i did this as an example (sorry the bad variables naming)

Code: Select all
import java.util.*;

public class test {
   
    public static void main(String arg[])
    {
       Properties p= System.getProperties();
       String s=p.getProperty("user.dir");
       System.out.println(s);
        File f=new File(s);
       
        String pa=f.getParent();
           
        System.out.println(pa); //if you want to go backward
       
        File fd=new File(pa);
       
        File[] dir=fd.listFiles(); //get a list of the files and dirs
       
        for(int x=0;x<dir.length;x++)  //look for the dir or file you want
        {
           if(dir[x].getName().equals("your file"))  //found
           {
              System.out.println("You found your dir");
              File[] dirs=dir[x].listFiles();
              for(int y=0;y<dir.length;y++)
              {
                 System.out.println(dirs[y].getName()); //listing the files in the directory
              }
                 
           }
           //System.out.println(dir[x]);
        }
       
       
   
    }
}




good luck
ggomez
 
Posts: 45
Joined: Tue Apr 17, 2007 3:30 am
Location: !!!!!MéXiCo¡¡¡¡¡¡


Who is online

Users browsing this forum: No registered users and 1 guest