java.io.File.getPath()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(9.9k)|赞(0)|评价(0)|浏览(194)

本文整理了Java中java.io.File.getPath()方法的一些代码示例,展示了File.getPath()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。File.getPath()方法的具体详情如下:
包路径:java.io.File
类名称:File
方法名:getPath

File.getPath介绍

[英]Returns the path of this file.
[中]返回此文件的路径。

代码示例

代码示例来源:origin: libgdx/libgdx

public File file () {
    if (type == FileType.External) return new File(HeadlessFiles.externalPath, file.getPath());
    if (type == FileType.Local) return new File(HeadlessFiles.localPath, file.getPath());
    return file;
  }
}

代码示例来源:origin: jenkinsci/jenkins

@Override
    public String call() throws IOException {
      File exe = getExeFile("mvn");
      if(exe.exists())
        return exe.getPath();
      exe = getExeFile("maven");
      if(exe.exists())
        return exe.getPath();
      return null;
    }
}

代码示例来源:origin: gocd/gocd

public static String configuration(String warFile) {
    if (new File(warFile).isDirectory()) {
      return new File(warFile, webdefaultXml).getPath();
    }
    return "jar:file:" + warFile + "!/" + webdefaultXml;
  }
}

代码示例来源:origin: wildfly/wildfly

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");
}

代码示例来源:origin: GitLqr/LQRWeChat

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();
}

代码示例来源:origin: libgdx/libgdx

/** 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);
  }
}

代码示例来源:origin: stackoverflow.com

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) {}
  }
}

代码示例来源:origin: iBotPeaches/Apktool

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);
    }
  }
}

代码示例来源:origin: stanfordnlp/CoreNLP

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);
}

代码示例来源:origin: stackoverflow.com

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);
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * 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');
  }
}

代码示例来源:origin: libgdx/libgdx

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);

代码示例来源:origin: stackoverflow.com

// 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".

代码示例来源:origin: neo4j/neo4j

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 );
  }
}

代码示例来源:origin: lets-blade/blade

@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));
}

代码示例来源:origin: javamelody/javamelody

private static void mkdirs(File directory) {
  if (!directory.exists() && !directory.mkdirs()) {
    throw new IllegalStateException("Can't create directory " + directory.getPath());
  }
}

代码示例来源:origin: SonarSource/sonarqube

@Override
public FilePredicate is(File ioFile) {
 if (ioFile.isAbsolute()) {
  return hasAbsolutePath(ioFile.getAbsolutePath());
 }
 return hasRelativePath(ioFile.getPath());
}

代码示例来源:origin: stanfordnlp/CoreNLP

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;
}

代码示例来源:origin: jenkinsci/jenkins

/**
 * 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);
}

代码示例来源:origin: wildfly/wildfly

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);
}

相关文章