【问题标题】:Encrypt/Decrypt file. ASCII +1 for encryption/decryption加密/解密文件。 ASCII +1 用于加密/解密
【发布时间】:2014-11-05 06:30:13
【问题描述】:

我正在尝试读入一个文本文件,然后将 ASCII 表中的每个字母“加密”/转换为 +1(我也想“解密”,因此为 -1)。所以“a”会变成“b”,“b”会变成“c”等等。我只需要转换字母(忽略其他所有内容,按原样打印)。这部分代码我遇到了问题:

        for(int i = 0; i <= words.size(); i++)
        {                
            for(int j = 0; j <= words.get(i).length(); j++)
            {
                char ch = ' ';
                ch = words.get(i).charAt(j);
                ch += 1;                    
                morewords.add(ch);
            }              
            fileOut.print(morewords.get(i)  + " ");
        }  

我已经想出了如何 +1 字符,但我不确定如何将它添加回数组或正确打印出来(因为“morewords.add(ch)”只会添加字符,而不是将所有字符转换为添加字符串)。 “words.get(i).length()”取数组“words”的整个长度,当我只想要数组中字符串@位置“i”的长度时,它会抛出一个错误,因为长度数组的长度比字符串单词长。我已经坚持了几个小时,我无法弄清楚。我在想也许我不应该将它们作为字符串读入,而应该将它们作为字符读入,这可能更简单?

 public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        ArrayList<String> words = new ArrayList<String>();
        ArrayList<Character> morewords = new ArrayList<Character>();
        String fileName = ""; //Replace Test with this
        File f;
        Scanner fileIn;

        System.out.println("Please enter a file name for encryption: ");
        //fileName = in.nextLine();
        fileName = "Test.txt";


        try
        {
            //Build the file and attach a scanner to it
            f = new File (fileName); 
            fileIn = new Scanner (f);            

            System.out.println(f.exists()); //For errors
            int counting = 0;

            //Reads in indvidual strings. 
            for(counting =0; fileIn.hasNext(); counting++)
            {                
                words.add(fileIn.next());
                System.out.println(words);
            }            

            PrintWriter fileOut = new PrintWriter ("Backwards.txt");

            for(int i = 0; i <= words.size(); i++)
            {                
                for(int j = 0; j <= words.get(i).length(); j++)
                {
                    char ch = ' ';
                    ch = words.get(i).charAt(j);
                    ch += 1;                    
                    morewords.add(ch);
                }              
                fileOut.print(morewords.get(i)  + " ");
            }            

            fileOut.close();            

        }
        catch(FileNotFoundException e)
        {
            System.out.println("Couldn't find file");
        }

    }  

【问题讨论】:

  • 根据 Andrea 的回答,这似乎是一个错字。是吗?

标签: java encryption file-io ascii


【解决方案1】:

首先在for循环中是正确的

for (int i = 0; i <= words.size()-1; i++){}

如果您从 0 开始,则以长度 1 结束

我改变的是

PrintWriter fileOut = new PrintWriter("C:/Backwards.txt");
        for (int i = 0; i <= words.size()-1; i++)
        {
            for (int j = 0; j <= words.get(i).length()-1; j++)
            {
                char ch = ' ';
                ch = words.get(i).charAt(j);
                ch ++; // +=1
                morewords.add(ch);
                fileOut.print(ch);
            }
            fileOut.print(" ");
        }

        fileOut.close();

如果我理解正确,它就会正确输出 =)

这是我的代码

public static void main(String[] args) throws Exception
{
    BufferedReader inChannel = new BufferedReader(new FileReader("C:/script.txt"));
    BufferedWriter outChannel = new BufferedWriter(new FileWriter("C:/output.txt"));
    String toParse = "";
    while ( (toParse = inChannel.readLine()) != null )
    {
        String toWrite = "";
        for(int i=0; i!=toParse.length();i++)
        {
            char c = toParse.charAt(i);
            if(true) //check if must be encoded or not
            {
                c++;
                toWrite += c;
            }
        }
        outChannel.write(toWrite);
        outChannel.newLine();
    }       
    inChannel.close();
    outChannel.close();
}

希望有所帮助

【讨论】:

  • 确实有帮助,谢谢!将 -1 添加到 words.size() 解决了这个问题。另外,感谢您向我展示了一种更简单、更快捷的方法!
猜你喜欢
  • 1970-01-01
  • 2013-05-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多