0

I was looking back at some older class assignments and for one the user had to provide a text file which would be encoded according to an encryption key the user also gave. I essentially solved the problem by placing the content of the text file within a string, retrieving each letter from the string and encrypting it, and then printing the encrypted character back into the same text file. The problem is my professor docked 5% for storing the whole file content within a string, writing something like: "What if the file contents were very large?" I even recall the few people I talked to after the project was graded saying they lost points for the same reason.

At the time I thought he made sense and was too overburdened by my workload so didn't bother seeing if I could fix it because it seemed to be reasonable and simple enough. However now I can't understand how one would be able to edit the text file directly or write on the same file without storing the entire string (because one would otherwise lose its content). How would someone even go about this? Thank you!

Edit: whomever marked my thread as a duplicate to the one above clearly did not understand my question. I am asking how to manipulate the same file without using an absurd amount of memory as the solution I stated would. The other thread clearly asks what the quickest way to read from a file is, which is not at all the same thing. Joop had the right idea of what I meant so I'll try just that, thank you Joop.

  • And what have you tried already? – DimaSan Sep 01 '16 at 09:01
  • 1
    Look at `InputStream` (or `Reader` for text files). They allow you to read through files byte by byte (or char by char). – Thilo Sep 01 '16 at 09:02
  • it might be that he was out for the maximum size of a `String` which is bound to `MAX_INT`. Having content greater than this size could lead to exceptions – SomeJavaGuy Sep 01 '16 at 09:02
  • 1
    If the file has line breaks try http://stackoverflow.com/questions/5868369/how-to-read-a-large-text-file-line-by-line-using-java – suman j Sep 01 '16 at 09:03
  • 1
    One can read a file write to a second file piecewise, and afterwards delete the original file. That would not require memory so not be resource hungry. It would also be a bit more work. Hence the difference in points. – Joop Eggen Sep 01 '16 at 09:08
  • @Joop Thank you, I really appreciate it. I imagined it would be along those lines. I will have to try this in the morning. – Adrian Herrmann Sep 01 '16 at 12:57
  • @Thilo that's not what I meant, I was trying to see how one would edit a text file and write back to it without having to store an absurd amount of text in a string, which has a limit. – Adrian Herrmann Sep 01 '16 at 12:57
  • "Write back to (the same file)". Overwrite in place is a bit tricky (and only really possible if it is the same size). I'd write to a temporary file, then delete the original and rename the temporary file to match the original name. – Thilo Sep 01 '16 at 22:57
  • If you really want to update in-place, your duplicate is this one: http://stackoverflow.com/questions/13258714/use-java-to-modify-file-contents-in-place But I'd go with the method suggested by @JoopEggen (and the duplicate you did not like) and write to a new file. – Thilo Sep 01 '16 at 23:00

0 Answers0