我正在尝试构建一个程序,该程序需要一个文本文件,应用CaesarCipher方法并返回并输出文件。
import java.util.*;
import java.io.*;
class CaesarCipher
{
public static void main (String [] args) throws FileNotFoundException {
System.out.print("What is the input file name? ");
Scanner keyboard = new Scanner(System.in);
String fileName = keyboard.nextLine();
Scanner inputFile = new Scanner (new File (fileName));
String inputFileString = inputFile.toString();
System.out.print("What is the input file name? ");
int s = 4;
System.out.println("Text : " + inputFileString);
System.out.println("Shift : " + s);
System.out.println("Cipher: " + encrypt(inputFileString, s));
}
public static String encrypt(String inputFileString, int s) {
StringBuilder result = new StringBuilder();
for (int i=0; i< inputFileString.length(); i++) {
if (Character.isUpperCase(inputFileString.charAt(i))) {
char ch = (char)(((int)inputFileString.charAt(i) + s - 65) % 26 + 65);
result.append(ch);
}
else {
char ch = (char)(((int)inputFileString.charAt(i) + s - 97) % 26 + 97);
result.append(ch);
}
}
return result.toString();
}
}
我有两个问题: 1-程序正在编译,但是当我运行它并输入文本文件名时,出现此错误:
什么是输入文件名?文字: java.util.Scanner [delimiters = \ p {javaWhitespace} +] [position = 0] [match 有效=假] [需要输入=假] [来源 已关闭=否] [已跳过=否] [组分隔符= \,] [小数 分隔符=。] [正前缀=] [负前缀= \ Q- \ E] [正 suffix =] [负后缀=] [NaN string = \Q�\ E] [infinity string = \Q∞\ E]
2-如何构造一个包含编码文本的新outPut文件?
答案 0 :(得分:1)
您现有的代码 不是 实际上将输入文件读取到String
中。您可以使用多种方法来执行此操作,例如Files.readAllLines(Path)
,它会返回List<String>
个文件行。流式处理,并使用行分隔符收集它。喜欢,
String inputFileString = Files.readAllLines(new File(fileName).toPath()).stream()
.collect(Collectors.joining(System.lineSeparator()));
关于写入文件,请看一下PrintStream(File)
构造函数。
答案 1 :(得分:1)
它与StringBuilder一起使用
StringBuilder inputFileString = new StringBuilder();
while (inputFile.hasNext()) {
String x = inputFile.nextLine();
inputFileString.append(x);
}
答案 2 :(得分:0)
要完全回答您的问题:
File file = new File("your/file/path/file.ending");
try (FileInputStream fis = new FileInputStream(file)) {
// your code here
} catch (FileNotFoundException e){
e.printStackTrace();
}
总而言之,我创建了一个工作示例,该示例使用略有不同的方式来实现ca撒加密,并使用Files.lines(...)来读取文件内容,并使用Files.write(...)来将加密内容写入到一个新文件:
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Scanner;
import java.util.stream.Stream;
public class CaesarCipher {
public static void main(String[] args) throws IOException {
Scanner scanner = new Scanner(System.in);
System.out.print("What is the input file name? ");
Path filePath = Paths.get(scanner.nextLine());
System.out.print("What is the output file name? ");
Path encodedFilePath = Paths.get(scanner.nextLine());
System.out.print("Caesar cipher offset? ");
final int offset = Integer.parseInt(scanner.nextLine());
// Retrieve a a stream of the input file, where each line is mapped using the encrypt function
Stream<String> mappedFileStream = Files.lines(filePath).map(msg -> CaesarCipher.encrypt(msg, offset));
// Write encrypted content to a new file using our mapped stream as an iterable
Files.write(
encodedFilePath,
(Iterable<String>) mappedFileStream::iterator,
StandardCharsets.UTF_8,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
// Decrypt the encoded file content and print it
Files.lines(encodedFilePath)
.map((fileLine) -> CaesarCipher.decrypt(fileLine, offset))
.forEach(System.out::println);
}
public static String encrypt(String msg, int offset) {
offset = offset % 26 + 26;
StringBuilder encoded = new StringBuilder(msg.length());
for (char i : msg.toCharArray()) {
if (Character.isLetter(i)) {
char base = Character.isUpperCase(i) ? 'A' : 'a';
encoded.append((char) (base + (i - base + offset) % 26));
} else {
encoded.append(i);
}
}
return encoded.toString();
}
public static String decrypt(String enc, int offset) {
return encrypt(enc, 26 - offset);
}
}