The text files provided by J2SDK1.5 are willing to share with you. Reference (from): SDN - Core Java Technologies Tech Tips text file: hrinfo.txt content: old Zhao, 28, Feb-01, True Small bamboo, 22, DEC-03, FALSE Abo, 21, DEC-03, FALSE Kay Sub, 25, DEC-03, True Example: Import java.util.scanner; import java.io.file; import java.io.filenotFoundException
Public class readhuman {
private static void readFile (String fileName) {try {Scanner scanner = new Scanner (new File (fileName)); scanner.useDelimiter (System.getProperty ( "line.separator")); while (scanner.hasNext ()) {parseLine (Scanner.next ()); scanner.close ();} catch (filenotfoundexception e) {E.PrintStackTrace ();}}
Private static void parseeline (String line) {scanner linescanner = new scanner (line); linescanner.usedelimiter ("// s *, // s *"); // You can modify the UsedLimiter parameter to read the content separated by different separators String name = linescanner.next (); int Age = linescanner.nextint (); string idate = linescanner.next (); boolean iscertified = linescanner.nextBoolean (); system.out.println ("Name: name " Age: " Age ", SD Time: " iDate ", Verification Tag: " iScertified);
Public static void main (string [] args) {if (args.length! = 1) {system.err.println ("USAGE: Java Readhuman File Location); System.exit (0);} Readfile (Args [0 ]);}}