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,

Source: Internet
Author: User

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?

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.