as exercise school wrote method in java searches character in file. here code:
public static void countletter(char needle, string haystack) throws ioexception { file f = new file(haystack); try (scanner in = new scanner(f)) { string str = null; while (in.hasnext()){ str += in.next(); } char[] chararr = str.tochararray(); int counter = 0; (char c: chararr) { if (c == needle){ counter++; } } system.out.println(counter); } } this need have question. is file object ever opened? , if is, ever closed? used try-with-resources on scanner object i'm pretty sure don't have explicitly close that, file object?
file object abstract representation of pathname, has nothing opening file. can not closed.
Comments
Post a Comment