i would like to know how to implement the FileNotFoundException in my code.

my code concatenate files from a Folder, it's working well even if there is no file.
So i want to throw a message something like 'There is no file in the folder"
If there is no files in C:\FileA
here is my code:

public class Concatenate
//FileNotFoundException   IOException
  static public void main(String arg[]) throws java.io.IOException

    PrintWriter pw = new PrintWriter(new FileOutputStream("C:/FileB/FileB.txt"));// directory where concatenated file are created

    File file = new File("C:/FileA");//directory where files are stored and deleted after concatenation
    File[] files = file.listFiles();
    for (int i = 0; i < files.length; i++)
      File currentFile = files[i];
      System.out.println("Processing " + currentFile.getPath() + "... ");
      BufferedReader br = new BufferedReader(new FileReader(currentFile));
      String line = br.readLine();
      while (line != null)
        line = br.readLine();
      if (!currentFile.delete())
        // Failed to delete file
        System.out.println("Failed to delete "+ currentFile.getName());
    System.out.println("All files have been concatenated into FileB.txt");

Thank you

Recommended Answers



If(files.length>0){// check array 
   for (int i = 0; i < files.length; i++){
      //some stuff

if (file.exists()){ // check path
   //something with Drive, Folder or File …
Jump to Post

All 2 Replies

ok Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.