Tự học guitar, lập trình, khiêu vũ, tennis, máy tính

Saturday
Feb 11th
Text size
  • Increase font size
  • Default font size
  • Decrease font size

Home arrow Programming arrow Ngôn ngữ Java arrow JSP - Một ứng dụng từ điển Anh-Việt phần 10
JSP - Một ứng dụng từ điển Anh-Việt phần 10

JSP - Một ứng dụng từ điển Anh-Việt phần 10

Thật ra bạn hoàn toàn có thể giữ nguyên method readFileAsString() và thực hiện 1trong 2 cách sau:

1-Thay đổi file
Arguments  trong Run configuration. Bạn click chuột phải vào TestJSP2 như trong hình, sau đó chọn Run As--->Open Run Dialog...



Trong cửa sổ mở ra bạn click vào
Arguments  tab để nhập nội dung sau vào ô VM Arguments


-Xmx512m



2-Bạn cũng có thể chạy thử trong Command Line. Nhanh nhất là copy TestJSP2.class và Cmd.exe vào thư mục cài đặt Java và chạy:

java -Xms256m -Xmx512m TestJSP2

Giờ đây bạn chạy thử với code sau, bạn sẽ không thấy lỗi nữa. Nhưng chúng ta sẽ cần phải thay đổi GetDemicalValue() vì nó có vấn đề, nhiều trường hợp nó sẽ trả về số âm.


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

public class TestJSP2 {

    public static double GetDemicalValue(String str) {
          String base64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
          double decValue = 0;
          double str_length = str.length();
            for(int i = 0; i
<str_length;i++){               
                 char char_at = str.charAt(i);
                int pos_of_char = base64.indexOf(char_at);
                decValue = decValue + Math.pow(64,str_length-1-i)* pos_of_char;
            }
            return decValue;
     }
    public static String readFileAsString(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            fileData.append(readData);
            buf = new char[1024];
        }
        reader.close();
        return fileData.toString();
    
    }
    public static String readFileAsString2(String filePath)
    throws java.io.IOException{
        StringBuffer fileData = new StringBuffer(1000);
        BufferedReader reader = new BufferedReader(
                new FileReader(filePath));
        char[] buf = new char[1024];
        List Mylist = new ArrayList();
      
        String contents="";
        int numRead=0;
        while((numRead=reader.read(buf)) != -1){
            String readData = String.valueOf(buf, 0, numRead);
            Mylist.add(readData);
            buf = new char[1024];
        }
        reader.close();
        String[] s1 = Mylist.toArray(new String[0]);
        for(int i = 0; i < s1.length; ++i){
            contents = s1[i];
        }
        return contents;
      
    }
    public static void main(String[] args) {

        try{
        String fIndexString = readFileAsString("C:\\anhviet109K.index");
        String fDictString = readFileAsString("C:\\anhviet109K.dict");
       
        int indexOfWord = fIndexString.indexOf("abacist");
        char as9 = (char)9;
        char as10 = (char)10;
        int beginOfPos = fIndexString.indexOf(as9,indexOfWord);
        int endOfPos = fIndexString.indexOf(as9,beginOfPos+1);
        int endOfCont = fIndexString.indexOf(as10,endOfPos+1);
      
      
        String posOfWordInDict = fIndexString.substring(beginOfPos, endOfPos);
        String contOfWordInDict = fIndexString.substring(endOfPos, endOfCont);
        System.out.println("Vi tri và do dai trong file INDEX: "+posOfWordInDict+"=="+contOfWordInDict);
       
        int posOfWordInDictDec = (int)GetDemicalValue(posOfWordInDict);
        int contOfWordInDictDec = (int)GetDemicalValue(contOfWordInDict);
        String contentInDict = fDictString.substring(2456, 2477);
        System.out.println("Noi dung trong file DICT: "+contentInDict);
      
        } catch (IOException e) {
              System.out.println("IOException: " + e);
            }
       
    }

}

 
< Trước   Tiếp >