/** * Read Parse CSV file, will read the result output of the console, and encapsulated into 4 teacher objects. * 1, Clay Turtle, 13101015338, Haidian District 2, Mimi, 13201015338, Chaoyang District, Beijing 3, Kokura, 13601015818, Beijing Xuanwu District 4, Rice Island Love, 13201025818, Chaoyang District, Beijing
/*** Read Parse CSV file, will read the result output of the console, and encapsulated into 4 teacher objects. * 1, Turtle, 13101015338, Haidian District 2, Mimi, 13201015338, Chaoyang District 3, Kokura, 1 3601015818, Beijing Xuanwu District 4, Rice Island Love, 13201025818, Chaoyang District, Beijing thought: 1. Read parse CSV file, first think of using BufferedReader Advanced stream ReadLine method. Reads each line of text. The 2.BufferedReader relies on the conversion stream ISR and the node stream FIS, because a line of text is read from the file, so the low-level node stream is FileInputStream (file). 3. Set up the flow and use the loop to make BufferedReader's ReadLine method read a line of text. Because the ReadLine method reads one line at a time, it returns null at the end, so you can use the while, depending on the characteristics of the return value. The loop is stopped until null. This allows you to read all the statements in the file 4. Key points: 1, turtle, 13101015338, Haidian District, Beijing reading a line, such text, you can use the split method of the string class to separate commas and blanks. Get 4 short strings of one row, and then create 1 teacher objects using the teacher (string id,string name,string tel,string address) method. loop, creating 4 objects. (This can be used to store objects in collections or arrays of containers). */ Public classReadcsv { Public Static voidMain (string[] args) {/*Creating folders and File Objects*/File dir=NewFile ("." +file.separator+ "src" +file.separator+ "practise" +file.separator+ "IO"); if(!dir.exists ()) {Dir.mkdir (); } File File=NewFile (dir, "List2.csv"); /*Create a method that can get teacher information and deposit teacher Objects*/List<Teacher> list =getcsvlist (file); /*Output Teacher Collection*/System.out.println (list); /*output of the third teacher object*/System.out.println (List.get (2)); } Public StaticList<teacher>getcsvlist (file file) {/*BufferedReader reading Files*/BufferedReader in=NULL; Try{ in=NewBufferedReader (NewInputStreamReader (NewBufferedinputstream (Newfileinputstream (file))); } Catch(FileNotFoundException e) {System.out.println ("File not Found"); E.printstacktrace (); } /*Loop Read*/ArrayList<Teacher> list =NewArraylist<teacher>(); /*String Variables*/String Line=NULL; String[] STRs=NULL; Try { while((Line=in.readline ())! =NULL){ /*Key points: A method of splitting 4 elements*/STRs= Line.split (", \\s"); /*key points: assigning 4 elements to the teacher construction method*/List.add (NewTeacher (strs[0],strs[1],strs[2],strs[3])); /*The output displays a STRs string array showing ReadLine read results per loop.*/System.out.println (arrays.tostring (STRs)); } } Catch(IOException E1) {e1.printstacktrace (); } finally { Try{/*Close Resource*/ if(In! =NULL) In.close (); } Catch(IOException e) {e.printstacktrace (); } } returnlist; }}
View Code
Exercise: Reading the parse CSV file, reading the console of the result output, and encapsulating it into 4 teacher objects.