这是一些代码,我发现查看文件夹中的文件xor会转换文件并输出它们。基于我的搜索,我相信它是java,但它没有运行,我得到的只是错误。
final Path indir = Paths.get("C:\\Temp\\crin");
final Path outdir = Paths.get("C:\\Temp\\crout");
final List<Path> contents = new ArrayList<>();
Files.newDirectoryStream(indir).forEach(contents::add);
for (final Path p : contents) {
final byte[] arr = Files.readAllBytes(p);
for (int i = 0; i < arr.length; i++)
arr[i] ^= 0x42;
Files.write(outdir.resolve(p.getFileName()), arr);
}
我在cmd中尝试这个并获取
javac decode.java
ecode.java:1: error: class, interface, or enum expected
final Path indir = Paths.get("C:\\Temp\\crin");
^
decode.java:2: error: class, interface, or enum expected
final Path outdir = Paths.get("C:\\Temp\\crout");
^
decode.java:3: error: class, interface, or enum expected
final List<Path> contents = new ArrayList<>();
^
decode.java:4: error: class, interface, or enum expected
Files.newDirectoryStream(indir).forEach(contents::add);
^
decode.java:5: error: class, interface, or enum expected
for (final Path p : contents) {
^
decode.java:5: error: class, interface, or enum expected
for (final Path p : contents) {
^
decode.java:6: error: class, interface, or enum expected
final byte[] arr = Files.readAllBytes(p);
^
decode.java:7: error: class, interface, or enum expected
for (int i = 0; i < arr.length; i++)
^
decode.java:7: error: class, interface, or enum expected
for (int i = 0; i < arr.length; i++)
^
decode.java:7: error: class, interface, or enum expected
for (int i = 0; i < arr.length; i++)
^
decode.java:9: error: class, interface, or enum expected
Files.write(outdir.resolve(p.getFileName()), arr);}
^
decode.java:9: error: class, interface, or enum expected
Files.write(outdir.resolve(p.getFileName()), arr);}
^
12 errors
这段代码被描述为有效,所以如果我错了,代码不是java那是什么,如果是java,我做错了什么。
更新
尝试更新的代码现在减少错误,我现在得到这个
javac decode.java
decode.java:13: error: ')' expected
Files.newDirectoryStream(indir).forEach(contents::add);
^
decode.java:13: error: illegal start of expression
Files.newDirectoryStream(indir).forEach(contents::add);
^
decode.java:13: error: ';' expected
Files.newDirectoryStream(indir).forEach(contents::add);
^
3 errors
答案 0 :(得分:0)
至少,您需要导入您正在使用的课程。您需要创建一个类,它需要包含main()
方法。像,
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class decode { // <-- by convention, should be Decode
public static void main(String[] args) {
final Path indir = Paths.get("C:\\Temp\\crin");
final Path outdir = Paths.get("C:\\Temp\\crout");
final List<Path> contents = new ArrayList<>();
try {
Files.newDirectoryStream(indir).forEach(contents::add);
for (final Path p : contents) {
final byte[] arr = Files.readAllBytes(p);
for (int i = 0; i < arr.length; i++)
arr[i] ^= 0x42;
Files.write(outdir.resolve(p.getFileName()), arr);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意我上面没有运行您的代码,您需要将文件添加到C:\Temp\crin\
,以便代码读取它们。