这是一种加密/解密文件(SourceFile.txt)并将结果保存在另一个文件(ConvertedFile.txt)中的方法
private void crypto(int index, int k) throws IOException// 0 -> encrypt 1 -> decrypt
{
FileReader in;
FileWriter out;
try
{
in = new FileReader("SourceFile.txt");
out = new FileWriter("ConvertedFile.txt");
}
catch(FileNotFoundException e)
{
System.out.println("File NOT Found.");
return;
}
c = new cipher(k);
Scanner sc = new Scanner(in);
String line,token;
StringTokenizer st;
while(sc.hasNextLine())
{
line = sc.nextLine();
st = new StringTokenizer(line," ");
while(st.hasMoreTokens())
out.write(c.convert(st.nextToken(),index)+" ");
out.write(10);
}
in.close();
out.close();
}
如果源文件中有类似的内容
玛丽有
一点
羊肉
转换后的文件包含加密内容
Qevc 列赫 epxxpi
无论如何,我可以如何使用 FileWriter 在每行末尾打印 '\n'(新行)?
我什至尝试了 out.write(10)... 没用
最佳答案
使用PrintWriter
PrintWriter p = new PrintWriter(out);
p.println(str);
关于java - 使用 FileWriter 打印新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236021/