I am trying to read in a .txt file which acts as a database from my Java program. I have written to the file before running the program via TextEdit and using Java FileWriter class but the program remains to show an empty array after running these lines of code. This is the only bit that is creating the problem and the remaining parts of my code (not shown here) run perfectly fine when I use an ArrayList as a temporary database. Can someone help me find out why is the .txt file always empty?
String filePathName = "User Data.txt";
File userFile = new File(filePathName);
if (userFile.createNewFile()) {
System.out.println("Database has been successfully created");
System.out.println();
}
Scanner fileReader = new Scanner(userFile);
FileWriter fileWriter = new FileWriter(userFile);
Scanner userInput = new Scanner(System.in);
ArrayList<User> userInfo = new ArrayList<>();
boolean actionValid = false;
StringBuilder fileInput = new StringBuilder();
while (fileReader.hasNext()) {
fileInput.append(" ").append(fileReader.next());
}
String[] fileList = fileInput.toString().split(" ");
System.out.println(Arrays.toString(fileList));