public static String configuration(String warFile) { if (new File(warFile).isDirectory()) { return new File(warFile, webdefaultXml).getPath(); } return "jar:file:" + warFile + "!/" + webdefaultXml; } }
protected void createRootDir() { root_dir=new File(location); if(root_dir.exists()) { if(!root_dir.isDirectory()) throw new IllegalArgumentException("location " + root_dir.getPath() + " is not a directory"); } else root_dir.mkdirs(); if(!root_dir.exists()) throw new IllegalArgumentException("location " + root_dir.getPath() + " could not be accessed"); }
private static String saveBitmap(Bitmap bm, String imageUrlName) { File f = new File(SAVEADDRESS, imageUrlName); try { FileOutputStream out = new FileOutputStream(f); bm.compress(Bitmap.CompressFormat.JPEG, 100, out); out.flush(); out.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return SCHEMA + f.getPath(); }
/** Returns a stream for reading this file as bytes. * @throw RuntimeException if the file handle represents a directory, doesn't exist, or could not be read. */ public InputStream read () { if (type == FileType.Classpath && !file.exists()) { InputStream input = FileDescriptor.class.getResourceAsStream("/" + file.getPath().replace('\\', '/')); if (input == null) throw new RuntimeException("File not found: " + file + " (" + type + ")"); return input; } try { return new FileInputStream(file()); } catch (FileNotFoundException ex) { if (file().isDirectory()) throw new RuntimeException("Cannot open a stream to a directory: " + file + " (" + type + ")", ex); throw new RuntimeException("Error reading file: " + file + " (" + type + ")", ex); } }
import java.io.File; public class PathTesting { public static void main(String [] args) { File f = new File("test/.././file.txt"); System.out.println(f.getPath()); System.out.println(f.getAbsolutePath()); try { System.out.println(f.getCanonicalPath()); } catch(Exception e) {} } }
public static void cpdir(File src, File dest) throws BrutException { dest.mkdirs(); File[] files = src.listFiles(); for (int i = 0; i < files.length; i++) { File file = files[i]; File destFile = new File(dest.getPath() + File.separatorChar + file.getName()); if (file.isDirectory()) { cpdir(file, destFile); continue; } try { InputStream in = new FileInputStream(file); OutputStream out = new FileOutputStream(destFile); IOUtils.copy(in, out); in.close(); out.close(); } catch (IOException ex) { throw new BrutException("Could not copy file: " + file, ex); } } }
public static SimpleMatrix loadMatrix(String binaryName, String textName) throws IOException { File matrixFile = new File(binaryName); if (matrixFile.exists()) { return SimpleMatrix.loadBinary(matrixFile.getPath()); } matrixFile = new File(textName); if (matrixFile.exists()) { return NeuralUtils.loadTextMatrix(matrixFile); } throw new RuntimeException("Could not find either " + binaryName + " or " + textName); }
public void restartApplication() { final String javaBin = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java"; final File currentJar = new File(MyClassInTheJar.class.getProtectionDomain().getCodeSource().getLocation().toURI()); /* is it a jar file? */ if(!currentJar.getName().endsWith(".jar")) return; /* Build command: java -jar application.jar */ final ArrayList<String> command = new ArrayList<String>(); command.add(javaBin); command.add("-jar"); command.add(currentJar.getPath()); final ProcessBuilder builder = new ProcessBuilder(command); builder.start(); System.exit(0); }
/** * On Windows, jenkins.war is locked, so we place a new version under a special name, * which is picked up by the service wrapper upon restart. */ @Override public void rewriteHudsonWar(File by) throws IOException { File dest = getHudsonWar(); // this should be impossible given the canRewriteHudsonWar method, // but let's be defensive if(dest==null) throw new IOException("jenkins.war location is not known."); // backing up the old jenkins.war before its lost due to upgrading // unless we are trying to rewrite jenkins.war by a backup itself File bak = new File(dest.getPath() + ".bak"); if (!by.equals(bak)) FileUtils.copyFile(dest, bak); String baseName = dest.getName(); baseName = baseName.substring(0,baseName.indexOf('.')); File baseDir = getBaseDir(); File copyFiles = new File(baseDir,baseName+".copies"); try (FileWriter w = new FileWriter(copyFiles, true)) { w.write(by.getAbsolutePath() + '>' + getHudsonWar().getAbsolutePath() + '\n'); } }
boolean found = false; for (Pattern pattern : inputRegex) { if (pattern.matcher(file.getName()).matches()) { found = true; continue; if (inputFilter != null && !inputFilter.accept(dir, file.getName())) continue; String outputName = file.getName(); if (outputSuffix != null) outputName = outputName.replaceAll("(.*)\\..*", "$1") + outputSuffix; entry.outputFile = new File(outputRoot, outputName); } else { entry.outputFile = new File(outputDir, outputName); if (recursive && file.isDirectory()) { File subdir = outputDir.getPath().length() == 0 ? new File(file.getName()) : new File(outputDir, file.getName()); process(file.listFiles(inputFilter), outputRoot, subdir, dirToEntries, depth + 1);
// Prepare source somehow. String source = "package test; public class Test { static { System.out.println(\"hello\"); } public Test() { System.out.println(\"world\"); } }"; // Save source in .java file. File root = new File("/java"); // On Windows running on C:\, this is C:\java. File sourceFile = new File(root, "test/Test.java"); sourceFile.getParentFile().mkdirs(); Files.write(sourceFile.toPath(), source.getBytes(StandardCharsets.UTF_8)); // Compile source file. JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); compiler.run(null, null, null, sourceFile.getPath()); // Load and instantiate compiled class. URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() }); Class<?> cls = Class.forName("test.Test", true, classLoader); // Should print "hello". Object instance = cls.newInstance(); // Should print "world". System.out.println(instance); // Should print "test.Test@hashcode".
private static void mockFiles( String[] filenames, ArrayList<File> files, boolean isDirectories ) { for ( String filename : filenames ) { File file = mock( File.class ); String[] fileNameParts = filename.split( "/" ); when( file.getName() ).thenReturn( fileNameParts[fileNameParts.length - 1] ); when( file.isFile() ).thenReturn( !isDirectories ); when( file.isDirectory() ).thenReturn( isDirectories ); when( file.exists() ).thenReturn( true ); when( file.getPath() ).thenReturn( filename ); files.add( file ); } }
@Override public void download(@NonNull String fileName, @NonNull File file) throws Exception { if (!file.exists() || !file.isFile()) { throw new NotFoundException("Not found file: " + file.getPath()); } String contentType = StringKit.mimeType(file.getName()); headers.put("Content-Disposition", "attachment; filename=" + new String(fileName.getBytes("UTF-8"), "ISO8859_1")); headers.put(HttpConst.CONTENT_LENGTH.toString(), String.valueOf(file.length())); headers.put(HttpConst.CONTENT_TYPE_STRING, contentType); this.body = new StreamBody(new FileInputStream(file)); }
@Override public FilePredicate is(File ioFile) { if (ioFile.isAbsolute()) { return hasAbsolutePath(ioFile.getAbsolutePath()); } return hasRelativePath(ioFile.getPath()); }
private Tree primeNextTree() { Tree t = null; try { t = tr.readTree(); if(t == null && primeNextFile()) //Current file is exhausted t = tr.readTree(); //Associate this tree with a file and line number if(t != null && t.label() != null && t.label() instanceof HasIndex) { HasIndex lab = (HasIndex) t.label(); lab.setSentIndex(curLineId++); lab.setDocID(currentFile.getName()); } } catch (IOException e) { System.err.printf("%s: Error reading from file %s:%n%s%n", this.getClass().getName(), currentFile.getPath(), e.toString()); throw new RuntimeException(e); } return t; }
/** * Checks if the home directory is valid. * @since 1.563 */ public FormValidation doCheckHome(@QueryParameter File value) { // this can be used to check the existence of a file on the server, so needs to be protected Jenkins.getInstance().checkPermission(Jenkins.ADMINISTER); if (value.getPath().isEmpty()) { return FormValidation.ok(); } if (!value.isDirectory()) { return FormValidation.warning(Messages.ToolDescriptor_NotADirectory(value)); } return checkHomeDirectory(value); }
protected void createDiskCacheFile() throws IOException { root_dir=new File(this.cache_dir); if(root_dir.exists()) { if(!root_dir.isDirectory()) throw new IllegalArgumentException("location " + root_dir.getPath() + " is not a directory"); } else { root_dir.mkdirs(); } if(!root_dir.exists()) throw new IllegalArgumentException("location " + root_dir.getPath() + " could not be accessed"); filter=(dir, name1) -> name1.endsWith(SUFFIX); }