java - Read data from multiple text files -
i have text files in folder. contains letters , numbers. example 1 of tex file contains:
db: localhost data created: 2016-01-18 user: root pass: usbw
so want scan files in folder. , print info inside every text file screen. far trying use code:
class filehandle { int i; string a; string b; public void openfile() throws filenotfoundexception { file dir = new file("c:/folder/db"); if (dir.isdirectory()) { (file file : dir.listfiles()) { scanner s = new scanner(file); string f = file.getname(); system.out.println("file name:" + f); while (s.hasnext()) { if (s.hasnextint()) { = s.nextint(); system.out.println("int: " + i); } = s.next(); b = s.next(); system.out.printf("%s", a); system.out.printf("%s", b); s.close(); } } } } }
but got error:
file name:localdb.txt exception in thread "main" java.lang.illegalstateexception: scanner closed @ java.util.scanner.ensureopen(scanner.java:1070) localhostcreated: @ java.util.scanner.hasnext(scanner.java:1334) @ databasesearch.filehandle.openfile(filehandle.java:30) @ databasesearch.databasesearch.main(databasesearch.java:21) c:\users\d1sturbance\appdata\local\netbeans\cache\8.1\executor- snippets\run.xml:53: java returned: 1 build failed (total time: 0 seconds)
remove s.close();
or move outside of while
loop.
Comments
Post a Comment