Example usage for java.io File createNewFile

List of usage examples for java.io File createNewFile

Introduction

In this page you can find the example usage for java.io File createNewFile.

Prototype

public boolean createNewFile() throws IOException 

Source Link

Document

Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist.

Usage

From source file:de.flapdoodle.embed.process.io.file.Files.java

public static File createTempFile(File tempDir, String tempFileName)
        throws IOException, FileAlreadyExistsException {
    File tempFile = new File(tempDir, tempFileName);

    File parent = tempFile.getParentFile();
    if (!parent.exists() && !parent.mkdirs()) {
        throw new IllegalStateException("Couldn't create dir: " + parent);
    }/*from w  w  w  .j a v a 2 s.  com*/

    if (!tempFile.createNewFile())
        throw new FileAlreadyExistsException("Could not create Tempfile", tempFile);
    return tempFile;
}

From source file:WarUtil.java

private static void writeLastModifiled(File file, long time) {
    BufferedWriter writer = null;
    try {/*ww  w .j  ava2 s  . com*/
        if (!file.exists()) {
            file.createNewFile();
        }
        writer = new BufferedWriter(new FileWriter(file));
        writer.write(Long.toString(time));
        writer.flush();
    } catch (Exception e) {

    }
}

From source file:com.googlecode.logVisualizer.LogVisualizer.java

/**
 * Creates KoL data files if they do not already exist in the file system.
 *//*from   w w w.j a v a2 s  . co m*/
public static void writeDataFilesToFileSystem() {
    final List<File> kolDataFiles = Lists.newArrayList();
    kolDataFiles
            .add(new File(ROOT_DIRECTORY + File.separator + KOL_DATA_DIRECTORY + "bbcodeAugmentations.txt"));
    kolDataFiles.add(new File(ROOT_DIRECTORY + File.separator + KOL_DATA_DIRECTORY + "htmlAugmentations.txt"));
    kolDataFiles.add(new File(ROOT_DIRECTORY + File.separator + KOL_DATA_DIRECTORY + "textAugmentations.txt"));

    for (final File f : kolDataFiles)
        if (!f.exists()) {
            String tmpLine;
            final BufferedReader br = DataUtilities.getReader(KOL_DATA_DIRECTORY, f.getName());

            try {
                f.createNewFile();
                final PrintWriter fileWriter = new PrintWriter(f);

                while ((tmpLine = br.readLine()) != null)
                    fileWriter.println(tmpLine);

                fileWriter.close();
                br.close();
            } catch (final IOException e) {
                e.printStackTrace();
            }
        }
}

From source file:de.longri.cachebox3.Utils.java

/**
 * Returns TRUE has the given PATH write permission!
 *
 * @param Path//  w  w  w.  ja va 2s  . c  om
 * @return
 */
public static boolean checkWritePermission(String Path) {
    boolean result = true;
    try {
        String testFolderName = Path + "/Test/";

        File testFolder = new File(testFolderName);
        if (testFolder.mkdirs()) {
            File test = new File(testFolderName + "Test.txt");
            test.createNewFile();
            if (!test.exists()) {
                result = false;
            } else {
                test.delete();
                testFolder.delete();
            }
        } else {
            result = false;
        }
    } catch (IOException e) {
        result = false;
        e.printStackTrace();
    }
    return result;
}

From source file:net.rptools.maptool.util.AssetExtractor.java

public static void extract() throws Exception {
    new Thread() {
        @Override/*ww w.ja  v a 2 s  .c  om*/
        public void run() {
            JFileChooser chooser = new JFileChooser();
            chooser.setMultiSelectionEnabled(false);
            chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
            if (chooser.showOpenDialog(null) != JFileChooser.APPROVE_OPTION) {
                return;
            }
            File file = chooser.getSelectedFile();
            File newDir = new File(file.getParentFile(),
                    file.getName().substring(0, file.getName().lastIndexOf('.')) + "_images");

            JLabel label = new JLabel("", JLabel.CENTER);
            JFrame frame = new JFrame();
            frame.setTitle("Campaign Image Extractor");
            frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
            frame.setSize(400, 75);
            frame.add(label);
            SwingUtil.centerOnScreen(frame);
            frame.setVisible(true);
            Reader r = null;
            OutputStream out = null;
            PackedFile pakfile = null;
            try {
                newDir.mkdirs();

                label.setText("Loading campaign ...");
                pakfile = new PackedFile(file);

                Set<String> files = pakfile.getPaths();
                XStream xstream = new XStream();
                int count = 0;
                for (String filename : files) {
                    count++;
                    if (filename.indexOf("assets") < 0) {
                        continue;
                    }
                    r = pakfile.getFileAsReader(filename);
                    Asset asset = (Asset) xstream.fromXML(r);
                    IOUtils.closeQuietly(r);

                    File newFile = new File(newDir, asset.getName() + ".jpg");
                    label.setText("Extracting image " + count + " of " + files.size() + ": " + newFile);
                    if (newFile.exists()) {
                        newFile.delete();
                    }
                    newFile.createNewFile();
                    out = new FileOutputStream(newFile);
                    FileUtil.copyWithClose(new ByteArrayInputStream(asset.getImage()), out);
                }
                label.setText("Done.");
            } catch (Exception ioe) {
                MapTool.showInformation("AssetExtractor failure", ioe);
            } finally {
                if (pakfile != null)
                    pakfile.close();
                IOUtils.closeQuietly(r);
                IOUtils.closeQuietly(out);
            }
        }
    }.start();
}

From source file:de.suse.swamp.modules.actions.DatapackActions.java

public static boolean storeFile(Databit dbit, boolean overwrite, FileItem fi, String uname) throws Exception {
    // skip empty uploads: 
    if (fi != null && !fi.getName().trim().equals("") && fi.getSize() > 0) {
        String fileDir = new SWAMPAPI().doGetProperty("ATTACHMENT_DIR", uname);

        if (!(new File(fileDir)).canWrite()) {
            throw new Exception("Cannot write to configured path: " + fileDir);
        }/*from   w  w  w  .  java2 s  . co m*/

        String fileName = fi.getName();
        // fix for browsers setting complete path as name: 
        if (fileName.indexOf("/") >= 0)
            fileName = fileName.substring(fileName.lastIndexOf("/") + 1);
        if (fileName.indexOf("\\") >= 0)
            fileName = fileName.substring(fileName.lastIndexOf("\\") + 1);

        File file = new File(fileDir + fs + dbit.getId() + "-" + fileName);
        if (!overwrite) {
            if (!file.createNewFile()) {
                throw new Exception("Cannot write to file: " + file.getName() + ". File already exists?");
            }
        } else {
            if (file.exists()) {
                file.delete();
            }
            // if its a file with a new name, delete the old one: 
            File oldFile = new File(fileDir + fs + dbit.getId() + "-" + dbit.getValue());
            if (oldFile.exists()) {
                Logger.DEBUG("Deleting old file: " + oldFile.getPath());
                oldFile.delete();
            }
        }

        FileOutputStream stream = new FileOutputStream(file);
        stream.write(fi.get());
        stream.close();
        return true;
    } else {
        return false;
    }
}

From source file:gdt.data.grain.Support.java

/**
 * Copy the icon file from the class path into the target directory. 
 * @param handler the class/*from ww  w.  j  a  v a  2  s  .  co  m*/
 * @param icon$ the name of the icon file.
 *  @param directory$ the icons directory path .
 */
public static void addHandlerIcon(Class<?> handler, String icon$, String directory$) {
    try {

        File iconFile = new File(directory$ + "/" + icon$);
        if (iconFile.exists())
            return;
        iconFile.createNewFile();
        InputStream is = handler.getResourceAsStream(icon$);
        FileOutputStream fos = new FileOutputStream(iconFile);
        byte[] b = new byte[1024];
        int bytesRead = 0;
        while ((bytesRead = is.read(b)) != -1) {
            fos.write(b, 0, bytesRead);
        }
        is.close();
        fos.close();
    } catch (Exception e) {
        Logger.getLogger(gdt.data.grain.Support.class.getName()).severe(e.toString());
    }
}

From source file:com.alibaba.otter.shared.common.utils.NioUtilsPerformance.java

public static void sendFileTest(File source, File target) throws Exception {
    FileInputStream fis = null;/*  ww w.  ja  v a  2 s .  c o m*/
    FileOutputStream fos = null;
    try {
        fis = new FileInputStream(source);
        fos = new FileOutputStream(target);
        FileChannel sChannel = fis.getChannel();
        FileChannel tChannel = fos.getChannel();
        target.createNewFile();
        sChannel.transferTo(0, source.length(), tChannel);
        tChannel.close();
        sChannel.close();
    } finally {
        IOUtils.closeQuietly(fis);
        IOUtils.closeQuietly(fos);
    }
}

From source file:Main.java

public static void copyFile(File in, File out) throws IOException {
    FileInputStream fis = new FileInputStream(in);
    if (!out.exists()) {
        if (in.isDirectory()) {
            out.mkdirs();//from   ww w  .j  a va  2  s.  com
        } else {
            File parentDir = new File(out.getParent());
            if (!parentDir.exists()) {
                parentDir.mkdirs();
            }
            out.createNewFile();
        }
    }
    FileOutputStream fos = new FileOutputStream(out);
    try {
        byte[] buf = new byte[8192];
        int i = 0;
        while ((i = fis.read(buf)) != -1)
            fos.write(buf, 0, i);
    } catch (IOException e) {
        throw e;
    } finally {
        fis.close();
        fos.close();
    }
}

From source file:cn.guoyukun.spring.web.upload.FileUploadUtils.java

private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException {

    uploadDir = FilenameUtils.normalizeNoEndSeparator(uploadDir);

    File desc = new File(uploadDir + File.separator + filename);

    if (!desc.getParentFile().exists()) {
        desc.getParentFile().mkdirs();/* w ww  . j a v a  2 s  .  com*/
    }
    if (!desc.exists()) {
        desc.createNewFile();
    }
    return desc;
}