List of usage examples for java.io File getAbsoluteFile
public File getAbsoluteFile()
From source file:edu.ku.brc.specify.tasks.services.PickListUtils.java
/** * @param localizableIO/*from ww w . ja va 2s . c om*/ * @param collection * @return */ public static boolean importPickLists(final LocalizableIOIFace localizableIO, final Collection collection) { // Apply is Import All PickLists FileDialog dlg = new FileDialog(((Frame) UIRegistry.getTopWindow()), getResourceString(getI18n("PL_IMPORT")), FileDialog.LOAD); dlg.setDirectory(UIRegistry.getUserHomeDir()); dlg.setFile(getPickListXMLName()); UIHelper.centerAndShow(dlg); String dirStr = dlg.getDirectory(); String fileName = dlg.getFile(); if (StringUtils.isEmpty(dirStr) || StringUtils.isEmpty(fileName)) { return false; } final String path = dirStr + fileName; File file = new File(path); if (!file.exists()) { UIRegistry.showLocalizedError(getI18n("PL_FILE_NOT_EXIST"), file.getAbsoluteFile()); return false; } List<BldrPickList> bldrPickLists = DataBuilder.getBldrPickLists(null, file); Integer cnt = null; boolean wasErr = false; DataProviderSessionIFace session = null; try { session = DataProviderFactory.getInstance().createSession(); session.beginTransaction(); HashMap<String, PickList> plHash = new HashMap<String, PickList>(); List<PickList> items = getPickLists(localizableIO, true, false); for (PickList pl : items) { plHash.put(pl.getName(), pl); //System.out.println("["+pl.getName()+"]"); } for (BldrPickList bpl : bldrPickLists) { PickList pickList = plHash.get(bpl.getName()); //System.out.println("["+bpl.getName()+"]["+(pickList != null ? pickList.getName() : "null") + "]"); if (pickList == null) { // External PickList is new pickList = createPickList(bpl, collection); session.saveOrUpdate(pickList); if (cnt == null) cnt = 0; cnt++; } else if (!pickListsEqual(pickList, bpl)) { session.delete(pickList); collection.getPickLists().remove(pickList); pickList = createPickList(bpl, collection); session.saveOrUpdate(pickList); collection.getPickLists().add(pickList); if (cnt == null) cnt = 0; cnt++; } } session.commit(); } catch (Exception ex) { wasErr = true; if (session != null) session.rollback(); ex.printStackTrace(); edu.ku.brc.af.core.UsageTracker.incrHandledUsageCount(); edu.ku.brc.exceptions.ExceptionTracker.getInstance().capture(PickListEditorDlg.class, ex); } finally { if (session != null) { session.close(); } } String key = wasErr ? "PL_ERR_IMP" : cnt != null ? "PL_WASIMPORT" : "PL_MATCHIMP"; UIRegistry.displayInfoMsgDlgLocalized(getI18n(key), cnt); return true; }
From source file:edu.ku.brc.util.AttachmentUtils.java
/** * @param attachmentLocation// w w w .j a va 2s . c o m * @return */ public static boolean isAttachmentDirMounted(final File attachmentLocation) { String fullPath = ""; String statsMsg = "The test to write to the AttachmentLocation [%s] %s."; try { fullPath = attachmentLocation.getCanonicalPath(); if (attachmentLocation.exists()) { if (attachmentLocation.isDirectory()) { File tmpFile = new File(attachmentLocation.getAbsoluteFile() + File.separator + System.currentTimeMillis() + System.getProperty("user.name")); //log.debug(String.format("Trying to write a file to AttachmentLocation [%s]", tmpFile.getCanonicalPath())); if (tmpFile.createNewFile()) { // I don't think I need this anymore FileOutputStream fos = FileUtils.openOutputStream(tmpFile); fos.write(1); fos.close(); tmpFile.delete(); //log.debug(String.format(statsMsg, fullPath, "succeeded")); return true; } else { log.error(String.format("The Attachment Location [%s] atachment file couldn't be created", fullPath)); } } else { log.error(String.format("The Attachment Location [%s] is not a directory.", fullPath)); } } else { log.error(String.format("The Attachment Location [%s] doesn't exist.", fullPath)); } } catch (Exception ex) { ex.printStackTrace(); } log.debug(String.format(statsMsg, fullPath, "failed")); return false; }
From source file:com.oeg.oops.VocabUtils.java
/** * Code to unzip a file. Inspired from/*from w ww. j a va 2s . c o m*/ * http://www.mkyong.com/java/how-to-decompress-files-from-a-zip-file/ * Taken from * @param resourceName * @param outputFolder */ public static void unZipIt(String resourceName, String outputFolder) { byte[] buffer = new byte[1024]; try { ZipInputStream zis = new ZipInputStream(VocabUtils.class.getResourceAsStream(resourceName)); ZipEntry ze = zis.getNextEntry(); while (ze != null) { String fileName = ze.getName(); File newFile = new File(outputFolder + File.separator + fileName); System.out.println("file unzip : " + newFile.getAbsoluteFile()); if (ze.isDirectory()) { String temp = newFile.getAbsolutePath(); new File(temp).mkdirs(); } else { FileOutputStream fos = new FileOutputStream(newFile); int len; while ((len = zis.read(buffer)) > 0) { fos.write(buffer, 0, len); } fos.close(); } ze = zis.getNextEntry(); } zis.closeEntry(); zis.close(); } catch (IOException ex) { System.err.println("Error while extracting the reosurces: " + ex.getMessage()); } }
From source file:graphene.util.fs.FileUtils.java
public static void deleteRecursively(final File directory) throws IOException { final Stack<File> stack = new Stack<>(); final List<File> temp = new LinkedList<>(); stack.push(directory.getAbsoluteFile()); while (!stack.isEmpty()) { final File top = stack.pop(); File[] files = top.listFiles(); if (files != null) { for (final File child : files) { if (child.isFile()) { if (!deleteFile(child)) { throw new IOException("Failed to delete " + child.getCanonicalPath()); }//from w w w . j av a 2 s. c o m } else { temp.add(child); } } } files = top.listFiles(); if ((files == null) || (files.length == 0)) { if (!deleteFile(top)) { throw new IOException("Failed to delete " + top.getCanonicalPath()); } } else { stack.push(top); for (final File f : temp) { stack.push(f); } } temp.clear(); } }
From source file:info.extensiblecatalog.OAIToolkit.utils.ApplInfo.java
/** * start logging/*from w ww . j a v a 2 s . co m*/ */ public static void initLogging(String rootDir, String logDir, String propertyFileName) throws FileNotFoundException { ClassLoader cloader = ApplInfo.class.getClassLoader(); InputStream logProps = cloader.getResourceAsStream(propertyFileName); if (null == logProps) { File propertyFile = new File(rootDir, propertyFileName); if (propertyFile.exists()) { try { System.out.println(" log4j property file: " + propertyFile.getAbsoluteFile()); logProps = new FileInputStream(propertyFile); } catch (FileNotFoundException e) { System.out.println("Exception" + e); throw e; } } } if (null != logProps) { LoggingParameters logParams = new LoggingParameters(logDir); Logging.initLogging(logParams, logProps); } }
From source file:edu.asu.cse564.samples.crud.io.GradebookIO.java
public static void writeToGradebook(List<Gradebook> gradebookList, String filename) { //GradebookIO gbio = new GradebookIO(); try {//ww w.j a v a 2s .c o m //String path = gbio.getPath(filename); String path = filename; File file = new File(path); // if file doesnt exists, then create it if (!file.exists()) { LOG.info("Creating file as it does not exist"); file.createNewFile(); LOG.debug("Created file = {}", file.getAbsolutePath()); } String jsonString = null; jsonString = Converter.convertFromObjectToJSON(gradebookList, gradebookList.getClass()); LOG.info("File path = {}", file.getAbsolutePath()); FileWriter fw = new FileWriter(file.getAbsoluteFile()); try (BufferedWriter bw = new BufferedWriter(fw)) { bw.write(jsonString); } } catch (Exception e) { e.printStackTrace(); ; } }
From source file:com.sugarcrm.candybean.datasource.CsvDataAdapterSystemTest.java
private static void createFile(String dir, String filename, String content) { try {/*from w w w . j av a 2 s . c o m*/ File file = new File(dir + File.separator + filename); // Create file if not exist if (!file.exists()) { file.createNewFile(); System.out.println("createFile(): created " + file.getAbsolutePath()); } else { System.out.println("createFile(): " + file.getAbsolutePath() + " already exists"); } FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); bw.write(content); bw.close(); } catch (IOException e) { e.printStackTrace(); } }
From source file:io.logspace.agent.hq.HqAgentController.java
private static File getFile(String path, String agentControllerId) { String resolvedPath = resolveProperties(path); File file = new File(resolvedPath); try {/*from w w w. j a v a 2 s .c o m*/ file = file.getCanonicalFile(); } catch (IOException e) { // ignore this } return new File(file.getAbsoluteFile(), createQueueFileName(agentControllerId)); }
From source file:org.cloudifysource.azure.AbstractCliAzureDeploymentTest.java
public static String runCliCommands(File cliExecutablePath, List<List<String>> commands, boolean isDebug) throws IOException, InterruptedException { if (!cliExecutablePath.isFile()) { throw new IllegalArgumentException(cliExecutablePath + " is not a file"); }/*from w ww . j a v a 2 s .co m*/ File workingDirectory = cliExecutablePath.getAbsoluteFile().getParentFile(); if (!workingDirectory.isDirectory()) { throw new IllegalArgumentException(workingDirectory + " is not a directory"); } int argsCount = 0; for (List<String> command : commands) { argsCount += command.size(); } // needed to properly intercept error return code String[] cmd = new String[(argsCount == 0 ? 0 : 1) + 4 /* cmd /c call cloudify.bat ["args"] */]; int i = 0; cmd[i] = "cmd"; i++; cmd[i] = "/c"; i++; cmd[i] = "call"; i++; cmd[i] = cliExecutablePath.getAbsolutePath(); i++; if (argsCount > 0) { cmd[i] = "\""; //TODO: Use StringBuilder for (List<String> command : commands) { if (command.size() > 0) { for (String arg : command) { if (cmd[i].length() > 0) { cmd[i] += " "; } cmd[i] += arg; } cmd[i] += ";"; } } cmd[i] += "\""; } final ProcessBuilder pb = new ProcessBuilder(cmd); pb.directory(workingDirectory); pb.redirectErrorStream(true); String extCloudifyJavaOptions = ""; if (isDebug) { extCloudifyJavaOptions += "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=9000 -Xnoagent -Djava.compiler=NONE"; } pb.environment().put("EXT_CLOUDIFY_JAVA_OPTIONS", extCloudifyJavaOptions); final StringBuilder sb = new StringBuilder(); logger.info("running: " + cliExecutablePath + " " + Arrays.toString(cmd)); // log std output and redirected std error Process p = pb.start(); BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream())); String line = reader.readLine(); while (line != null) { sb.append(line).append('\n'); line = reader.readLine(); logger.info(line); } final String readResult = sb.toString(); final int exitValue = p.waitFor(); logger.info("Exit value = " + exitValue); if (exitValue != 0) { Assert.fail("Cli ended with error code: " + exitValue); } return readResult; }
From source file:com.devilyang.musicstation.cache.ACache.java
public static ACache get(File cacheDir, long max_zise, int max_count) { ACache aCache = mInstanceMap.get(cacheDir.getAbsoluteFile() + myPid()); if (aCache == null) { aCache = new ACache(cacheDir, max_zise, max_count); mInstanceMap.put(cacheDir.getAbsolutePath() + myPid(), aCache); }/*from www. j av a 2 s. c o m*/ return aCache; }