gpt4 book ai didi

java - Java中的流、缓冲区

转载 作者:行者123 更新时间:2023-12-02 08:25:01 31 4
gpt4 key购买 nike

我有java考试,当我学习时,我看到了这个练习,我尝试解决它,但我发现一些困难,所以请帮助我考虑实用程序中方法的以下注释、 header 和部分代码名为 Atbash 的加密类。

    /**
* Prompts the user for the pathname of a source .txt file to encrypt.
* Prompts the user for the pathname of a destination .txt file
* to which the encrypted text will be written.
* The pathnames are then used to create two File objects.
* The method then attempts to open streams on both files, one to
* read from the source file, the other to write to the destination file.
*
* The method then reads from the read stream a line at a time.
* Each line read is converted to uppercase using the message
* toUpperCase(). The method then constructs an encrypted string
* into which all the alphabetic characters from the line are
• substituted by their atbash equivalent, however any numeric or
* punctuation characters are simply added unchanged to the encrypted
* string. After each string is constructed it is written to the write
* stream followed by a newline character.
*/
public static void encryptFile()
{
OUDialog.alert("Please choose a file to encrypt");
String pathname = OUFileChooser.getFilename();
File aFile = new File(pathname);
OUDialog.alert("Please give a file name for the encrypted file");
String pathname2 = OUFileChooser.getFilename();
File bFile = new File(pathname2);
BufferedReader bufferedFileReader = null;
BufferedWriter bufferedFileWriter = null;

try
{
String currentLine;
bufferedFileReader = new BufferedReader(new FileReader(aFile));
bufferedFileWriter = new BufferedWriter(new FileWriter(bFile));
String codedLine = "";
char currentChar;
String alphabet ="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String mirror ="ZYXWVUTSRQPONMLKJIHGFEDCBA";
currentLine = bufferedFileReader.readLine();
while (//Boolean condition to be written as your answer for part (i))
/**this what I do while(currentLine!=null)*/

{
//Statement block to be written as your answer for part (ii)

}
}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
finally
{
try
{
//Statement block to be written as your answer for part (iii)
/* bufferedFileReader.close();
* bufferedFileWriter.close();
*/

}
catch (Exception anException)
{
System.out.println("Error: " + anException);
}
}
}

(i) 写出上述方法中 while 循环的 boolean 条件。

while(currentLine= bufferedFileReader.readLine()!= null)
{

}

(ii) 编写上述方法中 while 语句 block 的代码。这应该确保当 while 循环终止时,目标 .txt 文件包含源 .txt 文件的加密版本。您的代码应该使用变量 bufferedFileReader、bufferedFileWriter、currentLine、codedLine、currentChar、字母表和镜像。您可能会发现 String 协议(protocol)中的以下两条消息很有用:charAt(int) 返回参数指定索引处的 char 值和indexOf(char)返回第一次出现的索引接收者内部的争论。

(iii) 编写最后一个 try block 的代码。

bufferedFileReader.close();
bufferedFileWriter.close();

最佳答案

这里

/**this what I do while(currentLine!=-1)*/

由于 currentLineString,因此您无法将其与 int 值进行比较。您应该检查 BufferedReader.readLine() 的 Javadoc查看它如何指示没有更多数据可供读取,然后相应地修复该情况。

关于java - Java中的流、缓冲区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4689883/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com