List of usage examples for org.apache.commons.io FileUtils forceMkdir
public static void forceMkdir(File directory) throws IOException
From source file:com.ss.language.model.gibblda.LDADataset.java
private static void readFromDatabase(int total, LDADataset data) throws IOException { // ?id//w ww .j a v a 2 s . co m File docIdxFile = new File(LDACmdOption.curOption.get().dir, LDACmdOption.curOption.get().docIdFile); if (docIdxFile.isFile()) { FileUtils.forceDelete(docIdxFile); } if (!docIdxFile.getParentFile().isDirectory()) { FileUtils.forceMkdir(docIdxFile.getParentFile()); } docIdxFile.createNewFile(); BufferedWriter br = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(docIdxFile), LDACmdOption.curOption.get().fileEncoding)); int perPage = 100; int totalPage = (total / perPage) + (total % perPage == 0 ? 0 : 1); int index = -1; for (int curPage = 0; curPage < totalPage; ++curPage) { // ? String titleSql = "select document_title,document_content from " + tableName + " order by rec_id asc limit " + (curPage * perPage) + "," + perPage; List<Map<String, Object>> result = DatabaseConfig.query(titleSql); if (result == null || result.isEmpty()) { break; } for (Map<String, Object> record : result) { String title = (String) record.get("document_title"); String content = (String) record.get("document_content"); if (content == null || content.trim().isEmpty()) { continue; } ++index; System.out.println("??" + (curPage + 1) + "" + (index + 1) + "?/" + total + "?"); data.setDoc(content, index); br.write(title + IOUtils.LINE_SEPARATOR); } } br.flush(); br.close(); }
From source file:ke.co.tawi.babblesms.server.servlet.upload.ContactUpload.java
/** * @param item/* ww w. ja v a2s . c o m*/ * @return the file handle */ private File processUploadedFile(FileItem item) { // A random folder in the system temporary directory and write the file there String folder = System.getProperty("java.io.tmpdir") + File.separator + RandomStringUtils.randomAlphabetic(5); File file = null; try { FileUtils.forceMkdir(new File(folder)); file = new File(folder + File.separator + item.getName()); item.write(file); } catch (IOException e) { logger.error("IOException while processUploadedFile: " + item.getName()); logger.error(e); } catch (Exception e) { logger.error("Exception while processUploadedFile: " + item.getName()); logger.error(e); } return file; }
From source file:name.martingeisse.ecobuild.moduletool.output.OutputTool.java
protected void handleFolder(IModuleToolContext context, State state, String[] segments) throws IOException { // basic command syntax check if (segments.length != 2) { context.getLogger().logError("Found 'folder' command with " + segments.length + " arguments"); return;//from w w w . jav a 2 s . c om } // parse the module path of the folder ModulePath modulePath; try { modulePath = new ModulePath(segments[1]); } catch (UserMessageBuildException e) { context.getLogger().logError("Found 'folder' command with malformed folder path. " + e.getMessage()); return; } // resolve the module path to a folder path File folderPath; try { folderPath = modulePath.resolve(context.getRootBuildFolder(), state.currentFolder); } catch (UserMessageBuildException e) { context.getLogger().logError("Found 'folder' command with unresolvable folder path. " + e.getMessage()); return; } // create the folder and any required ancestor folders FileUtils.forceMkdir(folderPath); // make this the current folder, remembering the previous one state.folderStack.push(state.currentFolder); state.currentFolder = folderPath; }
From source file:com.uwsoft.editor.data.manager.DataManager.java
public void createEmptyProject(String projectName, int width, int height) throws IOException { if (workspacePath.endsWith(File.separator)) { workspacePath = workspacePath.substring(0, workspacePath.length() - 1); }/*from w ww . j a v a 2s. c o m*/ String projPath = workspacePath + File.separator + projectName; currentWorkingPath = workspacePath; FileUtils.forceMkdir(new File(projPath)); FileUtils.forceMkdir(new File(projPath + File.separator + "export")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets")); FileUtils.forceMkdir(new File(projPath + File.separator + "scenes")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/images")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/particles")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/animations")); FileUtils.forceMkdir(new File(projPath + File.separator + "assets/orig/pack")); // create project file ProjectVO projVo = new ProjectVO(); projVo.projectName = projectName; projVo.projectVersion = AppConfig.getInstance().version; // create project info file ProjectInfoVO projInfoVo = new ProjectInfoVO(); projInfoVo.originalResolution.name = "orig"; projInfoVo.originalResolution.width = width; projInfoVo.originalResolution.height = height; //TODO: add project orig resolution setting currentProjectVO = projVo; currentProjectInfoVO = projInfoVo; sceneDataManager.createNewScene("MainScene"); FileUtils.writeStringToFile(new File(projPath + "/project.pit"), projVo.constructJsonString(), "utf-8"); FileUtils.writeStringToFile(new File(projPath + "/project.dt"), projInfoVo.constructJsonString(), "utf-8"); }
From source file:com.wavemaker.common.util.IOUtils.java
/** * Copy from: file to file, directory to directory, file to directory. * //from w ww. j av a 2s . c o m * @param source File object representing a file or directory to copy from. * @param destination File object representing the target; can only represent a file if the source is a file. * @param excludes A list of exclusion filenames. * @throws IOException */ public static void copy(File source, File destination, List<String> excludes) throws IOException { if (!source.exists()) { throw new IOException("Can't copy from non-existent file: " + source.getAbsolutePath()); } else if (excludes.contains(source.getName())) { return; } if (source.isDirectory()) { if (!destination.exists()) { FileUtils.forceMkdir(destination); } if (!destination.isDirectory()) { throw new IOException("Can't copy directory (" + source.getAbsolutePath() + ") to non-directory: " + destination.getAbsolutePath()); } File files[] = source.listFiles(new java.io.FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.indexOf("#") == -1 && name.indexOf("~") == -1; } }); for (int i = 0; i < files.length; i++) { copy(files[i], new File(destination, files[i].getName()), excludes); } } else if (source.isFile()) { if (destination.isDirectory()) { destination = new File(destination, source.getName()); } InputStream in = new FileInputStream(source); OutputStream out = new FileOutputStream(destination); copy(in, out); in.close(); out.close(); } else { throw new IOException( "Don't know how to copy " + source.getAbsolutePath() + "; it's neither a directory nor a file"); } }
From source file:com.github.brandtg.switchboard.LevelDbBasedLogIndex.java
@Override public void init(Properties config) { // This directory will contain all collection LevelDB files dbRoot = new File(config.getProperty(PROP_DB_ROOT, DEFAULT_DB_ROOT)); try {/* w w w .jav a 2 s. co m*/ FileUtils.forceMkdir(dbRoot); } catch (IOException e) { throw new IllegalStateException("Could not create " + dbRoot, e); } // Level DB options dbOptions = new Options(); dbOptions.createIfMissing(true); // TODO: Expose more options via properties }
From source file:com.baifendian.swordfish.common.hadoop.HdfsClient.java
/** * hdfs ?//from w w w . j a va2 s. c o m * * @param hdfsFile hdfs * @param localFile * @param overwrite ? */ public void readFile(String hdfsFile, String localFile, boolean overwrite) throws HdfsException { // Path pathObject = new Path(hdfsFile); File fileObject = new File(localFile); try { if (!isFile(pathObject)) { throw new HdfsException("File " + hdfsFile + " is not a valid file"); } // ???? if (!overwrite && fileObject.exists()) { LOGGER.info("{} has exist, do not overwrite", localFile); return; } // ?? File parentPath = fileObject.getParentFile(); if (parentPath != null && !parentPath.exists()) { FileUtils.forceMkdir(parentPath); } } catch (IOException e) { LOGGER.error("Operator Hdfs exception", e); throw new HdfsException("Operator Hdfs exception", e); } try (FSDataInputStream in = fileSystem.open(pathObject); OutputStream out = new BufferedOutputStream(new FileOutputStream(fileObject));) { byte[] b = new byte[1024]; int numBytes = 0; while ((numBytes = in.read(b)) > 0) { out.write(b, 0, numBytes); } out.flush(); } catch (IOException e) { LOGGER.error("Operator Hdfs exception", e); throw new HdfsException("Operator Hdfs exception", e); } }
From source file:de.awtools.basic.file.AWToolsFileUtilsTest.java
@Before public void setUp() { try {/*from w w w .j av a 2 s. co m*/ FileUtils.forceMkdir(new File(TMP_DIR)); } catch (IOException ex) { // ignore } }
From source file:com.alibaba.jstorm.cluster.StormConfig.java
/** * Return supervisor's pid dir// ww w .j a va 2 s . c o m * * @param conf * @return * @throws IOException */ public static String supervisorPids(Map conf) throws IOException { String ret = supervisor_local_dir(conf) + FILE_SEPERATEOR + "pids"; try { FileUtils.forceMkdir(new File(ret)); } catch (IOException e) { LOG.error("Failed to create dir " + ret, e); throw e; } return ret; }
From source file:hoot.services.controllers.info.ReportsResourceTest.java
@Test @Category(UnitTest.class) public void testGetReportFile() throws Exception { String storePath = _rps._homeFolder + "/" + _rps._rptStorePath; File f = new File(storePath); File fWks = new File(storePath + "/123_test_file"); if (fWks.exists()) { FileUtils.forceDelete(fWks);//from w w w. j av a 2 s. co m } FileUtils.forceMkdir(f); FileUtils.forceMkdir(fWks); String currTime = "" + System.currentTimeMillis(); JSONObject metaData = new JSONObject(); metaData.put("name", "Test Report1"); metaData.put("description", "This is test report 1"); metaData.put("created", currTime); metaData.put("reportpath", _rps._homeFolder + "/test-files/test_report1.pdf"); File meta = new File(storePath + "/123_test_file/meta.data"); FileUtils.write(meta, metaData.toJSONString()); File fout = _rps._getReportFile("123_test_file"); assertNotNull(fout); fout = _rps._getReportFile("123_test_file_not_there"); assertNull(fout); FileUtils.forceDelete(fWks); }