Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create file in jsp servlet

When I create a file in java servlet, I can't find that file for opening. This is my code in servlet:

FileOutputStream fout;
    try {
        fout = new FileOutputStream("title.txt");
        new PrintStream(fout).println(request.getParameter("txttitle"));
        fout.close();
        System.out.println(request.getParameter("txttitle"));
    } catch (Exception e) {
        System.out.println("I can't create file!");
    }

Where I can find that file?

like image 839
Farshid Shekari Avatar asked Dec 29 '25 10:12

Farshid Shekari


2 Answers

if you create file first as in

File f = new File("title.txt");
fout = new FileOutputStream(f);

then you use getAbsolutePath to return the location of where it has been created

System.out.println (f.getAbsolutePath());
like image 67
Scary Wombat Avatar answered Dec 31 '25 01:12

Scary Wombat


Since you have'nt specified any directory for the file, it will be placed in the default directory of the process that runs your servlet container.

I would recommand you to always specify the full path of your your file when doing this kind of things.

If you're running tomcat, you can use System.getProperty("catalina.base") to get the path of the tomcat base directory. This can sometimes help.

like image 39
Maurice Perry Avatar answered Dec 31 '25 03:12

Maurice Perry



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!