List of usage examples for java.io File getParent
public String getParent()
null
if this pathname does not name a parent directory. From source file:com.att.aro.core.fileio.impl.FileManagerImpl.java
/** * rename a File with a newName/*w ww. j ava 2s. com*/ * * @param origFileName * a File * @param newName * a String containing a new name * @return */ @Override public boolean renameFile(File origFileName, String newName) { String path = origFileName.getParent(); File renameFile = createFile(path, newName); return (!renameFile.exists() && origFileName.renameTo(renameFile)); }
From source file:net.sf.jasperreports.samples.wizards.SampleNewWizard.java
@Override protected void createProject(IProgressMonitor monitor, IProject prj) throws CoreException, JavaModelException { Set<String> cpaths = new HashSet<String>(); Set<String> lpaths = new HashSet<String>(); super.createProject(monitor, prj); File copyto = prj.getLocation().toFile(); Set<URL> paths = Activator.getSamplesManager().getURLs(); Enumeration<?> en = Activator.getDefault().getBundle().findEntries("resources", "*", true); //$NON-NLS-1$ //$NON-NLS-2$ while (en.hasMoreElements()) paths.add((URL) en.nextElement()); en = JasperReportsPlugin.getDefault().getBundle().findEntries("lib", "*.jar", true); //$NON-NLS-1$ //$NON-NLS-2$ while (en.hasMoreElements()) paths.add((URL) en.nextElement()); for (URL url : paths) { OutputStream out = null;/*from w ww . j av a 2 s .c o m*/ InputStream in = null; if (url.getFile().endsWith(".zip")) { File zip = null; try { in = new BufferedInputStream(url.openStream(), 1024); zip = File.createTempFile("arc", ".zip", copyto); out = new BufferedOutputStream(new FileOutputStream(zip)); FileUtils.copyInputStream(in, out); unpackArchive(zip, copyto, monitor, cpaths, lpaths); } catch (IOException e) { e.printStackTrace(); } finally { FileUtils.closeStream(in); FileUtils.closeStream(out); if (zip != null) zip.delete(); } } else { String path = url.getPath(); File file = new File(copyto, File.separator + path); new File(file.getParent()).mkdirs(); String fname = file.getName(); try { org.apache.commons.io.FileUtils.copyURLToFile(url, file); if (file.getParentFile().getName().equals("src")) cpaths.add(path.substring(0, path.lastIndexOf("/"))); if (file.getParentFile().getParentFile().getName().equals("lib") && (fname.endsWith(".jar") || fname.endsWith(".zip"))) lpaths.add(path); if (file.getParentFile().getName().equals("lib") && (fname.endsWith(".jar") || fname.endsWith(".zip"))) lpaths.add(path); } catch (Exception e) { // e.printStackTrace(); } } if (monitor.isCanceled()) return; } IJavaProject project = JavaCore.create(prj); addSourceFolders(cpaths, project, monitor); addLibraries(lpaths, project, monitor); prj.refreshLocal(IProject.DEPTH_INFINITE, monitor); prj.build(IncrementalProjectBuilder.FULL_BUILD, monitor); prj.close(monitor); prj.open(monitor); }
From source file:com.mobileman.projecth.business.patient.PatientQuestionAnswerServiceTest.java
/** * @throws Exception//from www .java2s . co m */ @Test public void saveFileEntryAnswer() throws Exception { DateFormat dirNameDateFormat = new SimpleDateFormat("yyyy_MM"); DateFormat fileNameDateFormat = new SimpleDateFormat("hh_mm_ss"); Disease psoriasis = diseaseService.findByCode(DiseaseCodes.PSORIASIS_CODE); List<Haq> haqs = haqService.findByDisease(psoriasis.getId()); Haq haq1 = haqs.get(0); assertEquals(1, haq1.getQuestions().size()); Question question = haq1.getQuestions().get(0); Answer noAnswer = question.getQuestionType().getAnswers().get(0); assertFalse(noAnswer.isActive()); Answer fileAnswer = question.getQuestionType().getAnswers().get(1); assertTrue(fileAnswer.isActive()); User patient = userService.findUserByLogin("sysuser1"); assertNotNull(patient); DateFormat dateFormat = DateFormat.getDateInstance(); Date logDate = dateFormat.parse("1.1.2011"); File tmpFile = File.createTempFile("projecth", ".test"); configurationService.setImagesRootDirectoryPath(tmpFile.getParent()); int count = patientQuestionAnswerService.findAll().size(); patientQuestionAnswerService.saveAnswer(patient.getId(), question.getId(), noAnswer.getId(), tmpFile.getPath(), logDate); List<PatientQuestionAnswer> answers = patientQuestionAnswerService.findAll(); assertEquals(count + 1, answers.size()); assertEquals(null, answers.get(answers.size() - 1).getCustomAnswer()); /////////////// POSITIVE patientQuestionAnswerService.saveAnswer(patient.getId(), question.getId(), fileAnswer.getId(), tmpFile.getPath(), logDate); answers = patientQuestionAnswerService.findAll(); assertEquals(count + 2, answers.size()); assertEquals( patient.getId() + File.separator + psoriasis.getId() + File.separator + dirNameDateFormat.format(new Date()) + File.separator + fileNameDateFormat.format(new Date()) + ".test", answers.get(answers.size() - 1).getCustomAnswer()); }
From source file:eu.impact_project.iif.t2.client.WorkflowRunnerTest.java
/** * Test of doPost method, of class WorkflowRunner. *//*w w w. j a v a 2 s . c om*/ @Test public void testDoPost() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); ServletConfig config = mock(ServletConfig.class); ServletContext context = mock(ServletContext.class); RequestDispatcher dispatcher = mock(RequestDispatcher.class); ServletOutputStream stream = mock(ServletOutputStream.class); HttpSession session = mock(HttpSession.class); when(request.getSession(true)).thenReturn(session); ArrayList<Workflow> flowList = new ArrayList<>(); Workflow flow = new Workflow(); flow.setStringVersion("Esto es una prueba"); flow.setWsdls("<wsdl>http://www.ua.es</wsdl>"); flow.setUrls("http://www.ua.es"); ArrayList<WorkflowInput> flowInputs = new ArrayList<>(); WorkflowInput input = new WorkflowInput("pru0Input"); input.setDepth(1); flowInputs.add(input); input = new WorkflowInput("pru1Input"); input.setDepth(0); flowInputs.add(input); flow.setInputs(flowInputs); flowList.add(flow); when(session.getAttribute("workflows")).thenReturn(flowList); when(config.getServletContext()).thenReturn(context); URL url = this.getClass().getResource("/config.properties"); File testFile = new File(url.getFile()); when(context.getRealPath("/")).thenReturn(testFile.getParent() + "/"); Part[] parts = new Part[] { new StringPart("user", "user"), new StringPart("pass", "pass"), new StringPart("workflow0pru0Input", "prueba0"), new StringPart("workflow0pru0Input0", "prueba0.0"), new StringPart("workflow0pru1Input", "prueba1") }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); final ByteArrayInputStream inputContent = new ByteArrayInputStream(requestContent.toByteArray()); when(request.getInputStream()).thenReturn(new ServletInputStream() { @Override public int read() throws IOException { return inputContent.read(); } }); when(request.getContentType()).thenReturn(multipartRequestEntity.getContentType()); WorkflowRunner runer = new WorkflowRunner(); try { runer.init(config); runer.doPost(request, response); } catch (ServletException ex) { fail("Should not raise exception " + ex.toString()); } catch (IOException ex) { fail("Should not raise exception " + ex.toString()); } catch (NullPointerException ex) { //ok no funciona el server de taverna } }
From source file:eu.impact_project.iif.t2.client.WorkflowRunnerTest.java
/** * Test of doPost method, of class WorkflowRunner. *///from w ww .ja va2s .com @Test public void testDoPostURLFail() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); HttpServletResponse response = mock(HttpServletResponse.class); ServletConfig config = mock(ServletConfig.class); ServletContext context = mock(ServletContext.class); RequestDispatcher dispatcher = mock(RequestDispatcher.class); ServletOutputStream stream = mock(ServletOutputStream.class); HttpSession session = mock(HttpSession.class); when(request.getSession(true)).thenReturn(session); ArrayList<Workflow> flowList = new ArrayList<>(); Workflow flow = new Workflow(); flow.setStringVersion("Esto es una prueba"); flow.setWsdls("<wsdl>http://www.ua.es</wsdl>"); flow.setUrls("http://falsa.es"); ArrayList<WorkflowInput> flowInputs = new ArrayList<>(); WorkflowInput input = new WorkflowInput("pru0Input"); input.setDepth(1); flowInputs.add(input); input = new WorkflowInput("pru1Input"); input.setDepth(0); flowInputs.add(input); flow.setInputs(flowInputs); flowList.add(flow); when(session.getAttribute("workflows")).thenReturn(flowList); when(config.getServletContext()).thenReturn(context); URL url = this.getClass().getResource("/config.properties"); File testFile = new File(url.getFile()); when(context.getRealPath("/")).thenReturn(testFile.getParent() + "/"); Part[] parts = new Part[] { new StringPart("user", "user"), new StringPart("pass", "pass"), new StringPart("workflow0pru0Input", "prueba0"), new StringPart("workflow0pru0Input0", "prueba0.0"), new StringPart("workflow0pru1Input", "prueba1") }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); final ByteArrayInputStream inputContent = new ByteArrayInputStream(requestContent.toByteArray()); when(request.getInputStream()).thenReturn(new ServletInputStream() { @Override public int read() throws IOException { return inputContent.read(); } }); when(request.getContentType()).thenReturn(multipartRequestEntity.getContentType()); WorkflowRunner runer = new WorkflowRunner(); try { runer.init(config); runer.doPost(request, response); } catch (ServletException ex) { fail("Should not raise exception " + ex.toString()); } catch (IOException ex) { fail("Should not raise exception " + ex.toString()); } catch (NullPointerException ex) { //ok no funciona el server de taverna } }
From source file:eionet.gdem.utils.Utils.java
/** * Deletes the folder, where specified file locates. * * @param filePath// w w w .j a v a 2 s. co m */ public static void deleteParentFolder(String filePath) { File file = new File(filePath); String folder = file.getParent(); // check if the folder is not Properties.tmpFolder File oFolder = new File(folder); File oTmpFolder = new File(Properties.tmpFolder); // if parent folder is system tmp folder, then delete only the // specifieds file if (oFolder.equals(oTmpFolder)) { deleteFile(filePath); } else { deleteFolder(folder); } }
From source file:net.lightbody.bmp.proxy.jetty.util.RolloverFileOutputStream.java
private void removeOldFiles() { if (_retainDays > 0) { Calendar retainDate = Calendar.getInstance(); retainDate.add(Calendar.DATE, -_retainDays); int borderYear = retainDate.get(java.util.Calendar.YEAR); int borderMonth = retainDate.get(java.util.Calendar.MONTH) + 1; int borderDay = retainDate.get(java.util.Calendar.DAY_OF_MONTH); File file = new File(_filename); File dir = new File(file.getParent()); String fn = file.getName(); int s = fn.toLowerCase().indexOf(YYYY_MM_DD); if (s < 0) return; String prefix = fn.substring(0, s); String suffix = fn.substring(s + YYYY_MM_DD.length()); String[] logList = dir.list(); for (int i = 0; i < logList.length; i++) { fn = logList[i];/* w w w. j a va2 s. co m*/ if (fn.startsWith(prefix) && fn.indexOf(suffix, prefix.length()) >= 0) { try { StringTokenizer st = new StringTokenizer( fn.substring(prefix.length(), prefix.length() + YYYY_MM_DD.length()), "_."); int nYear = Integer.parseInt(st.nextToken()); int nMonth = Integer.parseInt(st.nextToken()); int nDay = Integer.parseInt(st.nextToken()); if (nYear < borderYear || (nYear == borderYear && nMonth < borderMonth) || (nYear == borderYear && nMonth == borderMonth && nDay <= borderDay)) { log.info("Log age " + fn); new File(dir, fn).delete(); } } catch (Exception e) { if (log.isDebugEnabled()) e.printStackTrace(); } } } } }
From source file:com.moviejukebox.tools.FileTools.java
/** * Find the parent directory of the movie file. * * @param movieFile//from w w w . ja v a2 s .c om * @return Parent folder * @author Stuart Boston */ public static String getParentFolder(File movieFile) { String parentFolder; if (movieFile.isDirectory()) { // for VIDEO_TS parentFolder = movieFile.getPath(); } else { parentFolder = movieFile.getParent(); } // Issue 1070, /BDMV/STREAM is being appended to the parent path if (parentFolder.toUpperCase().endsWith(BDMV_STREAM)) { parentFolder = parentFolder.substring(0, parentFolder.length() - 12); } return parentFolder; }
From source file:info.magnolia.test.RepositoryTestCase.java
protected void extractConfigFile(String propertyName, InputStream configFileStream, String extractToPath) throws Exception { String targetFilename = Path.getAbsoluteFileSystemPath(extractToPath); File targetFile = new File(targetFilename); // extract resource to the filesystem (jackrabbit can't use a stream) new File(targetFile.getParent()).mkdirs(); IOUtils.copy(configFileStream, new FileOutputStream(targetFile)); SystemProperty.setProperty(propertyName, extractToPath); }
From source file:bio.igm.utils.init.ReduceConstructs.java
public ReduceConstructs(String _path, String _ptes_path, String _can_path, int _segment_size) throws IOException { this.path = _path; File f = new File(_path); try {//from w w w. j ava 2s . c o m if (f.isDirectory()) { LOG = new Logging(_path, ReduceConstructs.class.getName()).setup(); } else { LOG = new Logging(f.getParent(), ReduceConstructs.class.getName()).setup(); } } catch (IOException ex) { Logger.getLogger(ReduceConstructs.class.getName()).log(Level.SEVERE, null, ex); } this.segment_size = _segment_size; LOG.info("Reading supplied reference FASTA files.."); File p = new File(_ptes_path); if (p.exists()) { constructs = readConstructs(_ptes_path); } else { LOG.info("No PTES FASTA reference supplied - running unguided analysis.."); } File c = new File(_can_path); if (c.exists()) { can_constructs = readConstructs(_can_path); } else { LOG.info("No Canonical Junctions FASTA reference supplied"); } LOG.info("Shrinking constructs to specified segment length.."); reduce_constructs("ptes"); reduce_constructs("canonical"); }