2016-11-29 59 views
-1
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没有写作。我加入打印数组“命令”出来,它似乎工作正常,但没有什么会打印到我的friends.txt或audit.txt。PrintWriter未写入文件,但split()似乎正常工作?

+0

也许尝试刷新PrintWriter的每行之后,以确保所有待定更改写入。 – zack6849

+0

汤姆你是对的,谢谢。 – Elmangos

回答

-3

扎克是正确的,你需要调用flush()上的PrintWriter因为接近()不调用flush()

+0

为什么你认为缓冲区中有东西需要刷新? – Tom