2016-11-28 42 views
0

我有一個應用程序,我必須讀取.txt文件,以便我可以存儲一些值並保留它們。除了我想讓這些值對外部用戶不可讀或「不可理解」這一事實之外,這種方法工作得很好。字節加密的文本文件

我的想法是將文件內容轉換爲十六進制或二進制文件,並在讀取過程中將其更改回Char。問題在於我無法訪問由於編譯器而導致的String.Format等方法。

這裏是我當前如何閱讀和保存的值:

   byte[] buffer = new byte[1024]; 
       int len = myFile.read(buffer); 
       String data = null; 
       int i=0; 
       data = new String(buffer,0,len); 

類來打開和操作文件:

public class File { 
    private boolean debug = false; 
    private FileConnection fc = null; 
    private OutputStream os = null; 
    private InputStream is = null; 
    private String fileName = "example.txt"; 
    private String pathName = "logs/"; 
    final String rootName = "file:///a:/"; 

    public File(String fileName, String pathName) { 
     super(); 
     this.fileName = fileName; 
     this.pathName = pathName; 
     if (!pathName.endsWith("/")) { 
      this.pathName += "/"; // add a slash 
     } 
    } 


    public boolean isDebug() { 
     return debug; 
    } 

    public void setDebug(boolean debug) { 
     this.debug = debug; 
    } 


    public void write(String text) throws IOException { 
     write(text.getBytes()); 
    } 


    public void write(byte[] bytes) throws IOException { 
     if (debug) 
      System.out.println(new String(bytes)); 
     os.write(bytes); 
    } 


    private FileConnection getFileConnection() throws IOException { 
     // check if subfolder exists 
     fc = (FileConnection) Connector.open(rootName + pathName); 
     if (!fc.exists() || !fc.isDirectory()) { 
      fc.mkdir(); 
      if (debug) 
       System.out.println("Dir created"); 
     } 
     // open file 
     fc = (FileConnection) Connector.open(rootName + pathName + fileName); 
     if (!fc.exists()) 
      fc.create(); 
     return fc; 
    } 

    /** 
    * release resources 
    */ 
    public void close() { 
     if (is != null) 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     is = null; 
     if (os != null) 
      try { 
       os.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     os = null; 
     if (fc != null) 
      try { 
       fc.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     fc = null; 

    } 

    public void open(boolean writeAppend) throws IOException { 
     fc = getFileConnection(); 
     if (!writeAppend) 
      fc.truncate(0); 
     is = fc.openInputStream(); 
     os = fc.openOutputStream(fc.fileSize()); 

    } 

    public int read(byte[] buffer) throws IOException { 

     return is.read(buffer); 
    } 

    public void delete() throws IOException { 
     close(); 
     fc = (FileConnection) Connector.open(rootName + pathName + fileName); 
     if (fc.exists()) 
       fc.delete(); 


    } 

} 

我想知道如何讀的簡單方法這個內容。二進制或十六進制,都會爲我工作。

+0

你使用哪種編譯器不支持String的格式化方法? Java 1.5以後一直存在......如果這是一個選項,會推薦更新你的編譯器。 – Ironcache

+0

這不是因爲我正在開發一款只能使用1.3的設備,可悲的是。 – Lkun

+0

我當然沒有如何做到這一點的第一手知識,但是[這個問題](http://stackoverflow.com/questions/9655181/how-to-convert-a-byte-array-to-a -hex-string-in-java)有幫助嗎? – Ironcache

回答

1

因此,與這個問題有一定的瞭解,我相信你真的找混淆的一種形式?正如評論中提到的那樣,最簡單的方法可能是一種密碼形式。

考慮這個例子一個移位密碼的實現:

常見

int shift = 11; 

寫作

// Get the data to be wrote to file. 
String data = ... 

// cipher the data. 
char[] chars = data.toCharArray(); 
for (int i = 0; i < chars.length; ++i) { 
    chars[i] = (char)(chars[i] + shift); 
} 
String cipher = new String(chars); 

// Write the data to the cipher file. 
... 

閱讀

// Read the cipher file. 
String data = ... 

// Decipher the data. 
char[] chars = data.toCharArray(); 
for (int i = 0; i < chars.length; ++i) { 
    chars[i] = (char)(chars[i] - shift); 
} 
String decipher = new String(chars); 

// Use data as required. 
... 

這是example implementation on Ideone。輸出:

Data : I can read this IP 192.168.0.1 
Cipher : T+nly+}plo+st~+T[+<D=9<AC9;9< 
Decipher: I can read this IP 192.168.0.1 

爲了滿足Java 3的要求,我儘量保持這個儘可能低的水平。

請注意,這是不是安全的任何手段。移位密碼(像泡泡中的大多數密碼)很容易被惡意實體破壞。如果安全性是一個實際問題,請不要使用它。

+0

工作就像一個魅力! 謝謝@Ironache :) – Lkun

0

您的解決方案太複雜。與Java 8,你可以嘗試:

String fileName = "configFile.txt"; 

    try (Stream<String> stream = Files.lines(Paths.get(fileName))) { 
     //TO-DO .Ex 
     stream.forEach(System.out::println); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
+0

用戶指定他們的編譯器不支持String的格式方法......它當然不支持流。 – Ironcache

+0

@tuyendev,正如Ironcache提到的,我只限於1.3ide。提前致謝。 – Lkun