Reputation: 11
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));
Upvotes: 1
Views: 1122
Reputation: 25903
You have
Scanner fileReader = new Scanner(userFile);
FileWriter fileWriter = new FileWriter(userFile);
So your reader and writer are both tied to userFile
. While this is already a bad idea to begin with (they will interfere with each other), in this particular case it also causes your problem.
The reason why is because new FileWriter(userFile)
will truncate the file before it starts writing. Hence the file is also empty for your fileReader
immediatly.
You can open a FileWriter
in append-mode by adding true
as second parameter (check the documentation) but as said, the bigger problem is that you are having a reader and writer tied to the same resource at the same time.
I do not see you using the fileWriter
in your code snippet. Hence I am suggesting to first finish all your reading and creating the fileWriter
later, when you actually need it, after you are fully done reading it.
In case you actually want to read and write at the same time, you should prepare your new file content in a List<String>
first and delay the actual writing to the moment when you are fully done writing.
One last note, prefer using NIO for such file operations. It is much simpler to use and will also ensure that you do not keep the streams open longer than necessary.
This section looks odd to me:
StringBuilder fileInput = new StringBuilder();
while (fileReader.hasNext()) {
fileInput.append(" ").append(fileReader.next());
}
String[] fileList = fileInput.toString().split(" ");
You are reading the file and preparing a StringBuilder
just to split the result again. This is very wasteful. What you actually should use here is a List
:
List<String> fileList = new ArrayList<>();
while (fileReader.hasNext()) {
fileList.add(fileReader.next());
}
With NIO you could throw away your scanner and simplify to:
List<String> fileList = Files.lines(userFile.toPath())
.collect(Collectors.toList());
Upvotes: 3