0

I have a Java program that I am using to scan a directory to look for certain files. It finds the files but now I am trying to get the code to open the files once it finds them, but I am not sure how to do that.

Here a part of my code

File file = new File("/Users/******/Desktop/******");
    String[] A = file.list();

    File[] C = file.listFiles();
for (String string : A) {
    if (string.endsWith(".txt")) {
            System.out.println(string);

        }
        if (string.contains("******")) {

            System.out.println("It contains X file");
        }

    }

I am trying to get it so once it finds the files ending in .txt, it opens all of them

I have tried using Google on how to solve his, I came across .getRuntime() and so I tried

try{
    Runtime.getRuntime().exec("******.txt");
     } catch(IOException e){
            }

But I am not fully understanding how how this works. I am trying to get to so that once it finds the files it opens them. I am not trying to have the IDE open the text on the screen. I want the actual Notepad/TextEdit program to open.

bhsingh
  • 71
  • 1
  • 5
  • 14
  • Welcome to Stack Overflow! Have you attempted to solve this problem? If you have, please edit your question to include your code and research to show what hasn't worked for you. If you haven't, you should attempt to solve it yourself first and then post the code and research here. It makes your question easier for others to answer too! – SuperBiasedMan Aug 20 '15 at 13:54
  • 1
    Possible duplicate of http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java – Kevin Aug 20 '15 at 13:54
  • Edited to explain what I have tried to solve it – bhsingh Aug 20 '15 at 13:59

1 Answers1

0
    File[] files = new File("/Users/******/Desktop/******").listFiles();
    for (File f : files) {
        String fileName = f.getName();
        if (fileName.endsWith(".txt")) {
            System.out.println(fileName);

        }
        if (fileName.contains("******")) {
            System.out.println("It contains X file");
        }
    }
chengpohi
  • 13,467
  • 1
  • 21
  • 40