所以教授给了我们以下练习:
我们必须创建一个程序,该程序将从具有已知路径的文本文件中读取数据,然后使用Caesar密码对数据进行加密,最后将加密的文本写入用户通过args具有给定路径的另一个文本文件中。
用户应能够给出移动速率/键。
程序必须通过args []运行。
规则:
* args [0] = the startFile path.
* that shall be already existed and no need to make it.
* args[1] = the endFile path.
* args[2] =the shift key as an int between 0 and 25
* we cant use try/catch so I did use the throws IOException
* the program must read the data from the startFile
* then encrypt it
* the program shall make the output file and write the encrypted data in it.
* then the program shall end.
* the program shall work properly with big text files and I have no idea what does he mean with that!. but obviously, my code does not.
要运行此程序,我在Eclipse运行配置中做了以下操作:
args [0] = start.txt //本地路径
args [1] = end.txt //本地路径
args [3] = 2 //将字母移动一个
INPUT(1)
当然,我已经创建了一个start.txt(转到路径,右键单击新文本文件,然后在其中写入。),将其放在正确的路径中,并在其中写入以下内容:
abcdefghijklmnopqrstuvwxyz
ABCDEFGHIJKLMNOPQRSTUVWXYZ
输出(控制台)
然后我开始运行,作为该程序的输出,我得到了几乎所有我想要的东西:
文件已存在。
现在程序将从起始文件中读取数据!
数据已被扫描!
现在程序将加密起始文件中的数据!
数据已成功加密
现在程序将把加密的数据写入输出文件中!
数据已写入!
完成!
输出(end.txt)
现在,如果我打开end.txt,我只会看到这些奇怪的字母,这些字母对我完全没有意义:
该程序现在只打印大写
INPUT(2)
对于此输入,程序只是发疯了:
输出(2)
程序将文本的最后一行转换为:
所以我认为这与加密方法无关,但与扫描方法无关,我不知道我要缺少的是什么!。
public static void main(String[] args) throws IOException {
String data;//transfer the scanned data to the encrypt method
String encodeData;//transfer the encoded data to the write method
int shift;
System.out.println("enter a start path for the input file and the output file!");
//first arg for the path where the data can be scanned and be encrypted later on
String inPath=args[0];
//second arg for the path of the output txt in which the data will be encrypted
String outPath =args[1];
//to make the output file
makefile(outPath);
System.out.println("now the program will read the data from the start file!");
//just to scan the start txt in which the text shall be encrypted later on.
data=scanFile(inPath);
System.out.println("the data has been scaned!");
System.out.println("now the program will encrypt the data in the start file!");
//third arg is for the shift rate
shift= Integer.parseInt(args[2]);
//the scanned data will be encrypted
encodeData=encrypt(data,shift);
System.out.println("the data has been succesfully encrypted");
System.out.println("now the program will write the encrypted data in the output file!");
//to write the encrypted data in the endfile/outPath.
dateiSchreiben(outPath, encodeData);
System.out.println("the data has been written!");
System.out.println("finished!");
}
/**
*
* @param Path in which the outputfile will be created.
* @return path
* @throws IOException
*/
public static String makefile (String Path) throws IOException{
//to creat the file in the desired path
File inFile= new File (Path);
//to check if the file exists or not.
if (inFile.createNewFile())
{
System.out.println("File is created!");
}else {
System.out.println("File already exists.");
}
return Path;
}
/**
*
* @param path in which the scanned data will be written.
* @param fileContent the data will be written here after being scanned and encrypted.
* @return fileContent
* @throws IOException
*/
public static String dateiSchreiben (String path, String fileContent) throws IOException{
//i used BufferWriter to write in the endFile/outPath
BufferedWriter fileWriter = new BufferedWriter(new FileWriter(path));
//the file content will be taken from the encodedata string
fileWriter.write(fileContent);
//i was told to use flush then close but the pro told its no neccesery in this case.
//relax i dont wanna dive through this now im still noob and right now im focusing on the homework
fileWriter.close();
return fileContent;
}
/**
*
* @param path to scan the startfile/inPath.txt
* @return path
* @throws IOException
*/
public static String scanFile (String path) throws IOException{
String content="";
File inFile= new File (Path);
Scanner in=new Scanner (inFile); //the scanner will get to the given path and scan
//to iterate for each line in the file
while (in.hasNextLine()){
content +=(in.nextLine()+ System.lineSeparator());
}
//close the scanner when finished
in.close();
return content;
}
/**
*
* @param text the scanned data in the txt from the in path.
* @param shift int to tell how much does teh user want to shift the letters./encode key!.
* @return
*/
public static String encrypt (String text, int shift){
//string in which all the encoded data will land and will be givin back through return to the main.
String encryptOut="";
char letter;
//just to avoid the magic numbers and the one letters chars...
//the prof want us to do so pffffff.
char buchZ='Z',buchz='z',buchA='A',bucha='a';
int one = 1;
//To iterate for each letter as single char!.
for (int i = 0; i < text.length() ; i++) {
letter=text.charAt(i);
//if the letter is capital letter!.
if (letter <= buchZ && letter >= buchA) {
letter = (char) (letter + shift);
/*
* by encode we may have overflow with the letter 'Z' and 'z'
* 'Z'=90 'A'=65 so the number after one shift is 91=[ and we have overflow
* so we need to substract 25 and then do -1
* mathmatically its letter=(letter-('Z'-'A')-1)
* but this can also be written like letter=(letter-'Z'+'A'-1)
*/
if (letter > buchZ) {
letter = (char) (letter - buchZ + buchA - one );
}
//so if the letter in the capital letters range put in the string. if not procced to else if.!
encryptOut += letter;
//if the letters are small letters.
}else if (letter <= buchz && letter >= bucha) {
letter = (char) (letter + shift);
if (letter > buchz) {
letter = (char) (letter - buchz + bucha - one );
}
//so if small letters put in the string otherwise procced to else..
encryptOut += letter;
//space and signs shall not be encrypted! :)
}else {
encryptOut += letter;
}
}
return encryptOut;
}
任何建议将不胜感激。
请在尝试为我解释要点或指出我的错误时,请用一些代码说明或解释您的建议,如果有任何优化我的代码的调整,请随时纠正我。