Jump to content

[JAVA] Problem z przeszukiwaniem plików z wzorcem


Recommended Posts

Mam listowanie plików w folderze i wszystko fajnie, ale jak szukam po nazwie to są powtórzenia i dlatego chcę w tych powtórzeniach wyszukać tekst, dzięki czemu na pewno dostane dobry plik wyjściowy, więc po prawidłowym listowaniu mam tak:

if (compareInFile("isVideoEnabled", file.getAbsolutePath())) {
    System.out.println("mam to!");
} else {
    System.out.println("dupa");
}

a tu cały boolean:

public static boolean compareInFile(String inputWord, String inputFile) {
		 
	    String word = "";
	 
	    File file = new File(inputFile);
	    try {
	        Scanner input = new Scanner(file);
	        while (input.hasNext()) {
	            word = input.next();
	            if (inputWord.equals(word)) {
	                return true;
	            }
	        }
	 
	    } catch (Exception error) {
	    }
	    return false;
	}

 

Rozwiązane:

File filey = new File(file.getAbsolutePath());

                	try {
                	    Scanner scanner = new Scanner(filey);

                	    //now read the file line by line...
                	    int lineNum = 0;
                	    while (scanner.hasNextLine()) {
                	        String line = scanner.nextLine();
                	        lineNum++;
                	        if(line.contains("isVideoEnabled")) { 
                	            System.out.println("ho hum, i found it on line " +lineNum);
                	        }
                	    }
                	} catch(FileNotFoundException e) { 
                	    //handle this
                	}

 

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...