Java Algorithm interview questions: Read all names from text files similar to the following, print duplicate names and repeated times, and sort by repeated times; read docx and read doc. Use the poi jar package to provide download,
Read all names from text files similar to the following, print duplicate names and repeated times, and sort by repeated times
1, Zhang San, 28
2, Li Si, 35
3, Zhang San, 28
4, Wang Wu, 35
5, Zhang San, 28
6, Li Si, 35
7, Zhao 6, 28
8, Tian Qi, 35
Package com. swift; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; import java. util. map; import java. util. map. entry; import java. util. treeMap; import org. apache. poi. xwpf. extractor. XWPFWordExtractor; import org. apache. poi. xwpf. usermodel. XWPFDocument; public class IO_Sort _ Select {public static void main (String [] args) throws IOException {/** read all names from text files similar to the following, print the duplicate Name and number of repeated times, and sort by repeated times */File file_s = new File ("e: \ neck \ data.docx "); file dir = new File ("e: \ neck"); if (! Dir. exists () & dir. isDirectory () {System. out. println ("directory does not exist, will be created... "); dir. mkdirs ();} File file_t = new File (dir, "data.txt"); List <String> list = new ArrayList <String> (); String text = readFromDocx (file_s ); string [] hang = text. split ("\ n"); for (int I = 0; I
The code above reads the docx word file, and garbled characters may occur during direct reading. Because word contains not only text but also other compressed attributes, you must use the jar package of poi for parsing.
The parsed jar package is as follows:
This should be comprehensive. You can also parse excel operations or write tables.
:
Link: https://pan.baidu.com/s/1htYPKLA password: e36e
The following describes how to read the doc.
Package com. swift; import java. io. file; import java. io. fileInputStream; import java. io. IOException; import java. util. arrayList; import java. util. collections; import java. util. comparator; import java. util. list; import java. util. map; import java. util. map. entry; import java. util. treeMap; import org. apache. poi. hwpf. HWPFDocument; import org. apache. poi. xwpf. extractor. XWPFWordExtractor; import org. apache. poi. xwpf. user Model. XWPFDocument; public class IO_Sort_Select {public static void main (String [] args) throws IOException {/** read all names from text files similar to the following, print the duplicate Name and number of repeated times, and sort by repeated times */File file_s = new File ("e: \ neck \ data.doc "); file dir = new File ("e: \ neck"); if (! Dir. exists () & dir. isDirectory () {System. out. println ("directory does not exist, will be created... "); dir. mkdirs () ;}list <String> List = new ArrayList <String> (); String text = readFromDoc (file_s); String [] hang = text. split ("\ r"); for (int I = 0; I
The text read by doc cannot be separated by "\ n", but only "\ r" is used. Otherwise, only one line can be split, do you think the program is inexplicably uneasy about your own ideas?