我正在尝试将程序的输出发送到名为results.txt的文本文件。这是我的尝试
public void writeFile(){
try{
printstream r = new printstream(new File("Results.txt"));
printstream console = System.out;
System.setOut(r);
} catch (FileNotFoundException e){
System.out.println("Cannot write to file");
}
但是每次我运行代码并打开文件时,文件都是空白的。这是我要输出的内容:
public void characterCount (){
int l = all.length();
int c,i;
char ch,cs;
for (cs = 'a';cs <='z';cs++){
c = 0;
for (i = 0; i < l; i++){
ch = all.charAt(i);
if (cs == ch){
c++;
}
}
if (c!=0){
//THIS LINE IS WHAT I'M TRYING TO PRINT
System.out.println("The character"+ " "+ cs + " "+ "appears --> "+" "+c+" "+ "times");
}
}
}
我在哪里出错,它会继续创建文件但不写入文件? (顺便说一句,我确实有一个主要方法)