扫描程序在Java中重新启动

时间:2016-11-17 17:23:01

标签: java

我的任务是以64个字符的块读取文本文件,并使用2个不同的进程(称为Substitution和Column Transposition)来加密它。然后,我必须解密它并将其写入另一个文件。

我已经编写并测试了加密和解密两个过程,它运行得非常好。但后来我尝试循环进程,以防输入文件中有超过64个字符。

作为测试用例,我尝试了128个字符的输入文件。不幸的是,结果只给了我两次前64个字符。我已经跟踪了扫描仪的位置并超过了64,但读取的字符从0开始。我不确定问题是什么。

以下是我的代码的相关部分:



public static void main(String[] args) {	
		
		//Declare variables
		Scanner console = new Scanner(System.in);
		String inputFileName = null;
		File inputFile = null;
		Scanner in = null;
		
		do
		{
			//Check if there are enough arguments
			try
			{
				inputFileName = args[1];
			}
			catch (IndexOutOfBoundsException exception)
			{
				System.out.println("Not enough arguments.");
				System.exit(1);
			}
			catch (Exception exception)
			{
				System.out.println("There was an error. Please try again.");
				System.exit(1);
			}
			
			//Check if Input File is valid
			try
			{
				inputFile = new File(inputFileName);
				in = new Scanner(inputFile);
				outputFile = new File(outputFileName);
				out = new Scanner(outputFile);
			}
			catch (FileNotFoundException exception)
			{
				System.out.println("Could not find input file.");
				System.exit(1);
			}
			catch (Exception exception)
			{
				System.out.println("There was an error. Please try again.");
				System.exit(1);
			}
		} while (outputFileName != null && !inputFile.exists());

  
  //Encryption

  //Prepare patterns
		String subPattern = CreateSubstitutionPattern(hash);
		int[] transPattern = CreateTranspositionPattern(hash);
		
		//Apply patterns
		String textContent = "";
		String applySub = "";
		String applyTrans = "";
		do
		{
			textContent = Read64Chars(in);
			applySub = applySub + ApplySubstitutionPattern(textContent, subPattern);
			applyTrans = applyTrans + ApplyTranspositionPattern(applySub, transPattern);
		} while (in.hasNext());
  
  //Decryption

  String encryptContent = "";
		Scanner encrypt = new Scanner(applyTrans);
		String removeTrans = "";
		String removeSub = "";
		do
		{
			encryptContent = Read64Chars(encrypt);
			System.out.println(applyTrans);
			removeTrans = removeTrans + RemoveTranspositionPattern(encryptContent, transPattern);
			removeSub = removeSub + RemoveSubstitutionPattern(removeTrans, subPattern);
		} while (encrypt.hasNext());
		
		console.close();
		in.close();
		encrypt.close();
  System.out.println(removeSub); //For temporary testing
  }

public static String Read64Chars (Scanner in)
	{
		String textContent = "";
		in.useDelimiter("");
		for (int x=0; x<64; x++)
		{
			if (in.hasNext())
			{
				textContent = textContent + in.next().charAt(0);
			}
		}
		return textContent;
	}
&#13;
&#13;
&#13;

请注意我有更多变量来填充args [0]和args [2],但为了简单起见,我删除了它们。

我想知道,一旦扫描仪读取了它的一部分输入,它是否真的消耗了&#34;它,那部分被删除。当通过方法再次声明时,扫描仪是否会重置?例如,声明是仅指向原始扫描仪的输入源,还是指向具有当前属性的实际扫描仪?

2 个答案:

答案 0 :(得分:0)

encrypt是来自Scanner的不同in,当您第一次呼叫Read64Chars时,您将前进64个字符。因此,当您致电encrypt时,Read64Chars(encrypt)从第一个字符开始。您似乎希望两次都使用相同的扫描仪。

此外,将来请以小写字母开头命名您的功能。我感到肮脏的打字...... :)。

答案 1 :(得分:0)

获取整个加密文本的正确解决方案是像这样的代码

public static String encryptedTextFile (Scanner in)
{
    //ArrayList<String> stringBlocksOf64Chars = new ArrayList<String>();
    StringBuilder encryptedTxt = new StringBuilder();
    String currentTxt = "";
    while (in.hasNextLine()) {
        String line = currentTxt + in.nextLine();
        currentTxt = "";
        int i = 0;
        for( ; i < line.length()/64 ; i++){
            currentTxt = line.substring(i * 64, (i+1)*64);
            //TODO - encrypt the text before adding it to the list
            encryptedTxt.append(currentTxt);//encryptedTxt.append(encrypt(currentTxt));
        }
        currentTxt = line.substring(i * 64, line.length());
    }
    encryptedTxt.append(currentTxt);
    /*for(String str : stringBlocksOf64Chars)
        System.out.println(str);*/
    return encryptedTxt.toString();
}

您的循环for (int x=0; x<64; x++)确保您始终只读取前64个字符而不是完整文件。为了解决这个问题,你应该逐行读取整个文件。

上面的代码块遵循这个想法。

打破逻辑的步骤。

  1. 使用扫描仪逐行读取文件。
  2. 将每行分成64个字符的块,并一次加密块64个字符
  3. 生成加密文本,添加加密的64个字符。
  4. 无论您做什么,首先要分解您希望在代码中使用的逻辑/步骤,以使其更易于理解或编码。 将行分为64个字符