使用FileWriter并保持LineBreaks / WhiteSpace

时间:2018-11-07 07:06:02

标签: java oop

您好,我目前正在使用Java FileWriter系统和HashTable来重新创建解码后的消息。我正在尝试通过遍历包含所有编码键值对(10111 = C)的文件并写入文本文件来重新创建解码消息

我的编码KV对 https://github.com/DijonLee/Project2/blob/master/freq.txt

prop.load(new FileReader(freqFile));
for (Map.Entry entry : prop.entrySet()) {
    // map.put((String) entry.getKey(), (String) entry.getValue());
}
BufferedReader in = new BufferedReader(new FileReader(freqFile));

String line;
while ((line = in.readLine()) != null) {
    // System.out.println(line); // ensure my line breaks are okay

    if (line.contains("=")) {
        String[] strings = line.split("=");
        map.put(strings[0], (strings[1]));
    }
}

在这里,我尝试根据KV对“解码”我的文字

FileReader inputStream = null;
inputStream = new FileReader("encoded.txt");

FileWriter outputStream = null;
outputStream = new FileWriter("ur_dec.txt");

int c;
String decoder = "";
String key = "";

while ((c = inputStream.read()) != -1) {  // loop through file
    char cToChar = (char) c;              // get char
    decoder += cToChar;                  // build string
    if (map.containsValue(decoder)) {
        for (Map.Entry entry : map.entrySet()) {
            if (decoder.equals(entry.getValue())) {
                key = (String) entry.getKey();
                decoder = "";
                outputStream.write(key); // 
            }
            break; // breaking because its one to one map 
        }
    }
}

尽管我的程序可以正常工作,但它似乎会在我编码和解码时去除我想保留的制表符和其他空白,而且我不太确定为什么

0 个答案:

没有答案