2012-04-07 21 views
-1
  1. 我的java代碼需要運行將近10-15分鐘(輸入文件是7200+行長的查詢列表)。如何在短時間內運行以獲得相同的結果?
  2. 如何讓我的代碼只搜索aA到zZ和0到9?
  3. 如果我不做#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(); 
        } 
         } 
    } 
    

回答

0

這樣的事情我不會推薦的Java雖然它完全有可能是與GAWK或類似的東西容易得多。 GAWK也有類似於java的語法,所以很容易上手。你應該檢查一下。

+0

它的Java功課。 – krest 2012-04-07 17:21:33

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

相關問題