Save Java file input, count a string, count all strings, java strings

Source: Internet
Author: User

Save Java file input, count a string, count all strings, java strings

Import java. io. *; import java. util. *;/*** Created by Admin on 2018/3/20. */public class FileSaveTest {public static void main (String [] args) throws IOException {/* // enter and save snapshot lifecycle = new partition (System. in); String str = response. nextLine (); // input File file = new File ("1.txt"); // file FileOutputStream fos; // output stream fos = new FileOutputStream (File); fos. write (str. getBytes (); // write to System. out. println ("sccce Ss "); * // the number of times a String appears in the file. BufferedReader br = new BufferedReader (new FileReader (" 1.txt"); String result = ""; string s; while (s = br. readLine ())! = Null) {// read a row at a time result + = result + s + "";} int count = 0; String [] str = result. split (""); for (int I = 0; I <str. length; I ++) {if (str [I]. equals ("if") {count ++;} System. out. println (count); * // counts the number of occurrences of each String BufferedReader br = new BufferedReader (new FileReader ("1.txt"); String result =" "; String line; while (line = br. readLine ())! = Null) {// read a row at a time result + = result + line + "";} String [] str = result. split (""); Map <String, Integer> map = new HashMap <String, Integer> (); for (String s: str) map. put (s, map. get (s) = null? 1: map. get (s) + 1); Iterator iterator = map. keySet (). iterator (); while (iterator. hasNext () {Object o = iterator. next (); System. out. println (o + ":" + map. get (o ));}}}

 

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.