Skip to main content

File searching in your system through in Java

File searching through java
Input    : File Name and the directory to which searching
Output : List of files

Main Class :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.rakesh.codetalk.fileSearch;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * @author Rakesh KR
 * @since  December 2K14
 */
public class FileSearchBO {
 
 public static void main(String argv[]) throws Exception{
  
  FileSearchDTO fileSearchDTO = new FileSearchDTO();
  FileSearchDAO fileSearchDAO = new FileSearchDAO();
  BufferedReader br           = new BufferedReader(new InputStreamReader(System.in));
  
  System.out.println("Enter the fileName to be searched : ");
  fileSearchDTO.setFileNameToSearch(br.readLine());
  
  System.out.println("Enter the directory to which search : ");
  fileSearchDAO.search(new File(br.readLine()), fileSearchDTO , new ArrayList<String>());
  
  if(fileSearchDTO.getResultFileList()!=null && !fileSearchDTO.getResultFileList().isEmpty()){
   
   System.out.println("\n\nFile Founded At : ");
   
   for(String files : fileSearchDTO.getResultFileList()){

    System.out.println(" "+files);

   }
  } else{
   System.out.println("Error : "+fileSearchDTO.getStatusString());
  }
  
 }
 
}


Supporting Classes :
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.rakesh.codetalk.fileSearch;

import java.io.File;
import java.util.List;

/**
 * @author Rakesh KR
 * @since  December 2K14
 */
public class FileSearchDAO {
 
 public void search(File searchFile, FileSearchDTO fileSearchDTO , List<String> resultFileList){
  
  try{
      
   if(searchFile!=null){
    
    if(searchFile.isDirectory()){
     
     if(searchFile.canRead()){
      
      for(File file : searchFile.listFiles()){
       
       System.out.println(file);
       
       if(file.isDirectory()){
        search(file, fileSearchDTO, resultFileList);
       } else{
        if(fileSearchDTO.getFileNameToSearch().equalsIgnoreCase(file.getName())){
         resultFileList.add(file.getAbsolutePath());
        }
       }
       
      }
      
     } else{
      fileSearchDTO.setStatusString("No Permission to Read !!! File : "+searchFile.getAbsoluteFile());
     }
     
    } else{
     fileSearchDTO.setStatusString("Not a Directory !!! File : "+searchFile.getAbsoluteFile());
    }
    
   } else{
    fileSearchDTO.setStatusString("No such File !!! ");
   }
   
   if(resultFileList!=null && !resultFileList.isEmpty()){
    fileSearchDTO.setResultFileList(resultFileList);
    fileSearchDTO.setStatusString("File Searching Success !!! ");
   }
   
  } catch(Exception e){
   fileSearchDTO.setStatusString("Error in File Searching !!! ");
   e.printStackTrace();
  } finally{
   
   resultFileList = null;
   
  }
  
 }

}



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package com.rakesh.codetalk.fileSearch;

import java.io.File;
import java.util.List;

/**
 * @author Rakesh KR
 * @since  December 2K14
 */
public class FileSearchDTO {
 
 private File file;
 
 private String statusString;
 
 private String fileNameToSearch;
 
 private List<String> resultFileList;
 
 
 /**
  * @return the file
  */
 public File getFile() {
  return file;
 }

 /**
  * @param file the file to set
  */
 public void setFile(File file) {
  this.file = file;
 }

 /**
  * @return the statusString
  */
 public String getStatusString() {
  return statusString;
 }

 /**
  * @param statusString the statusString to set
  */
 public void setStatusString(String statusString) {
  this.statusString = statusString;
 }

 /**
  * @return the fileNameToSearch
  */
 public String getFileNameToSearch() {
  return fileNameToSearch;
 }

 /**
  * @param fileNameToSearch the fileNameToSearch to set
  */
 public void setFileNameToSearch(String fileNameToSearch) {
  this.fileNameToSearch = fileNameToSearch;
 }

 /**
  * @return the resultFileList
  */
 public List<String> getResultFileList() {
  return resultFileList;
 }

 /**
  * @param resultFileList the resultFileList to set
  */
 public void setResultFileList(List<String> resultFileList) {
  this.resultFileList = resultFileList;
 } 
  
}


Output Sample :
Enter the fileName to be searched : 
Rakesh.txt
Enter the directory to which search : 
/media/rakesh/WorkZ/WorkZ/CodeTalk/

/media/rakesh/WorkZ/WorkZ/CodeTalk/Short URL
/media/rakesh/WorkZ/WorkZ/CodeTalk/JAVA
/media/rakesh/WorkZ/WorkZ/CodeTalk/JAVA/What is the difference between JVM, JDK & JRE
/media/rakesh/WorkZ/WorkZ/CodeTalk/JAVA/Why we Pass String array as argument to main method [JAVA]
/media/rakesh/WorkZ/WorkZ/CodeTalk/JAVA/Rakesh.txt
/media/rakesh/WorkZ/WorkZ/CodeTalk/Linux
/media/rakesh/WorkZ/WorkZ/CodeTalk/Linux/Installing Java 7 on Ubuntu .txt
/media/rakesh/WorkZ/WorkZ/CodeTalk/Linux/Install Google Chrome.txt
/media/rakesh/WorkZ/WorkZ/CodeTalk/Linux/Rakesh.txt
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/copy of Ct-logo-small.png
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/Ct-logo-small.png
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/CodeTalk2.png
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/jvm_jre_jdk.jpg
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/CCCCCCCCC.jpg
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/CodeTalk.jpg
/media/rakesh/WorkZ/WorkZ/CodeTalk/Images/Ct-logo-big.png


File Founded At : 
 /media/rakesh/WorkZ/WorkZ/CodeTalk/JAVA/Rakesh.txt
 /media/rakesh/WorkZ/WorkZ/CodeTalk/Linux/Rakesh.txt

Comments

Popular posts from this blog

JSON (JavaScript Object Notation)

JSON (JavaScript Object Notation) is an open standard file format and data interchange format that uses human-readable text to store and transmit data objects consisting of attribute–value pairs and arrays (or other serializable values). It is a common data format with diverse uses in electronic data interchange, including that of web applications with servers. JSON is a language-independent data format. It was derived from JavaScript, but many modern programming languages include code to generate and parse JSON-format data. JSON filenames use the extension .json. Any valid JSON file is a valid JavaScript (.js) file, even though it makes no changes to a web page on its own. Douglas Crockford originally specified the JSON format in the early 2000s. He and Chip Morningstar sent the first JSON message in April 2001. Official website :- https://json.org/

Object serialization in java

Serialization is the conversion of an object to a series of bytes, so that the object can be easily saved to persistent storage or streamed across a communication link. The byte stream can then be deserialized - converted into a replica of the original object. When you want to serialize an object, that respective class should implement the marker interface serializable. It just informs the compiler that this java class can be serialized. You can tag properties that should not be serialized as transient. You open a stream and write the object into it. Code for serialization of a java class : Data.java package com . codetalk . serialization ; import java.io.Serializable ; public class Data implements Serializable { private static final long serialVersionUID = 1L; private String firstName ; private String lastName ; /** * @return the firstName */ public String getFirstName () { return firstName ; } /** * @param firstNam

Amazon Lambda (AWS Lambda)

 AWS Lambda is an event-driven, serverless computing platform provided by Amazon as a part of Amazon Web Services. It is a computing service that runs code in response to events and automatically manages the computing resources required by that code. It was introduced on November 13, 2014. AWS Lambda was designed for use cases such as image or object uploads to Amazon S3, updates to DynamoDB tables, responding to website clicks, or reacting to sensor readings from an IoT connected device.  Unlike Amazon EC2, which is priced by the hour but metered by the second, AWS Lambda is metered by rounding up to the nearest millisecond with no minimum execution time. AWS Lambda can also be used to automatically provision back-end services triggered by custom HTTP requests, and "spin down" such services when not in use, to save resources. Since December 2020 Lambda supports Docker containers through ECR up to 10 GB in size. Official website :- https://aws.amazon.com/lambda