- 我的java代碼需要運行將近10-15分鐘(輸入文件是7200+行長的查詢列表)。如何在短時間內運行以獲得相同的結果?
- 如何讓我的代碼只搜索aA到zZ和0到9?
如果我不做#2,我輸出中的一些字符顯示爲「?」。我該如何解決這個問題?我該如何讓我的java代碼只搜索a到z和0到9
// no parameters are used in the main method public static void main(String[] args) { // assumes a text file named test.txt in a folder under the C:\file\test.txt Scanner s = null; BufferedWriter out = null; try { // create a scanner to read from the text file test.txt FileInputStream fstream = new FileInputStream("C:\\user\\query.txt"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); // Write to the file out = new BufferedWriter(new FileWriter("C:\\user\\outputquery.txt")); // keep getting the next String from the text, separated by white space // and print each token in a line in the output file //while (s.hasNext()) { // String token = s.next(); // System.out.println(token); // out.write(token + "\r\n"); //} String strLine=""; String str=""; while ((strLine = br.readLine()) != null) { str+=strLine; } String st=str.replaceAll(" ", ""); char[]third =st.toCharArray(); System.out.println("Character Total"); for(int counter =0;counter<third.length;counter++){ //String ch= "a"; char ch= third[counter]; int count=0; for (int i=0; i<third.length; i++){ // if (ch=="a") if (ch==third[i]) count++; } boolean flag=false; for(int j=counter-1;j>=0;j--){ //if(ch=="b") if(ch==third[j]) flag=true; } if(!flag){ System.out.println(ch+" "+count); out.write(ch+" "+count); } } // close the output file out.close(); } catch (IOException e) { // print any error messages System.out.println(e.getMessage()); } // optional to close the scanner here, the close can occur at the end of the code finally { if (s != null) { // close the input file s.close(); } } }
-1
A
回答
0
這樣的事情我不會推薦的Java雖然它完全有可能是與GAWK或類似的東西容易得多。 GAWK也有類似於java的語法,所以很容易上手。你應該檢查一下。
0
SO並不是真的提出這樣一個廣泛的問題的地方,但我會轉介到regular expression and text match in Java的下一頁。另外,請查看Javadocs for regexes。
如果你按照這個鏈接你應該得到你想要的,否則你可以發佈一個更具體的問題回到SO。
+0
這些字符在servlet html文件中顯示爲問號?爲什麼 »1 ¡1 Г14 ¶4 ¤4 Ї1 ў1 – krest 2012-04-07 22:04:45
相關問題
- 1. 如何使用ORD和CHR只有A到Z和0到9?
- 2. Java正則表達式a-z,A-Z,0-9和(。)(_)( - )
- 3. [a-z] +,[A-Z] +,[0-9] +和awk中的標誌命令
- 4. 取代a-z,A-Z,0-9以外的任何東西或任何空格
- 5. 允許a-z,A-Z,0-9,空格和 - 。 preg_match
- 6. 正則表達式匹配A-Z,A-Z,0-9,_和
- 7. 我如何搜索和複製只搜索到下一列
- 8. php簡單編碼解碼功能僅使用(0-9 A-Z a-z)
- 9. 測試字符串是否是Java變量標識符:(a-z,A-Z,_,$)後跟(a-z,A-Z,0-9,_,$)
- 10. 針對{[A-Z] [a-z] [0-9]}的QLineEdit驗證文本輸入
- 11. 如何只計算只包含A-Z和a-z的單詞?
- 12. 如何從字符串中獲取A-Z和0-9?
- 13. 瞭解正則表達式\?[A-Z] {2} [a-z] {2} [0-9] {2}!
- 14. RegEx匹配以下字符A-Z,a-z,0-9,? ! $,'。
- 15. VBScript將字符串轉換爲a-z A-Z 0-9
- 16. symfony2實體驗證正則表達式a-z A-Z 0-9
- 17. 正則表達式:字符串必須包含a-z和A-Z和0-9
- 18. 如何僅從Java文本文件中讀取字母(a-z || A-Z)和數字(0-9)字符?
- 19. REGEX a-z 0-9但不只是數字
- 20. 只允許a-Z,0-9使用preg_match,preg_replace
- 21. PHP正則表達式用於檢查A-Z a-z 0-9 _和
- 22. 允許0-9,a-z,A-Z和特殊字符。 ,@&(){} []:;在文本框中
- 23. 對於0-9,a-z,A-Z區分大小寫to_s和to_i方法?
- 24. 如何讓我的java代碼編譯?
- 25. 我如何修復從我的Vigenere密碼中'z'回到'a'的循環
- 26. 如何添加a-z過濾器到wordpress搜索結果?
- 27. postgres regexp_replace只允許a-z和A-Z
- 28. 我如何讓我的代碼說「你不能分爲0」
- 29. PHP - 檢查形式的preg_match正確的文本(A-Z,A-Z,0-9)
- 30. 如何通過htaccess在URL中允許1-9 a-z A-Z - _%?
它的Java功課。 – krest 2012-04-07 17:21:33