Here you can find the source of copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)
Parameter | Description |
---|---|
resourceName | the file name of the file in the jar |
replace | whether the file in the data folder should be replace if it exists |
Parameter | Description |
---|---|
RuntimeException | if the resource could not be copied |
public static Path copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace)
//package com.java2s; /*//w w w. j a v a 2 s.c om * BasePlugin * Copyright (C) 2014 Viciouss <http://www.doncarnage.de> * * This program 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; either * version 3 of the License, or (at your option) any later version. * * This program 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 * USA */ import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.StandardCopyOption; public class Main { /** * Copies the given file from the jar to the data folder. * * @param resourceName the file name of the file in the jar * @param replace whether the file in the data folder should be replace if it exists * @return a file handle to the copied file * @throws RuntimeException if the resource could not be copied */ public static Path copyFileToDataFolder(ClassLoader cl, String resourceName, Path targetFolder, boolean replace) { if (resourceName == null || "".equals(resourceName)) { throw new IllegalArgumentException("You need to provide a source file name."); } if (targetFolder == null) { throw new IllegalArgumentException("The destination is not defined."); } Path targetFile = targetFolder.resolve(resourceName); createDirectoriesForFile(targetFile); if (replace || !Files.exists(targetFile)) { InputStream is = cl.getResourceAsStream(resourceName); if (is == null) { throw new RuntimeException( String.format("The resource identified by %s could not be read.", resourceName)); } try { Files.copy(is, targetFile, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { throw new RuntimeException(e); } } if (!Files.exists(targetFile)) { throw new RuntimeException(String.format("Could not copy file %s to directory %s.", resourceName, targetFolder.toString())); } return targetFile; } /** * Creates all missing directories for the current path. This is to prevent an exception that would get thrown if the path contains a file in a directory * that does not yet exist as private logging files could and actually get by default stored in the log files subdirectory of the plugins own directory. * * @param file the file for which all needed directories should be created * @throws RuntimeException if the directories could not be created */ public static void createDirectoriesForFile(Path file) { Path parent = file.getParent(); try { Files.createDirectories(parent); } catch (IOException e) { throw new RuntimeException(e); } } }