• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 181
  • Last Modified:

Form values ---> Writing to a text file --- JAVA

Hello Everybody,

I have a form (Submit.jsp) which contains three fields. First one is 'Wall time' and the second one is memory and the third one is 'comment'. Once i enterend the values for these three fields and submit the form, a text file has to be generated as shown below.For example if i entered in the first column 500, in the second column '200mb', in the third column './subrun', the text file should be genetared like as shown below.That means the user is setting the values for the second line,third line and sixth line (see the below text file).

=================
Job.txt
=================
#!/bin/sh
#PBS -l walltime=500:00  <---  second line
#PBS -l mem=200mb       <---  third line line
#PBS -j oe  

./subrun                        <---  sixth line
==================

Please help me in this regard.A snippest of code would be helpful.
Thanks
Rams
0
ramsin
Asked:
ramsin
  • 9
  • 5
  • 2
1 Solution
 
petmagdyCommented:
ok in ur Servlet do like this snippet:

import java.text.*;

public void service(Request request, ....)
{
   String wallTime = (String) request.getParameter("comment");
   // Same for other 2 fields
   
 Object[] arguments = {
     wallTime,
     mem,
     3rd parameter
 };

 String result = MessageFormat.format(
     "#!/bin/sh /r/n  ........walltime= {1} /r/n #PBS -l mem= {2} ................",
     arguments);


    File fileToWrite = new File( "c:\job.txt" );
     try
    {
      FileOutputStream out = new FileOutputStream(fileToWrite);
      out.write( result.getBytes() );
      out.close();
    }

    catch (Exception ex)
    {
     }
 
}
0
 
petmagdyCommented:
sorry just little correction:
>>    String wallTime = (String) request.getParameter("comment");
i mean offcouse:
String wallTime = (String) request.getParameter("wallTime");
0
 
CEHJCommented:
Parameters to MessageFormat are 0-indexed and you probably need Unix line breaks:

final String SHELL_SCRIPT = "#!/bin/sh\n#PBS -l walltime={0}\n#PBS -l mem={1}\n#PBS -j oe\n\n{2}";
String result = MessageFormat.format(SHELL_SCRIPT, arguments);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
ramsinAuthor Commented:
Hello,
i am getting the total out put in one line.but i need the output in several lines as shown
below. I tried two options(see below(1) and (2) ) i mean /r/n and \n  as line breks...both are not working.


#!/bin/sh
#PBS -l walltime=500:00
#PBS -l mem=200mb
#PBS -l ncpus=60
#PBS -j oe

./subrun
./subrun


1)

final String SHELL_SCRIPT = " #!/bin/sh/r/n #PBS -l jobname={0}/r/n #PBS -l machine={1}/r/n #PBS -l user={2}/r/n #PBS -l ncpus={3}/r/n  #PBS -l priority={4}/r/n  #PBS -l architecture={5}/r/n #PBS -j oe/r/n /r/n {6}";


String result = MessageFormat.format(SHELL_SCRIPT, arguments);


Output ::
======
 #!/bin/sh/r/n #PBS -l jobname=jn5/r/n #PBS -l machine=m5/r/n #PBS -l user=us5/r/n #PBS -l ncpus=15/r/n  #PBS -l priority=1005/r/n  #PBS -l architecture=windows/r/n #PBS -j oe/r/n /r/n subrun3

2)
      Object[] arguments = {
                                                                                   jobname1,machine1,user1,ncr1,
                      priority1,architecture1,comment1
                                                               };

final String SHELL_SCRIPT = " #!/bin/sh\n #PBS -l jobname={0}\n #PBS -l machine={1}\n #PBS -l user={2}\n #PBS -l ncpus={3}\n  #PBS -l priority={4}\n  #PBS -l architecture={5}\n #PBS -j oe\n \n {6}";
String result = MessageFormat.format(SHELL_SCRIPT, arguments);

output ::
======
 #!/bin/sh  #PBS -l jobname=name1  #PBS -l machine=machine1  #PBS -l user=john  #PBS -l ncpus=14   #PBS -l priority=1024   #PBS -l architecture=solaris #PBS -j oe   ./subrun

Please reply,
Thank you
Rams
0
 
petmagdyCommented:
please try \r\n
0
 
petmagdyCommented:
also please try to put the generated file on UNIX and see how it looks while it is still \n
0
 
ramsinAuthor Commented:
thank you,
i am getting in several lines this time.I am getting the output like this::
I am finding a space before each line , somelines having two spaces before the lines.There should not be any spaces before the line.Otherwise this job will not be accepted by PBS (Portable Batch System).

 #!/bin/sh
 #PBS -l jobname=job1
 #PBS -l machine=ma1
 #PBS -l user=us1
 #PBS -l ncpus=c1
  #PBS -l priority=p1
  #PBS -l architecture=a1
 #PBS -j oe
 
 co1


Thank you, Pl reply,
Rams
0
 
petmagdyCommented:
in the message pattern don't put any spaces after \r\n
0
 
ramsinAuthor Commented:
Thanks, working fine. But one small prob agian, The file is saving to "C:\jakarta-tomcat-5.0.25\bin" if i left the defalut.
 File fileToWrite = new File( "job99.txt" );

If i try to save the file to desktop i am getting an error.
File fileToWrite = new File( "C:\Documents and Settings\ravuri.DS17\Desktop\job99.txt" );


plp reply,Thnks
rams
0
 
petmagdyCommented:
save to path dosn't include spaces and have write access on it
0
 
petmagdyCommented:
ohhh sorry
it should be like this:

File fileToWrite = new File( "C:\\Documents and Settings\\ravuri.DS17\\Desktop\\job99.txt" );


u can also save to ur web root directory like this:

    String path = getServletConfig().getServletContext().getRealPath( "/" ) ;
        File f = new File( path + "\\job99.txt" ) ;
0
 
CEHJCommented:
>>i am getting the total out put in one line

That's because you're not using my code ;-) Did you not see it? You also need to observe my point about line feeds
0
 
ramsinAuthor Commented:
Hello
I have a file named 'PBSJob.txt' which contains the follwoing contents:

#!/bin/sh
#PBS -l jobname=jn1
#PBS -l machine=mach1
#PBS -l user=us1
#PBS -l ncpus=18
#PBS -l priority=888
#PBS -l architecture=arch1
#PBS -j oe

.\ subrun

i have to read the contents of the above file  and write to mySQL database table(submitjobs) .My database table contains only one column named 'contents' which is 'BLOB' type.
Pl help me in this regard.



Thanks
rams
 
0
 
petmagdyCommented:
Did u solved the original Question problem and was able to generate the file u want?
0
 
ramsinAuthor Commented:
yes, i am able to generate the file.But, If i am able to store that file contents in the databse(as told above) then my total problem solved.

Thanks,pl reply
rams
0
 
petmagdyCommented:
that was not part of the original question, this is completly a new question, according to experts exchange rules and guidelines it is prevented:
1- Upscoping the Question
2- doing somebody homework

in order for us to help u and others please follow the regulations, although I will just provide u with a useful tip to start with, please read this link

http://www.ideas2work.com/share-knowledge-details.php/username/guddu/title/Storing%20and%20retrieving%20Files%20to%20and%20from%20Database%20as%20stream./technology/JDBC%20-%20Java/
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 9
  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now