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 ));}}}