Java File Save saveFile(String where)

Here you can find the source of saveFile(String where)

Description

Identical to savePath(), but returns a File object.

License

Open Source License

Declaration

public static File saveFile(String where) 

Method Source Code

//package com.java2s;
/*/*from w  w  w.j a  va  2 s.  co m*/
Part of the Processing project - http://processing.org
    
Copyright (c) 2012-13 The Processing Foundation
Copyright (c) 2004-12 Ben Fry and Casey Reas
Copyright (c) 2001-04 Massachusetts Institute of Technology
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
    
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA  02111-1307  USA
*/

import java.io.File;

public class Main {
    /**
     * Identical to savePath(), but returns a File object.
     */
    public static File saveFile(String where) {
        return new File(savePath(where));
    }

    /**
     * Returns a path inside the applet folder to save to. Like sketchPath(),
     * but creates any in-between folders so that things save properly.
     * <p/>
     * All saveXxxx() functions use the path to the sketch folder, rather than
     * its data folder. Once exported, the data folder will be found inside the
     * jar file of the exported application or applet. In this case, it's not
     * possible to save data into the jar file, because it will often be running
     * from a server, or marked in-use if running from a local file system.
     * With this in mind, saving to the data path doesn't make sense anyway.
     * If you know you're running locally, and want to save to the data folder,
     * use <TT>saveXxxx("data/blah.dat")</TT>.
     */
    public static String savePath(String where) {
        if (where == null)
            return null;
        createPath(where);
        return where;
    }

    /**
     * Takes a path and creates any in-between folders if they don't
     * already exist. Useful when trying to save to a subfolder that
     * may not actually exist.
     */
    static public void createPath(String path) {
        createPath(new File(path));
    }

    static public void createPath(File file) {
        try {
            String parent = file.getParent();
            if (parent != null) {
                File unit = new File(parent);
                if (!unit.exists())
                    unit.mkdirs();
            }
        } catch (SecurityException se) {
            System.err.println("You don't have permissions to create " + file.getAbsolutePath());
        }
    }
}

Related

  1. saveContent(OutputStream os, byte[] content)
  2. saveConvert(String theString, boolean escapeSpace)
  3. saveDoubleMatrix(double[][] matrix, PrintStream out)
  4. saveFile(InputStream st, File testFile)
  5. saveFile(String fname, byte[] bytes)
  6. saveInputToOutput(InputStream is, OutputStream os)
  7. saveIntArray(int[] array, PrintStream out)
  8. SaveIntFile(int[] list, String filename)
  9. saveStreamAsString(InputStream is)