I don't know why the isFile() of the element f in the files list traversed by the for loop in the FileInit class in the code below is always false.false. But if delete the content related to FileItem, it will return to normal.It's kind of weird to me, and I didn't find a targeted answer. It might be some peculiarity of Java.A cloud disk project is used to learn Java, using Google's Gson library.
public class FileInit {
String path;
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
//Query file method, will return a list containing file information
public String queryFiles() throws NoSuchAlgorithmException, IOException {
//Query the path and build a File collection
File file = new File(path);
File[] files = file.listFiles();
//Build a List to store the converted FileItem
List<FileItem> fileItems = new ArrayList<>();
//Traversing files to make judgments
for (File f:files){
FileItem fileItem = new FileItem(f);
fileItem.printFile();
fileItems.add(fileItem);
}
Gson gson = new Gson();
return gson.toJson(fileItems);
}
//formatted output
public void printFiles(String files){
Gson gson = new Gson();
Type type = new TypeToken<List<FileItem>>(){}.getType();
List<FileItem> fileItems = gson.fromJson(files, type);
// Format output file list
int count = 0;
for (FileItem f :fileItems ) {
String name;
if (f.getFileType()==null){
name = f.getFileName() + "/";
}else {
name = f.getFileName();
}
System.out.printf("%-40s", name);
count++;
if (count % 3 == 0) {
System.out.println();
}
}
System.out.println();
}
//Change directory command
public void changeDic(String addPath){
File fileDic1 = new File(path+addPath);
File fileDic2 = new File(addPath);
if (addPath.equals("..")) {
File parent = new File(path).getParentFile();
if (parent != null) {
this.path = parent.getPath();
}else{
System.out.println("Parent directory does not exist");
}
}
else {
if (fileDic1.exists()){
this.path = path+addPath;
} else if (fileDic2.exists()) {
this.path = addPath;
}else{
System.out.println("Illegal input path");
}
}
}
}
public class FileItem {
private String fileName;
private String fileHash;
private String filePath;
private long fileLength;
private String fileType;
//Construction method of FileItem
/*
Build a construction method that only needs fileName and filePath, and judge whether to calculate the hash value, file size, and file type according to the folder or file
*/
public FileItem(String fileName,String filePath) {
this.fileName = fileName;
this.filePath = filePath;
File file =new File(this.filePath+"/"+this.fileName);
if (file.isFile()){
try {
//Get the file size through the file built-in method
this.fileLength = file.length();
// Define a regular expression for extracting the suffix of the file name
String regex = "\\.(\\w+)$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(fileName);
// If the match is successful
if (matcher.find()) {
// Get the suffix of the file name
this.fileType = matcher.group(1);
}else{
this.fileType = null;
}
//Calculate the Hash value of the file by calling the FileHash method
this.fileHash=FileHash(file.getPath());
System.out.printf(fileHash);
System.out.print("\n");
} catch (NoSuchAlgorithmException | IOException e) {
throw new RuntimeException(e);
}
}else{
this.fileName=fileName;
this.fileLength=0;
this.fileType = null;
this.fileHash=null;
}
}
//Build a constructor that only needs a json file
public FileItem(String json){
Gson gson = new Gson();
FileItem fileItem = gson.fromJson(json,FileItem.class);
this.fileName=fileItem.getFileName();
this.filePath=fileItem.getFilePath();
this.fileHash=fileItem.getFileHash();
this.fileLength=fileItem.getFileLength();
this.fileType=fileItem.getFileType();
}
//Realize mutual conversion between FileItem and File class
public File toFile(){
return new File(this.filePath+"/"+this.fileName);
}
public FileItem(File file) throws NoSuchAlgorithmException, IOException {
FileItem fileItem = new FileItem(file.getName(),file.getPath());
this.fileName=fileItem.getFileName();
this.filePath=fileItem.getFilePath();
this.fileHash=fileItem.getFileHash();
this.fileLength=fileItem.getFileLength();
this.fileType=fileItem.getFileType();
}
//Display FileItem related information
public void printFile(){
if (fileType==null){
System.out.println(fileName+"是文件夹");
}else {
System.out.println(fileName+"是"+fileType+"文件");
}
double fileLenOp=(double)fileLength;
//Different file sizes use different output methods
if(fileLength<=1024*1024){
System.out.printf("file size is:%.3fKB\n",fileLenOp/1024);
} else if ((fileLength<1024*1024*1024)) {
System.out.printf("file size is:%.3fMB\n",fileLenOp/1024/1024);
}else {
System.out.printf("file size is:%.3fGB\n",fileLenOp/1024/1024/1024);
}
System.out.println("file hash is:"+fileHash);
}
//Getter and Setter methods for FileItem
public String getFileName() {
return fileName;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileHash() {
return fileHash;
}
public void setFileHash(String fileHash) {
this.fileHash = fileHash;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
}
public long getFileLength() {
return fileLength;
}
public void setFileLength(long fileLength) {
this.fileLength = fileLength;
}
public String getFileType() {
return fileType;
}
public void setFileType(String fileType) {
this.fileType = fileType;
}
}
I try to query the file information in a folder by using the methods related to the File class. And return them as a collection. Then realize the conversion of the File class and the FileItem class by traversing this collection to form a new FileItem class collection , so that it can be converted into Json format data for network transmission. However, all fileitems in the generated FileItem collection are judged as folders. And if you comment out the FileItem related content, the judgment of File will be normal, but if you don’t do this , the judgment of File will always remain false.
Related
I'm trying to load classes from tree structure similar to JavaEE web application. So I have directory which contains compiled classes (/classes) and sibling directory with libraries (/lib). In classes package structure I have compiled class com.test.Test.class with simple logic:
package com.test;
import org.json.JSONException;
import org.json.JSONObject;
import com.test.GameObjectState;
public class Test extends Thread {
#Override
public void run() {
JSONObject json = new JSONObject();
try {
json.put("state", GameObjectState.REMOVED);
} catch (JSONException e) {
e.printStackTrace();
}
System.out.println(json.toString());
}
}
And here's my main:
ClassLoader classLoader = ClassLoaderFactory.createClassLoader(repository);
Class<?> testClass = classLoader.loadClass("com.test.Test");
Thread t = (Thread) testClass.newInstance();
t.start();
My ClassLoader implementation follows the delegation model. During context init finally JSONObject & JSONException will be scanned and found in lib/java-json.jar (matching corresponding JarEntry binary name inside). When JarEntry found - creates InputStream to load data in buffer to define class.
JSONException loaded successfully while JSONObject failed on ClassFormatError. Magic is that I've extracted JSONObject in /classes directory manually from java-json.jar and no ClassFormatError has been thrown, JSONObject created and output printed successfully. Probably should note that JSONObject has inner/nested classes compiled in library.
And here's my classloader
public class WebAppClassLoader extends URLClassLoader {
private static final String CLASS_FILE_SUFFIX = ".class";
private static final String JAR_FILE_SUFFIX = ".jar";
private static final String CLASS_NOT_FOUND = "Class %s not found";
private File classesDirectory;
private Map<String, Class<?>> classes;
public WebAppClassLoader(File classesDirectory, URL[] urls) {
super(urls);
this.classesDirectory = classesDirectory;
this.classes = new ConcurrentHashMap<String, Class<?>>();
}
public static WebAppClassLoader newInstance(File classesDirectory, File libDirectory) throws IOException {
File[] jars = libDirectory.listFiles(new JarFilter());
Set<URL> jarUrls = new HashSet<URL>();
for(File jar : jars) {
jarUrls.add(jar.toURI().toURL());
}
return new WebAppClassLoader(classesDirectory, jarUrls.toArray(new URL[jarUrls.size()]));
}
protected static class DirectoryFilter implements FileFilter {
#Override
public boolean accept(File pathname) {
return pathname.isDirectory();
}
}
protected static class ClassFilter implements FileFilter {
#Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(CLASS_FILE_SUFFIX);
}
}
protected static class JarFilter implements FileFilter {
#Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(JAR_FILE_SUFFIX);
}
}
#Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
Class<?> result = null;
byte[] buffer = null;
try {
buffer = scanClasses(classesDirectory, asPathName(name));
if(buffer == null) {
buffer = scanLib(asPathName(name).replace('\\', '/'));
}
if(buffer != null) {
result = defineClass(name, buffer, 0, buffer.length);
classes.put(name, result);
}
} catch(IOException e) {
e.printStackTrace();
}
if(result == null) {
String error = String.format(CLASS_NOT_FOUND, name);
throw new ClassNotFoundException(error);
}
return result;
}
private byte[] scanClasses(File file, String path) throws IOException {
byte[] result = null;
File[] directories = file.listFiles(new DirectoryFilter());
File[] classes = file.listFiles(new ClassFilter());
for(File classFile : classes) {
if(classFile.getAbsolutePath().endsWith(path)) {
InputStream input = new FileInputStream(classFile);
input.read(result = new byte[input.available()]);
input.close();
break;
}
}
if(result == null) {
for(File directory : directories) {
result = scanClasses(directory, path);
if(result != null) {
break;
}
}
}
return result;
}
private byte[] scanLib(String path) throws IOException {
byte[] result = null;
for(URL url : getURLs()) urlLoop:{
File file = new File(url.getFile());
JarFile jarFile = new JarFile(file);
try {
Enumeration<JarEntry> entries = jarFile.entries();
while(entries.hasMoreElements()) {
JarEntry entry = entries.nextElement();
if(entry.getName().endsWith(path)) {
InputStream input = jarFile.getInputStream(entry);
input.read(result = new byte[input.available()]);
break urlLoop;
}
}
}finally {
jarFile.close();
}
}
return result;
}
#Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
Class<?> result = null;
if(classes.containsKey(name)) {
result = classes.get(name);
}else{
result = super.loadClass(name);
}
return result;
}
private static String asPathName(String binaryName) {
return binaryName.replace('.', '\\').concat(CLASS_FILE_SUFFIX);
}
}
Thanks
I am new to Java, I tried to practice some example but I am facing issue in the file handling topic.
Following is the example that what I am trying.
T1--> T2--> T3--> T4--> Gan--> q.txt
|
--> Lin-->Img-->s.png
|
--> p.txt
This is the folder structure.
And I want output in the following format.
p.txt
Lin/Img/s.png
Gen/q.txt
That means when the first file is getting in any directory, after that next file will be printed with the path from first file is got.
The above directory structure is not fixed. It may change.
Now following are code that I have did but I am not getting proper output:
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
class FileProgram {
private ArrayList<File> listOfDirectories = new ArrayList<File>();
private ArrayList<File> rawFiles = new ArrayList<File>();
private ArrayList<String> parentDir = new ArrayList<String>();
private ArrayList<String> filesToDisplay = new ArrayList<String>();
private Iterator i, listDir;
private boolean firstFile = false;
private String parents = "";
public void getDetails(File file) {
try {
if (file.exists()) {
File directoies[] = file.listFiles();
if (!rawFiles.isEmpty()) {
rawFiles.clear();
}
for (File f : directoies) {
rawFiles.add(f);
}
i = rawFiles.iterator();
while (i.hasNext()) {
File isFile = (File) i.next();
if (isFile.isFile()) {
displayFiles(isFile);
}
if (isFile.isDirectory()) {
listOfDirectories.add(isFile);
}
}
iterateInnerDirectories();
} else {
System.out.println("Invalid File Path");
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
FileProgram ray = new FileProgram();
ray.getDetails(new File("D:\\Temp"));
}
private void iterateInnerDirectories() {
listDir = listOfDirectories.iterator();
while (listDir.hasNext()) {
File isFile = (File) listDir.next();
File f = isFile;
listOfDirectories.remove(isFile);
getDetails(isFile);
}
}
private void displayFiles(File file) {
if (firstFile == false) {
firstFile = true;
String rootPath = file.getParent();
rootPath = rootPath.replace(file.getName(), "");
parentDir.add(rootPath);
parents = file.getParentFile().getName();
System.out.println(file.getName());
filesToDisplay.add(file.getName());
} else {
String rootPath = file.getParent();
rootPath = rootPath.replace(file.getName(), "");
if (parentDir.contains(rootPath)) {
parents = file.getParentFile().getName();
System.out.println(file.getName());
filesToDisplay.add(file.getName());
} else {
System.out.println(file);
}
}
}
}
Please anybody can help me to get proper output that I have mentioned above.
Thanks in advance.
Unless you're using a Java prior to Java 7 I would strongly suggest to use Path.
You can walk a directory recursively using Files.walkFileTree().
Once you encounter a file (!Files.isDirectory()), you can get its parent with Path.getParent(). And you can print the relative path to this parent of all further file using Path.relativize().
Short, Simple Implementation
In this implementation I don't even use Files.isDirectory() because visitFile() is only called for files:
public static void printFiles(Path start) {
try {
Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
Path parent;
#Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
if (parent == null)
parent = file.getParent();
System.out.println(parent.relativize(file));
return FileVisitResult.CONTINUE;
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
And this is how you call it:
printFiles(Paths.get("/path/to/T1"));
How to find the file format using Java(suppose .txt files, .java files, .class files, .zip files are in my directory)?
I need only .txt files is body provide solution it might be help full.
public class a {
public static void main(String[] ar) {
File f = new File("D:\\")
file[] = f.listFiles();
if (file.isFile && file is txt) {
}
}
}
Use File#listFiles(FilenameFilter) instead of File#listFiles():
f.listFiles(new FilenameFilter() {
private Pattern pattern = Pattern.compile(".*\\.txt");
#Override
public boolean accept(File dir, String name) {
return pattern.matcher(name).matches();
}
});
If i did understood you correctly you wanna find the file extensions , here is sample application I made for you using swing worker , you can now scan your entire computer with specific file extensions you defined in program currently it finds html , txt and pdf you can modify that.
Here is the entire project package
Create Scan Worker Class Like this below and fix the imports using IDE
public class ScanWorker extends SwingWorker<Void, String> {
ResultFrame helperv;
int finalTotal = 0;
String str3;
public String status;
public ScanWorker(ResultFrame x) {
finalTotal = 0;
helperv = x;
helperv.jTarea.setText("");
}
#Override
protected Void doInBackground() throws Exception {
helperv.jTarea.replaceSelection("Scanning..." + "\n");
Thread.sleep(100);
callMainScan();
return null;
}
#Override
protected void process(List<String> chunks) {
for (String str : chunks) {
helperv.jTarea.append(str);
helperv.scanlabel.setText(str);
}
}
#Override
protected void done() {
helperv.scanlabel.setText("Total Number of Files Matched: " + Integer.toString(finalTotal));
}
private void callMainScan() throws IOException {
String[] myStringArray = new String[3];
myStringArray[0] = "*.txt";
myStringArray[1] = "*.html";
myStringArray[2] = "*.pdf";
File[] paths;
paths = File.listRoots();
try {
for (File path : paths) {
String str = path.toString();
String slash = "\\";
String s = new StringBuilder(str).append(slash).toString();
Path startingDir = Paths.get(s);
for (String stre : myStringArray) {
String pattern = stre;
Finder finder = new Finder(pattern);
Files.walkFileTree(startingDir, finder);
finder.done();
}
}
} catch (IOException e) {
System.out.println("Exception occured");
}
}
public class Finder
extends SimpleFileVisitor<Path> {
private final PathMatcher matcher;
private int numMatches = 0;
Finder(String pattern) {
matcher = FileSystems.getDefault()
.getPathMatcher("glob:" + pattern);
}
// Compares the glob pattern against
// the file or directory name.
void find(Path file) {
Path name = file.getFileName();
if (name != null && matcher.matches(name)) {
numMatches++;
System.out.println(file);
str3 = file.toString();
publish(str3 + "\n");
}
}
// Prints the total number of
// matches to standard out.
void done() {
// System.out.println("Matched: "
// + numMatches);
finalTotal = finalTotal + numMatches;
}
// Invoke the pattern matching
// method on each file.
#Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) {
find(file);
return CONTINUE;
}
// Invoke the pattern matching
// method on each directory.
#Override
public FileVisitResult preVisitDirectory(Path dir,
BasicFileAttributes attrs) {
find(dir);
return CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path file,
IOException exc) {
// System.err.println(exc);
return CONTINUE;
}
}
}
Scan Button Action
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
new Thread(new Runnable() {
#Override
public void run() {
sw = new ScanWorker(ResultFrame.this);
sw.execute();
}
}).start();
}
You can use a File.listFiles(FilenameFilter) for it; like this:
File[] files = f.listFiles((File dir, String name) -> name.endsWith(".txt"));
Try this -
public static String getExtension(File file) {
String fileName = file.getName();
int i = fileName.lastIndexOf('.');
if (i > 0) {
return fileName.substring(i + 1);
}
return null;
}
public static void main(String[] arr) {
File f = new File("D:\\");
File files[] = f.listFiles();
for (File file : files) {
if (file.isFile()) {
if ("txt".equals(getExtension(file))) {
System.out.println(file.getName()+" is a text file");
}
}
}
}
You can try this.
String directory = "c:";
File file = new File(directory);
if (file.isDirectory()) {
File[] files = file.listFiles();
System.out.println(files.length);
for (File innerFiles : files) {
String fileName = innerFiles.getName();
if (fileName.contains(".") &&
(fileName.substring(fileName.lastIndexOf("."), fileName.length())
.equalsIgnoreCase(".txt"))
System.out.println(fileName);
}
}
I know how to copy a file from one directory to another, what I would like help on is copy a file with .jpg or .doc extension.
So for example.
Copy all files from D:/Pictures/Holidays
Scanning all folders in the above path and transfer all jpg's to a destination provided.
This works, but the file 'copy(File file, File outputFolder)' method could be enhanced for large files:
package net.bpfurtado.copyfiles;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyFilesFromType
{
public static void main(String[] args)
{
new CopyFilesFromType().copy("jpg", "C:\\Users\\BrunoFurtado\\Pictures", "c:/temp/photos");
}
private FileTypeOrFolderFilter filter = null;
private void copy(final String fileType, String fromPath, String outputPath)
{
filter = new FileTypeOrFolderFilter(fileType);
File currentFolder = new File(fromPath);
File outputFolder = new File(outputPath);
scanFolder(fileType, currentFolder, outputFolder);
}
private void scanFolder(final String fileType, File currentFolder, File outputFolder)
{
System.out.println("Scanning folder [" + currentFolder + "]...");
File[] files = currentFolder.listFiles(filter);
for (File file : files) {
if (file.isDirectory()) {
scanFolder(fileType, file, outputFolder);
} else {
copy(file, outputFolder);
}
}
}
private void copy(File file, File outputFolder)
{
try {
System.out.println("\tCopying [" + file + "] to folder [" + outputFolder + "]...");
InputStream input = new FileInputStream(file);
OutputStream out = new FileOutputStream(new File(outputFolder + File.separator + file.getName()));
byte data[] = new byte[input.available()];
input.read(data);
out.write(data);
out.flush();
out.close();
input.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private final class FileTypeOrFolderFilter implements FileFilter
{
private final String fileType;
private FileTypeOrFolderFilter(String fileType)
{
this.fileType = fileType;
}
public boolean accept(File pathname)
{
return pathname.getName().endsWith("." + fileType) || pathname.isDirectory();
}
}
}
Use a FileFilter when listing files.
In this case, the filter would select directories and any file type of interest.
Here is a quick example (crudely hacked out of another project) of gaining a list of types of files in a directory structure.
import java.io.*;
import java.util.ArrayList;
class ListFiles {
public static void populateFiles(File file, ArrayList<File> files, FileFilter filter) {
File[] all = file.listFiles(filter);
for (File f : all) {
if (f.isDirectory()) {
populateFiles(f,files,filter);
} else {
files.add(f);
}
}
}
public static void main(String[] args) throws Exception {
String[] types = {
"java",
"class"
};
FileFilter filter = new FileTypesFilter(types);
File f = new File("..");
ArrayList<File> files = new ArrayList<File>();
populateFiles(f, files, filter);
for (File file : files) {
System.out.println(file);
}
}
}
class FileTypesFilter implements FileFilter {
String[] types;
FileTypesFilter(String[] types) {
this.types = types;
}
public boolean accept(File f) {
if (f.isDirectory()) return true;
for (String type : types) {
if (f.getName().endsWith(type)) return true;
}
return false;
}
}
Use following file walker tree class to do that
static class TreeCopier implements FileVisitor<Path> {
private final Path source;
private final Path target;
private final boolean preserve;
private String []fileTypes;
TreeCopier(Path source, Path target, boolean preserve, String []types) {
this.source = source;
this.target = target;
this.preserve = preserve;
this.fileTypes = types;
}
#Override
public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) {
// before visiting entries in a directory we copy the directory
// (okay if directory already exists).
CopyOption[] options = (preserve)
? new CopyOption[]{COPY_ATTRIBUTES} : new CopyOption[0];
Path newdir = target.resolve(source.relativize(dir));
try {
Files.copy(dir, newdir, options);
} catch (FileAlreadyExistsException x) {
// ignore
} catch (IOException x) {
System.err.format("Unable to create: %s: %s%n", newdir, x);
return SKIP_SUBTREE;
}
return CONTINUE;
}
#Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
String fileName = file.toFile().getName();
boolean correctType = false;
for(String t: fileTypes) {
if(fileName.endsWith(t)){
correctType = true;
break;
}
}
if(correctType)
copyFile(file, target.resolve(source.relativize(file)), preserve);
return CONTINUE;
}
#Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
// fix up modification time of directory when done
if (exc == null && preserve) {
Path newdir = target.resolve(source.relativize(dir));
try {
FileTime time = Files.getLastModifiedTime(dir);
Files.setLastModifiedTime(newdir, time);
} catch (IOException x) {
System.err.format("Unable to copy all attributes to: %s: %s%n", newdir, x);
}
}
return CONTINUE;
}
#Override
public FileVisitResult visitFileFailed(Path file, IOException exc) {
if (exc instanceof FileSystemLoopException) {
System.err.println("cycle detected: " + file);
} else {
System.err.format("Unable to copy: %s: %s%n", file, exc);
}
return CONTINUE;
}
static void copyFile(Path source, Path target, boolean preserve) {
CopyOption[] options = (preserve)
? new CopyOption[]{COPY_ATTRIBUTES, REPLACE_EXISTING}
: new CopyOption[]{REPLACE_EXISTING};
if (Files.notExists(target)) {
try {
Files.copy(source, target, options);
} catch (IOException x) {
System.err.format("Unable to copy: %s: %s%n", source, x);
}
}
}
}
and call it using following two lines
String []types = {".java", ".form"};
TreeCopier tc = new TreeCopier(src.toPath(), dest.toPath(), false, types);
Files.walkFileTree(src.toPath(), tc);
.java and .form file types are not omitted to copy and passed as String array parameter, src.toPath() and dest.toPath() are source and destination paths, false is used to specify not to preserve previous files and overwrite them
if you want reverse that is to consider only these remove not and use as
if(!correctType)
Take a look at the listFiles methods from the File class:
Link 1
Link 2
You could try this code:
public class MyFiler implements FileNameFilter{
bool accept(File file, String name){
if(name.matches("*.jpg");
}
}
public void MassCopy(){
ArrayList<File> filesToCopy = new ArrayList<File>();
File sourceDirectory = new File("D:/Pictures/Holidays");
String[] toCopy = sourceDirectory.list(new MyFilter());
for(String file : toCopy){
copyFileToDestination(file);
}
}
I need a to find file according to its name in directory tree. And then show a path to this file. I found something like this, but it search according extension. Could anybody help me how can I rework this code to my needs...thanks
public class filesFinder {
public static void main(String[] args) {
File root = new File("c:\\test");
try {
String[] extensions = {"txt"};
boolean recursive = true;
Collection files = FileUtils.listFiles(root, extensions, recursive);
for (Iterator iterator = files.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
System.out.println(file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class Test {
public static void main(String[] args) {
File root = new File("c:\\test");
String fileName = "a.txt";
try {
boolean recursive = true;
Collection files = FileUtils.listFiles(root, null, recursive);
for (Iterator iterator = files.iterator(); iterator.hasNext();) {
File file = (File) iterator.next();
if (file.getName().equals(fileName))
System.out.println(file.getAbsolutePath());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Recursive directory search in Java is pretty darn easy. The java.io.File class has a listFiles() method that gives all the File children of a directory; there's also an isDirectory() method you call on a File to determine whether you should recursively search through a particular child.
You can use FileFilter Like this.
public class MyFileNameFilter implements FilenameFilter {
#Override
public boolean accept(File arg0, String arg1) {
// TODO Auto-generated method stub
boolean result =false;
if(arg1.startsWith("KB24"))
result = true;
return result;
}
}
And call it like this
File f = new File("C:\\WINDOWS");
String [] files = null;
if(f.isDirectory()) {
files = f.list(new MyFileNameFilter());
}
for(String s: files) {
System.out.print(s);
System.out.print("\t");
}
Java 8 Lamda make this easier instead of using FileNameFilter, pass lambda expression
File[] filteredFiles = f.listFiles((file, name) ->name.endsWith(extn));
I don't really know what FileUtils does, but how about changing "txt" in extenstions to "yourfile.whatever"?
public static File find(String path, String fName) {
File f = new File(path);
if (fName.equalsIgnoreCase(f.getName())) return f;
if (f.isDirectory()) {
for (String aChild : f.list()) {
File ff = find(path + File.separator + aChild, fName);
if (ff != null) return ff;
}
}
return null;
}