我的代码无法正常工作,我正在尝试解密消息,但我得到类似 , 0, 3, ,, , 5, 7, <, ;, , ;, 9, ,, (, 4, , , -, ,, ), (, , �, ]
的信息
请帮我看看我哪里错了:
public class WorkInFile {
public static void main(String[] args) throws IOException {
FileInputStream encoded=new FileInputStream("C://Games//encoded.txt");//contains ƪÄÖØÐîÃÜÙäÌÊÛÓÕÒáÄßÕÍǨ³¾êÉàÝâÝãƒâÝäìÚÇäÖçÅáâÄÄÌØÐƭèÑØǑÚÚŲã¨
FileInputStream coded = new FileInputStream("C://Games//code.txt");//contains icbakwtbxxvcelsmjpbochqlltowxhlhvhyywsyqraargpdsycikmgeakonpiwcqmofwms
String text = encoded.toString();
String text2=coded.toString();
char[] chars=text.toCharArray();
char[] chars2=text2.toCharArray();
int index=0;
char[] res=new char[text.length()];
for (char aChar : chars) {
for (char c : chars2) {
res[index] = (char) (aChar - c);
}
index++;
}
String result= Arrays.toString(res);
System.out.println(result);
}
}
最佳答案
Files.readAllBytes(Paths.get("file-path"))
Java 现在提供了一种漂亮的单行代码来读取文件内容。
以下是将文件内容作为字符串获取的工作代码:
// WorkInFile.java
import java.nio.file.*;
public class WorkInFile {
public static void main(String[] args) {
try {
String text = new String(Files.readAllBytes(Paths.get("encoded.txt")));
System.out.println("Encoded.txt = " + text);
String text2 = new String(Files.readAllBytes(Paths.get("code.txt")));
System.out.println("code.txt = " + text2);
} catch (Exception ex) {
System.out.println(ex.toString());
}
}
}
关于Java,解密文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59729744/