Hello,

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)
      {
        pw.println(line);
        line = br.readLine();
      }
      br.close();
      if (!currentFile.delete())
      {
        // Failed to delete file
        System.out.println("Failed to delete "+ currentFile.getName());
      }
    }
    pw.close();
    System.out.println("All files have been concatenated into FileB.txt");
    }
}

Thank you

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 meeting, networking, learning, and sharing knowledge.