This is very strange and I do not see but there must be several mistakes...
1) Why does it not read the entire file? I thought by using fileReader.read(buffer); It would read and put all the data in a buffer
2) When I split the buffer and want to read it back it only write 2 of the 5 words back with digits. Something wrong with my regex. If you have a good cheatsheet for regex I would be delighted. It is a shame that CodeGym has have one.
3) When I write things to the console it doesn't write character/strings just 2 strange memory locations. I have no idea why?
4) It writes only 2 words to the outputfile but also in a strange memorylocation code. I thought that fileReader and FileWriter both work with characters. I'm very confused what is happening....
Thanks for your help in advance!!
package com.codegym.task.task19.task1923;
/*
Words with numbers
/Users/lilianetop/Desktop/taak1916a.txt contains 8 words
/Users/lilianetop/Desktop/taak1916b.txt should contain 5 words
*/
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Solution {
public static void main(String[] args) throws IOException {
String input = args[0];
String output = args[1];
FileReader fileReader = new FileReader(input);
char[] buffer = new char[fileReader.read()];
while (fileReader.ready()) {
fileReader.read(buffer);//it doesn't read all the words why?
}
fileReader.close();
FileWriter fileWriter = new FileWriter(output);
String[] words = buffer.toString().trim().split(" ");
for (String word : words) {
System.out.println(word.toString());
fileWriter.write(word + " ");
if (word.matches(".+[0-9].+")) {
fileWriter.write(word + " ");
System.out.println(word + " ");
}
}
fileWriter.close();
}
}