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 не пишут. Я добавил, чтобы напечатать массив «command», и, похоже, он работает правильно, но ничего не будет печатать на моем файле friends.txt или audit.txt.PrintWriter не пишет файл, но функция split() работает правильно?
Может попробовать промывку PrintWriter после каждой строки, чтобы убедиться, что все ожидающие изменения записываются. – zack6849
Том, что ты был прав, спасибо. – Elmangos