Java Code Examples for java.io.File#isFile()

The following examples show how to use java.io.File#isFile() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private String readPidFile(Context context, String path) {
    String pid = "";

    File file = new File(path);
    if (file.isFile()) {
        List<String> lines = FileOperations.readTextFileSynchronous(context, path);

        for (String line : lines) {
            if (!line.trim().isEmpty()) {
                pid = line.trim();
                break;
            }
        }
    }
    return pid;
}
 
Example 2
Source Project: netbeans   File: ParserServletContext.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Return the set of resource paths for the "directory" at the
 * specified context path.
 *
 * @param path Context-relative base path
 */
public Set<String> getResourcePaths(String path) {
    
    LOGGER.log(Level.FINE,  "getResourcePaths({0})", path);
    Set<String> thePaths = new HashSet<String>();
    if (!path.endsWith("/"))
        path += "/";
    String basePath = getRealPath(path);
    if (basePath == null)
        return (thePaths);
    File theBaseDir = new File(basePath);
    if (!theBaseDir.exists() || !theBaseDir.isDirectory())
        return (thePaths);
    String theFiles[] = theBaseDir.list();
    for (int i = 0; i < theFiles.length; i++) {
        File testFile = new File(basePath + File.separator + theFiles[i]);
        if (testFile.isFile())
            thePaths.add(path + theFiles[i]);
        else if (testFile.isDirectory())
            thePaths.add(path + theFiles[i] + "/");
    }
    return thePaths;
    
}
 
Example 3
@NotNull
private String getExecutablePath() throws RunBuildException {
    final String path = getSonarScannerRoot();

    final String execName = myOsType == WINDOWS ? "sonar-runner.bat" : "sonar-runner";

    final File exec = new File(path + File.separatorChar + "bin" + File.separatorChar + execName);

    if (!exec.exists()) {
        throw new RunBuildException("SonarQube executable doesn't exist: " + exec.getAbsolutePath());
    }
    if (!exec.isFile()) {
        throw new RunBuildException("SonarQube executable is not a file: " + exec.getAbsolutePath());
    }
    return exec.getAbsolutePath();
}
 
Example 4
private void deleteDirectoryAndFile(File delFile) {
    if (delFile == null || !delFile.exists()) {
        return;
    }
    if (delFile.isFile()) {
        // ファイル削除
        if (delFile.exists() && !delFile.delete()) {
            delFile.deleteOnExit();
        }
    } else {
        // ディレクトリの場合、再帰する
        File[] list = delFile.listFiles();
        for (int i = 0; i < list.length; i++) {
            deleteDirectoryAndFile(list[i]);
        }
        if (delFile.exists() && !delFile.delete()) {
            delFile.deleteOnExit();
        }
    }
}
 
Example 5
Source Project: Android-utils   File: FileUtils.java    License: Apache License 2.0 6 votes vote down vote up
public static boolean deleteFilesInDirWithFilter(final File dir, final FileFilter filter) {
    if (dir == null) return false;
    // dir doesn't exist then return true
    if (!dir.exists()) return true;
    // dir isn't a directory then return false
    if (!dir.isDirectory()) return false;
    File[] files = dir.listFiles();
    if (files != null && files.length != 0) {
        for (File file : files) {
            if (filter.accept(file)) {
                if (file.isFile()) {
                    if (!file.delete()) return false;
                } else if (file.isDirectory()) {
                    if (!deleteDir(file)) return false;
                }
            }
        }
    }
    return true;
}
 
Example 6
/**
 * 读取整个目录下所有子文件的内容至String中.
 *
 * @param dir the dir
 * @param encoding the encoding
 * @return the string
 * @throws IOException 
 */
public static String readEntireDirectoryContent(File dir, String encoding) throws IOException {
    List<File> files = searchAllNotIgnoreFile(dir);
    StringBuffer result = new StringBuffer();
    for (File file : files) {
        if (file.isDirectory())
            continue;
        if (file.isFile() && file.exists()) {
            result.append(IOHelper.readFile(file, encoding));
        }
    }
    return result.toString();
}
 
Example 7
Source Project: netbeans   File: MakeJNLPTest.java    License: Apache License 2.0 5 votes vote down vote up
private static void findFilenames(File dir, String prefix, Set<String> names) {
    for (File f : dir.listFiles()) {
        if (f.isFile()) {
            names.add(prefix + f.getName());
        } else if (f.isDirectory()) {
            findFilenames(f, prefix + f.getName() + "/", names);
        }
    }
}
 
Example 8
public static void main (String[] args){
	
	int i=0;
	
	ParseCompileSummary report = new ParseCompileSummary();
	
	/* Get xml result folder */	
	
	File[] xmlReports = getTestResult(args[0]);
	String outputPath = args[1];
	countWarning = args[2];
	countError = args[3];
	
	int reportCount = xmlReports.length;
	
	/*parse arguments*/
	
	for(i=0;i<reportCount;i++){
		File ResultFileFolder = xmlReports[i];
		if (ResultFileFolder.isFile())
			continue;
		File[] files  = ResultFileFolder.listFiles( new SuffixFileFilter(".xml") );
		/* read junit test reports*/
		PluginName = ResultFileFolder.getName();
		System.out.println("Processing " + PluginName + "..." );

		for (int j=0;j<files.length;++j){
							
			ResultFile = files[j];
			report.getTestResult();
			report.wrtTestReport( outputPath );
		}
	}
	
}
 
Example 9
public CommandLineToolSearchResult locate(ToolType key, String exeName) {
    CommandLineToolSearchResult result = executables.get(exeName);
    if (result == null) {
        File exe = findExecutable(operatingSystem, exeName);
        result = exe == null || !exe.isFile() ? new MissingTool(key, exeName, pathEntries) : new FoundTool(exe);
        executables.put(exeName, result);
    }
    return result;
}
 
Example 10
private void restoreGoModFromBackupAndRemoveBackup(@Nonnull final File folder) throws IOException {
  final Collection<File> backupFiles = FileUtils.listFiles(folder, FileFilterUtils.nameFileFilter(GO_MOD_FILE_NAME_BAK), TrueFileFilter.INSTANCE);

  this.getLog().debug(String.format("Restoring go.mod from backup in %s, detected %d files", folder, backupFiles.size()));

  for (final File backup : backupFiles) {
    final File restored = new File(folder, GO_MOD_FILE_NAME);
    if (restored.isFile() && !restored.delete()) {
      throw new IOException("Can't delete file during backup restore: " + restored);
    }
    if (!backup.renameTo(restored)) {
      throw new IOException("Can't rename backup: " + backup + " -> " + restored);
    }
  }
}
 
Example 11
Source Project: vertx-maven-plugin   File: PackageMojo.java    License: Apache License 2.0 5 votes vote down vote up
private void attachIfNeeded(File jar) {
    if (jar.isFile() && classifier != null && attach) {
        ArtifactHandler handler = new DefaultArtifactHandler("jar");
        Artifact vertxJarArtifact = new DefaultArtifact(project.getGroupId(),
            project.getArtifactId(), project.getVersion(), "compile",
            "jar", classifier, handler);
        vertxJarArtifact.setFile(jar);
        this.project.addAttachedArtifact(vertxJarArtifact);
    }
}
 
Example 12
Source Project: boubei-tss   File: ArticleContent.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 检查发布路径是否为合法路径
 */
public boolean checkPubUrl() {
	 File pubFile = new File(pubUrl);
	 if ( !pubFile.exists() || !pubFile.isFile() || !pubFile.getName().endsWith(".xml") ) {
		 return false;
	 }
	 return true;
}
 
Example 13
/**
 * Recursively browses a directory and its subdirectories for files.
 * 
 * @param dir a directory
 */
private static void getFilesRec(File dir, ArrayList<File> files) {
	File[] filesOrDirs = dir.listFiles();
	for (File fileOrDir : filesOrDirs)
		if (fileOrDir.isFile()) files.add(fileOrDir);  // add normal files
		else getFilesRec(fileOrDir, files);  // browse subdirectories
}
 
Example 14
Source Project: nd4j   File: InFileDataSetCache.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public DataSet get(String key) {
    File file = resolveKey(key);

    if (!file.exists()) {
        return null;
    } else if (!file.isFile()) {
        throw new IllegalStateException("ERROR: cannot read DataSet: cache path " + file + " is not a file");
    } else {
        DataSet ds = new DataSet();
        ds.load(file);
        return ds;
    }
}
 
Example 15
Source Project: iaf   File: DirectoryCleaner.java    License: Apache License 2.0 5 votes vote down vote up
private void cleanupFile(File file, long lastModifiedDelta) {
	if (file.isDirectory()) {
		if (subdirectories) {
			log.debug("Cleanup subdirectory [" + file.getAbsolutePath()
					+ "]");
			File[] files = file.listFiles();
			if (files != null) {
				for (int i = 0; i < files.length; i++) {
					File file2 = files[i];
					cleanupFile(file2, lastModifiedDelta);
				}
			}
		}
		if (deleteEmptySubdirectories) {
			if (file.list().length == 0) {
				if (file.delete()) {
					log.info("deleted empty subdirectory ["
							+ file.getAbsolutePath() + "]");
				} else {
					log.warn("could not delete empty subdirectory ["
							+ file.getAbsolutePath() + "]");
				}
			}
		}
	} else {
		if (file.isFile()) {
			if (FileUtils.getLastModifiedDelta(file) > lastModifiedDelta) {
				String fileStr = "file [" + file.getAbsolutePath()
						+ "] with age [" + Misc.getAge(file.lastModified())
						+ "]";
				if (file.delete()) {
					log.info("deleted " + fileStr);
				} else {
					log.warn("could not delete file " + fileStr);
				}
			}
		}
	}
}
 
Example 16
Source Project: bazel   File: ResourceUsageAnalyzer.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Copies one resource directory tree into another; skipping some files, replacing the contents of
 * some, and passing everything else through unmodified
 */
private static void filteredCopy(
    File source, Path destination, Set<File> skip, Map<File, String> replace) throws IOException {

  File destinationFile = destination.toFile();
  if (source.isDirectory()) {
    File[] children = source.listFiles();
    if (children != null) {
      if (!destinationFile.exists()) {
        boolean success = destinationFile.mkdirs();
        if (!success) {
          throw new IOException("Could not create " + destination);
        }
      }
      for (File child : children) {
        filteredCopy(child, destination.resolve(child.getName()), skip, replace);
      }
    }
  } else if (!skip.contains(source) && source.isFile()) {
    String contents = replace.get(source);
    if (contents != null) {
      Files.write(contents, destinationFile, UTF_8);
    } else {
      Files.copy(source, destinationFile);
    }
  }
}
 
Example 17
Source Project: vespa   File: EffectiveServicesMojo.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void doExecute() throws Exception {
    File services = new File(servicesFile);
    if ( ! services.isFile())
        throw new IllegalArgumentException(servicesFile + " does not exist. Set correct path with -DservicesFile=<path to services.xml>");

    ZoneId zone = zoneOf(environment, region);
    Path output = Paths.get(outputDirectory).resolve("services-" + zone.environment().value() + "-" + zone.region().value() + ".xml");
    Files.write(output, effectiveServices(services, zone, InstanceName.from(instance)).getBytes(StandardCharsets.UTF_8));
    getLog().info("Effective services for " + zone + " written to " + output);
}
 
Example 18
@Override
public DataSet get(String key) {
    File file = resolveKey(key);

    if (!file.exists()) {
        return null;
    } else if (!file.isFile()) {
        throw new IllegalStateException("ERROR: cannot read DataSet: cache path " + file + " is not a file");
    } else {
        DataSet ds = new DataSet();
        ds.load(file);
        return ds;
    }
}
 
Example 19
private static void findFiles(File file, List<File> fileList) {
    if (file.isFile() && file.canRead()) {
        fileList.add(file);
    } else if (file.isDirectory()) {
        for (File fileInDir : file.listFiles()) {
            findFiles(fileInDir, fileList);
        }
    }
}
 
Example 20
Source Project: zulip-android   File: FileUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public boolean accept(File file) {
    final String fileName = file.getName();
    // Return files only (not directories) and skip hidden files
    return file.isFile() && !fileName.startsWith(HIDDEN_PREFIX);
}