PrintWriter没有写入文件,但split()似乎工作正常吗?

时间:2016-11-29 17:44:46

标签: java printwriter

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.Arrays;
import java.util.LinkedList;

public class ProjectTwoAccessControl{


 // public static void fOrAAdd(String x,PrintWriter n){
//      n.write(x);
//  }


public static void main(String [] args) throws IOException{
    PrintWriter friends = new PrintWriter("friends.txt"); 
    PrintWriter audit = new PrintWriter("auditlog.txt");



try {
FileInputStream inputCommands;

    inputCommands = new FileInputStream("test.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(inputCommands));
    String line;
    while ((line = br.readLine()) != null){ //2
        String[] command = line.split(" ");
        System.out.println(Arrays.toString(command));
//      for(int i = 0; i < command.length;i++){ //1
        if (command[0] == "friendadd"){
            friends.write(command[1]);
            audit.write("Friend " + command[1] + " was added.");

        }
//      } //1

} //2
}//try end
catch (FileNotFoundException e) {
    System.out.println("No file found by this name");
}
    friends.close();
    audit.close();

} 








}

我正在尝试为学校作业制作社交媒体风格的访问控制,并且我将在整个晚上继续工作,但我无法弄清楚为什么PrintWriters没有写作。我加入来打印数组&#34;命令&#34; out,它似乎工作正常,但没有任何东西会打印到我的friends.txt或audit.txt。

1 个答案:

答案 0 :(得分:-3)

zack是对的,你需要在PrintWriter上调用flush(),因为close()不会调用flush()