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

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

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

File.isFile介绍

[英]Indicates if this file represents a file on the underlying file system.
[中]指示此文件是否表示基础文件系统上的文件

代码示例

canonical example by Tabnine

public long getDirectorySize(File file) {
 if (!file.exists()) {
  return 0;
 }
 if (file.isFile()) {
  return file.length();
 }
 File[] files;
 if (!file.isDirectory() || (files = file.listFiles()) == null) {
  return 0;
 }
 return Arrays.stream(files).mapToLong(f -> getDirectorySize(f)).sum();
}

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

File folder = new File("your/path");
File[] listOfFiles = folder.listFiles();

  for (int i = 0; i < listOfFiles.length; i++) {
   if (listOfFiles[i].isFile()) {
    System.out.println("File " + listOfFiles[i].getName());
   } else if (listOfFiles[i].isDirectory()) {
    System.out.println("Directory " + listOfFiles[i].getName());
   }
  }

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

public int compare(File file1, File file2) {
    if (file1.isFile() && file2.isDirectory()) {
      return order;
    }
    if (file1.isDirectory() && file2.isFile()) {
      return -order;
    }
    return 0;
  }
}

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

/**
 * Returns <code>true</code> if file exists.
 */
public static boolean isExistingFile(File file) {
  if (file == null) {
    return false;
  }
  return file.exists() && file.isFile();
}

代码示例来源:origin: hankcs/HanLP

/**
   * 本地文件是否存在
   * @param path
   * @return
   */
  public static boolean isFileExisted(String path)
  {
    File file = new File(path);
    return file.isFile() && file.exists();
  }
}

代码示例来源:origin: eclipse-vertx/vert.x

private File getJava() {
 File java;
 File home = new File(System.getProperty("java.home"));
 if (ExecUtils.isWindows()) {
  java = new File(home, "bin/java.exe");
 } else {
  java = new File(home, "bin/java");
 }
 if (!java.isFile()) {
  out.println("Cannot find java executable - " + java.getAbsolutePath() + " does not exist");
  ExecUtils.exitBecauseOfSystemConfigurationIssue();
 }
 return java;
}

代码示例来源:origin: oracle/opengrok

@Override
boolean isRepositoryFor(File file, boolean interactive) {
  if (file.isDirectory()) {
    File f = new File(file, MYSCMSERVERINFO_FILE);
    return f.exists() && f.isFile();
  }
  return false;
}

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

@Test
public void shouldZipFileAndUnzipIt() throws IOException {
  zipFile = zipUtil.zip(srcDir, temporaryFolder.newFile(), Deflater.NO_COMPRESSION);
  assertThat(zipFile.isFile(), is(true));
  zipUtil.unzip(zipFile, destDir);
  File baseDir = new File(destDir, srcDir.getName());
  assertIsDirectory(new File(baseDir, emptyDir.getName()));
  assertIsDirectory(new File(baseDir, childDir1.getName()));
  File actual1 = new File(baseDir, file1.getName());
  assertThat(actual1.isFile(), is(true));
  assertThat(fileContent(actual1), is(fileContent(file1)));
  File actual2 = new File(baseDir, childDir1.getName() + File.separator + file2.getName());
  assertThat(actual2.isFile(), is(true));
  assertThat(fileContent(actual2), is(fileContent(file2)));
}

代码示例来源:origin: Tencent/tinker

public static final boolean deleteDir(File file) {
  if (file == null || (!file.exists())) {
    return false;
  }
  if (file.isFile()) {
    file.delete();
  } else if (file.isDirectory()) {
    File[] files = file.listFiles();
    for (int i = 0; i < files.length; i++) {
      deleteDir(files[i]);
    }
  }
  file.delete();
  return true;
}

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

/**
 * Returns the log file.
 * @return The file may reference both uncompressed or compressed logs
 */  
public @Nonnull File getLogFile() {
  File rawF = new File(getRootDir(), "log");
  if (rawF.isFile()) {
    return rawF;
  }
  File gzF = new File(getRootDir(), "log.gz");
  if (gzF.isFile()) {
    return gzF;
  }
  //If both fail, return the standard, uncompressed log file
  return rawF;
}

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

/** Processes the specified input file or directory.
 * @param outputRoot May be null if there is no output from processing the files.
 * @return the processed files added with {@link #addProcessedFile(Entry)}. */
public ArrayList<Entry> process (File inputFileOrDir, File outputRoot) throws Exception {
  if (!inputFileOrDir.exists())
    throw new IllegalArgumentException("Input file does not exist: " + inputFileOrDir.getAbsolutePath());
  if (inputFileOrDir.isFile())
    return process(new File[] {inputFileOrDir}, outputRoot);
  else
    return process(inputFileOrDir.listFiles(), outputRoot);
}

代码示例来源:origin: spockframework/spock

/**
 * Returns the class file for the given class (which has been verified to exist in the returned location),
 * or null if the class file could not be found (e.g. because it is contained in a Jar).
 */
public static File getClassFile(Class<?> clazz) {
 File dir = new File(clazz.getProtectionDomain().getCodeSource().getLocation().getPath());
 if (!dir.isDirectory()) return null; // class file might be contained in Jar
 File clazzFile = new File(dir, clazz.getName().replace('.', File.separatorChar) + ".class");
 return clazzFile.isFile() ? clazzFile : null;
}

代码示例来源: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: eclipse-vertx/vert.x

private static List<JavaFileObject> browseDir(String packageName, File directory) {
 List<JavaFileObject> result = new ArrayList<>();
 for (File childFile : directory.listFiles()) {
  if (childFile.isFile() && childFile.getName().endsWith(CLASS_FILE)) {
   String binaryName = packageName + "." + childFile.getName().replaceAll(CLASS_FILE + "$", "");
   result.add(new CustomJavaFileObject(childFile.toURI(), JavaFileObject.Kind.CLASS, binaryName));
  }
 }
 return result;
}

代码示例来源:origin: apache/storm

private void checkFilesExist(Collection<File> dependencies) {
    for (File dependency : dependencies) {
      if (!dependency.isFile() || !dependency.exists()) {
        throw new FileNotAvailableException(dependency.getAbsolutePath());
      }
    }
  }
}

代码示例来源:origin: hs-web/hsweb-framework

public static FileInfo from(File file) {
    FileInfo info = new FileInfo();
    info.setName(file.getName());
    info.setLength(file.length());
    info.setParent(file.getParent());
    info.setAbsolutePath(file.getAbsolutePath());
    info.setFile(file.isFile());
    info.setDir(file.isDirectory());
    return info;
  }
}

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

public int compare(File file1, File file2) {
    if (file1.isDirectory() && file2.isDirectory() || file1.isFile() && file2.isFile()) {
      return file1.getName().compareTo(file2.getName());
    } else {
      return file1.isDirectory() ? -1 : 1;
    }
  }
}

代码示例来源:origin: apache/incubator-druid

@Override
 public boolean accept(File pathname)
 {
  return pathname.exists()
      && pathname.isFile()
      && (pattern == null || pattern.matcher(pathname.getName()).matches());
 }
}

代码示例来源:origin: hankcs/HanLP

@Override
  public boolean accept(File pathname)
  {
    return pathname.isFile() && pathname.getName().endsWith(".txt");
  }
});

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

public static void delete(File file) throws IOException {
  if (file.isDirectory()) {
    for (File f : file.listFiles()) {
      delete(f);
    }
  }
  if (file.isDirectory() || file.isFile()) {
    if (!file.delete()) {
      throw new IllegalStateException("Deletion of " + file.getPath() + " failed");
    }
  }
}

相关文章

微信公众号

最新文章

更多