0

I am trying to make a webapps which creates a file in a specific folder. This app should work both on windows and ubuntu but they have different file structures. So how do I mention path in creation of file and again in Ubuntu I also need to use permissions. How can I give permissions to the folder in which I am trying to create a file. I am using java for this and this is my code:

 //bfr=new BufferedReader(new InputStreamReader(System.in));
 String fileName="/home/hemant/"+credentials[3];
 String content=String.valueOf(n)+"\n"+messages.length;
 File file=new File(fileName);       
 if(!file.exists()){
      System.out.println("filecreated");
      file.createNewFile();
 }   

my app is a tomcat based app. What should I do? I am new to this and don't have any idea.

Baby
  • 4,924
  • 3
  • 27
  • 50
cooljohny
  • 596
  • 3
  • 11
  • 29
  • Try using a properties file, or `System.getProperty("os.name");` in a pinch. – Elliott Frisch Jun 25 '14 at 06:09
  • 1
    Assuming you're wanting to save files server-side then this question is basically a duplicate of http://stackoverflow.com/a/2663855/3651800 (although that answer basically says "don't do this") – Matt Coubrough Jun 25 '14 at 06:12

3 Answers3

0

If you are using servlet,you can find path using this -

String path = servletConfig.getServletContext().getRealPath("/WEB-INF")

It will work on Ubantu and WIndows.

Ninad Pingale
  • 5,634
  • 4
  • 24
  • 45
0

You can check operating system, on which your webapp is deployed by:

System.getProperty("os.name");

You can create file accordingly , For giving permissions to file , full control over file attributes is available in Java 7, as part of the "new" New IO facility (NIO.2)

Or you can checkout this link for more information : http://www.mkyong.com/java/how-to-set-the-file-permission-in-java/

Abhijeet Panwar
  • 1,767
  • 3
  • 23
  • 43
  • Actually I want to change the permission of folder to 777 where I am creating the file so that it can be created without any problem.. – cooljohny Jun 25 '14 at 06:22
  • Refer this article : http://www.journaldev.com/855/how-to-set-file-permissions-in-java-easily-using-java-7-posixfilepermission you can also write your native code in java to change permissions of that folder. – Abhijeet Panwar Jun 25 '14 at 06:26
0

You can get the information of Operating System using this code :

public class OSValidator {

    private static String OS = System.getProperty("os.name").toLowerCase();

    public static void main(String[] args) {

        System.out.println(OS);

        if (isWindows()) {
            System.out.println("This is Windows");
        } else if (isMac()) {
            System.out.println("This is Mac");
        } else if (isUnix()) {
            System.out.println("This is Unix or Linux");
        } else if (isSolaris()) {
            System.out.println("This is Solaris");
        } else {
            System.out.println("Your OS is not support!!");
        }
    }

    public static boolean isWindows() {

        return (OS.indexOf("win") >= 0);

    }

    public static boolean isMac() {

        return (OS.indexOf("mac") >= 0);

    }

    public static boolean isUnix() {

        return (OS.indexOf("nix") >= 0 || OS.indexOf("nux") >= 0 || OS.indexOf("aix") > 0 );

    }

    public static boolean isSolaris() {

        return (OS.indexOf("sunos") >= 0);

    }

}
Keval Trivedi
  • 1,152
  • 2
  • 11
  • 26
  • What abt the permissions part.I want to give all the permissions to the folder I am creating the file in so that the file is created without any problem.. – cooljohny Jun 25 '14 at 06:23
  • give the permission to folder you can visit this link http://stackoverflow.com/questions/664432/how-do-i-programmatically-change-file-permissions – Keval Trivedi Jun 25 '14 at 06:29