Kategorier: Alla - memory - writing - files - reading

av Nathan Hyman för 3 årar sedan

105

Files

Files are essential components for data management and manipulation in programming. Printing to a file involves writing output data into an already existing file, typically utilizing a FileWriter and a PrintWriter.

Files

Files

What is a file?

A file is a collection of data stored in memory
Text files act like arrays

Keep in mind that the array should be visualized vertically, just for correctness. Technically in memory there is no actual directionality to the data's storage, but if asked, illustrate it as a vertical array.

Each line of the array is like a new element, and each even has their own index, like an array!

Organization is really important

Understanding where a file can be accessed is essential for understanding computers as a whole, as every file is organized SOMEWHERE

Files, folders, drives, devices, these are all levels of organization

What can we do with files?

Reading from file
Reading from file, just like printing to file, will need to be directed towards a specific place in memory. SPECIFICITY really really matters, as not specifying WHERE something is in memory will result in your desired file not being read.
Scanners can be used to read from files

.txt specifies that something is a text file, this is EVEN done when writing a file name in quotes.

When declaring a scanner, unless the file is in the same folder as the program, copy and paste the shortcut in memory from your files.

Printing to file
Writing a name for a file, which does not exist in the immediately accessible file of the java project, will CREATE a file.

Need to import and declare PrintWriter

Give a name to the file which will be written to

Appending to file is writing to an already existing file

REMEMBER TO CLOSE YOUR PRINTWRITER, or else your output to the file will NOT SAVE.

You do not need to close your FileWriter, this will happen when you close the PrintWriter which it is contained inside of.

Will need a FileWriter and a PrintWriter

Use the PrintWriter as a "shell for the FileWriter"

When declaring both, remember to write ", true"

Saying "true" effectively tells the program that the named file is Appendable.

In other words, declare the file writer AND the file which it will append to, inside of the printWriters declaration brackets.