-4

我这里有一些基本代码来加密文本文件。我想将文本文件中的每个字符加一,将来可能会有所不同。但我不知道如何将数组“arr”设置为 char 值以进行递增。任何想法都会有所帮助!

import java.io.*;

public class EncryptionMain {
public static void main(String args[]) {
    File file = new File("c:\\Visual Basic Sign in\\password.txt");
    StringBuilder contents = new StringBuilder();
    BufferedReader reader = null;

    try {
        reader = new BufferedReader(new FileReader(file));
        String text = null;

        // repeat until all lines is read
        while ((text = reader.readLine()) != null) {
            contents.append(text)
                    .append(System.getProperty(
                            "line.separator"));
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    int first = 0, second = 1;
    for (int i = 0; i < contents.length(); i++)
    {
    char[] arr = new char[7];
    contents.getChars(first, second, arr, 0); // get chars 0,1 to get 1st char
    char ch = arr.toChar();
        if (ch >= 'A' && ch < 'Z') ch++; 
        else if (ch == 'Z') ch = 'A'; 
        else if (ch >= 'a' && ch < 'z') ch++; 
        else if (ch == 'z') ch = 'z'; 

    System.out.println(arr);
    first++;
    second++;
    }
}

}

4

1 回答 1

2

您需要数组访问运算符[]来获取和设置数组元素。要得到...

char ch = arr[i];

稍后将其分配回数组...

arr[i] = ch;

另外,我认为您不想映射'z'到自身。改变

else if (ch == 'z') ch = 'z'; 

else if (ch == 'z') ch = 'a';
于 2013-06-13T16:44:44.923 回答