You will have to write them into the file as strings to be able to read them. The FileOutputStream is meant to write raw data as it says in the documentation. The docs also say that for writing characters you should consider FileWriter instead.
To convert the integer into a string you could do or a more quick-and-dirty way could be to simply concatenate it with a string, like this:
Here is a small example which does what you want
Notice the try-with-resources which closes the FileWriter automatically.
A note on exception handling: you should always catch the most specific exception that is practical. For example, if your writer throws an IOException, you should catch the IOException, instead of a blanket Exception.
Why do so few of the writer classes seem to take a Path as a constructor parameter?
Maybe I would use a formatter, which gets you out of using \n because you write %n. You can use this sort of method and pass the Writer to the Formatter constructor. You would still use try with resources, as you correctly mentioned. You are also right about always using classes with names like XYZWriter. I might also use an IntStream:-Note that range will run 0...999, so I wrote i + 1 to get 1...1000. You can pass a buffered writer to a Formatter constructor because it implements Appendable.
[edit: additional] The %d tag allows you to convert the int to a String and write it all in one action.
Thank you Campbell, I appreciate your tips! I haven't yet really studied IO, but I will definitely look into the classes you brought up. IO is still on my list; right now I am trying to conquer that many-headed monster Modules.
That's a pleasure We all learn from this forum, even those of us who have been here for 14 years.
I haven't yet really studied IO . . .
If you go to archive.org and find the wayback machine, you can find older versions of the Java™ Tutorials. This section of the current (Java8) Java™ Tutorials version will tell you about IO using the NIO package.