Resolved Java: creating directories and files

Discussion in 'Mac Programming' started by danner26, Jan 26, 2015.

  1. danner26, Jan 26, 2015
    Last edited: Jan 27, 2015

    danner26 macrumors newbie

    Joined:
    Jan 26, 2015
    #1
    Hello,

    I am a windows developer, but I need to get my program to be compatible on multiple platforms.

    In windows, I usually save and create directories in AppData. Where is the best place to do so on a mac?

    Also, I am use to creating a new directory, then the file using

    Code:
         File file = new File(System.getenv("APPDATA") + "/Test");
         file.mkdirs();
    
         File file2 = new File(System.getenv("APPDATA") + "/Test/test.txt");
         file.createNewFile();
    
    Please do not take this code as literal. There is a lot more implemented, I am just shorthanding this.

    Is this the proper way to do so on the OSX platform ?


    Thanks much,

    Daniel
     
  2. Sciuriware macrumors regular

    Sciuriware

    Joined:
    Jan 4, 2014
    Location:
    Gelderland
    #2
    Hi,
    1) save user data under /Users/{you},
    2) conventional in OSX is that data is saved under Applications and Library
    but if I were you I wouldn't mix data you don't know about with (your own)
    data you do know about.
    3) no problem to save your own stuff in (e.g.) /Users/{you}/MyData
    or even /Users/{you}/AppData

    Multi-platform programs are a drag: split MSWindows / UNIX like by the
    directory path separator ('/' '\\')
    ;JOOP!
     
  3. Sciuriware macrumors regular

    Sciuriware

    Joined:
    Jan 4, 2014
    Location:
    Gelderland
    #3
    Example:

    Code:
          if(File.separatorChar == '/')    // UNIX flavours.
          {
             if(new File("/Users").isDirectory()) // OSX.
             {
                // TODO
             }
             else                          // Other UNIX flavours.
             {
                // TODO
             }
          }
          else if(File.separatorChar == '\\') // MSWindows flavours.
          {
             if(new File("C:/Users").isDirectory()) // Windows 7, Vista.
             {
                // TODO
             }
             else
             {
                if(new File("C:/Documents and Settings/").isDirectory()) // Windows XP, NT.
                {
                   // TODO
                }
                else                       // Any older Windows 98, ME, 2000
                {
                   // TODO
                }
             }
          }
    
    Probably quite not perfect for several UNIX/LINUX/BSD flavours .......
    ;JOOP!
     
  4. hokan macrumors member

    Joined:
    Mar 18, 2014
    Location:
    Sweden
    #4
    While your basic idea seems reasonable, I would say that hard coding Windows paths to use the "C" drive is a pretty horrible idea.
    I'm sure that Windows has methods/functions similar to NSFileManager -URLForDirectory:inDomain:appropriateForURL:create:error: and the
    NSSearchPathDirectory locations - to find standard locations or the drive letter of the current boot drive, if Windows is installed on something odd like "D".
     
  5. Sciuriware macrumors regular

    Sciuriware

    Joined:
    Jan 4, 2014
    Location:
    Gelderland
    #5
    It was just an example, like the original post: don't take it literally.
    Btw.: on my own computer my data live on D:\ , /D/ resp.

    ;JOOP!
     
  6. hokan macrumors member

    Joined:
    Mar 18, 2014
    Location:
    Sweden
    #6
    I kind of assumed so.

    I mainly posted about this to avoid any readers getting the idea that this is a good way to do things.
     
  7. danner26 thread starter macrumors newbie

    Joined:
    Jan 26, 2015
    #7
    Hey guys thanks for the response, but where you see the

    System.getenv("APPDATA")

    Puts the directory path in windows appdata location. I found my answer, it is the same, just the paths are different.

    Thank you everyone who answered !
     

Share This Page