public class solution {
public static void main(String args[])
{
// Get the file
File f = new File("F:\\program");
// Check if the specified path
// is a directory or not
if (f.isDirectory())
System.out.println("Directory");
else
System.out.println("is not Directory");
}
}
public class FileDemo {
public static void main(String[] args) {
File f = null;
String path;
boolean bool = false;
try {
// create new file
f = new File("c:");
// true if the file path is directory, else false
bool = f.isDirectory();
// get the path
path = f.getPath();
// prints
System.out.println(path+" is directory? "+ bool);
// create new file
f = new File("c:/test.txt");
// true if the file path is directory, else false
bool = f.isDirectory();
// get the path
path = f.getPath();
// prints
System.out.print(path+" is directory? "+bool);
} catch(Exception e) {
// if any error occurs
e.printStackTrace();
}
}
}
public class FileIsDirectoryExample {
public static void main(String[] args) {
// initialize File object
File file = new File("C:\\javatutorialhq\\input");
// check if the file is a directory
boolean result = file.isDirectory();
if (result) {
System.out.println(file.getAbsolutePath() + " is a directory");
System.out.println("***** Listing all files on the directory *****");
// listing the files
String[] list = file.list();
for (String s : list) {
System.out.println(s);
}
}
}
}
public class FileExample {
public static void main(String[] args) throws Exception {
File file = null;
File parent = null;
boolean isDir;
boolean isFile;
try {
System.out.println("-- file --");
file = new File("C:\sandbox\test.txt");
// check if the file object is a directory
isDir = file.isDirectory();
System.out.println("Is '" + file.getAbsolutePath() + "' a directory? " + isDir);
// check if the file object is a file
isFile = file.isFile();
System.out.println("Is '" + file.getAbsolutePath() + "' a file? " + isFile);
System.out.println("-- parent --");
parent = file.getParentFile();
// check if the file object is a directory
isDir = parent.isDirectory();
System.out.println("Is '" + parent.getAbsolutePath() + "' a directory? " + isDir);
// check if the file object is a file
isFile = parent.isFile();
System.out.println("Is '" + parent.getAbsolutePath() + "' a file? " + isFile);
} catch (Exception e) {
System.err.println("Things went wrong: " + e.getMessage());
e.printStackTrace();
}
}
}
public class IsDirectoryExample {
public static void main(String... args) throws IOException {
Path dirPath = Files.createTempDirectory("test-dir");
System.out.println("path: " + dirPath);
boolean directory = Files.isDirectory(dirPath);
System.out.println("dir: " + directory);
Path filePath = Files.createTempFile("test-dir", ".txt");
System.out.println("path: " + filePath);
directory = Files.isDirectory(filePath);
System.out.println("dir: " + directory);
}
}
public static void moveFileToDirectory(File srcFile, File destDir, boolean createDestDir) throws IOException {
if (srcFile == null) {
throw new NullPointerException("Source must not be null");
}
if (destDir == null) {
throw new NullPointerException("Destination directory must not be null");
}
if (!destDir.exists() && createDestDir) {
destDir.mkdirs();
}
if (!destDir.exists()) {
throw new FileNotFoundException("Destination directory '" + destDir +
"' does not exist [createDestDir=" + createDestDir +"]");
}
if (!destDir.isDirectory()) {
throw new IOException("Destination '" + destDir + "' is not a directory");
}
moveFile(srcFile, new File(destDir, srcFile.getName()));
}
public void generateTargetFile(File fileOrDir) {
File workingDir = fileOrDir.isDirectory() ? fileOrDir : fileOrDir.getParentFile();
File templateFile = new File(workingDir,templateFileName);
File targetFile = new File(workingDir, targetFileName);
File valuesFile = new File(workingDir, valuesFileName);
String[] contents = FileSupport.readContentOfFile(templateFile);
Properties valueProperties = new Properties();
try {
valueProperties.load(new FileInputStream(valuesFile));
} catch (IOException e) {
throw new RuntimeException("Problem reading key value pairs from valuesFile " + valuesFile.getAbsolutePath());
}
for (String keyId: valueProperties.stringPropertyNames()) {
String value = valueProperties.getProperty(keyId);
TemplateKeyDefinition keyDefinition = getKeyDefinitionByKeyId(keyId);
if (keyDefinition == null) continue;
writeValueToContent(contents, keyDefinition, value);
}
FileSupport.writeContentOfFile(targetFile,contents);
}
public static void zip(String targetPath, String destinationFilePath, String password) {
try {
ZipParameters parameters = new ZipParameters();
parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
if (password.length() > 0) {
parameters.setEncryptFiles(true);
parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
parameters.setPassword(password);
}
ZipFile zipFile = new ZipFile(destinationFilePath);
File targetFile = new File(targetPath);
if (targetFile.isFile()) {
zipFile.addFile(targetFile, parameters);
} else if (targetFile.isDirectory()) {
zipFile.addFolder(targetFile, parameters);
}
} catch (Exception e) {
e.printStackTrace();
}
}
private List<Song> listSongs(File directory) {
ArrayList<Song> ret = new ArrayList<>();
if (!directory.exists()) {
System.out.println("Directory does not exist: " + directory);
return ret;
}
File[] files = directory.listFiles();
if (files == null) return ret;
for (File file : files) {
if (file.isDirectory()) {
ret.addAll(listSongs(file));
} else {
Song song = tryToReadSong(file);
if (song == null) continue;
ret.add(song);
}
}
return ret;
}
public static void delete(File file) {
if (file != null && file.exists()) {
if (file.isDirectory()) {
File[] children = file.listFiles();
if (children != null) {
for (File child : children) {
delete(child);
}
}
file.delete();
} else {
file.delete();
}
}
}