Files and reading data
Table of contents:
- Reading from keyboard
- Reading from a file
- Reading data of a specific format from a file
- Reading Objects From a File
Reading from keyboard
Application that runs on the Internet such as WhatApp, Messanger, Facebook, WhatsApp, and Telegram, handle user information that is stored in file-based databases. What these all have in common is that they read and manipulate data in one way or another. Also, the data being handled is ultimately stored in some format in one or more files.
Me, in no way responsible for the Facebook site crash….. pic.twitter.com/qsZE8cMbnV
— The Power of Chris Compels You! 👻 (@thechriswatt) October 4, 2021
In this example, we pass system input System.in
as a parameter ti the constructor of the Scanner
class. The input if the user is directed into the input stream one line at a time, which means that the information is send to be handled every time the user enters a new line.
Example:
Reading from a file
Example:
File is read from the project root by deafault, when new Scanned(Path.get("example_file.txt"))
is called. Folder that contains the folder src
and the filepom.xml
.
Read all the lines in the file, example:
Reading data of a specific format from a file
Data is often stored in files using a specific format. One such format that’s already familiar to us is comma-separated values (CSV) format, i.e., data separated by commas.
Example:
Reading Objects From a File
Example:
Reading objects from a file is a clear responsibility in and of itself, and should for that reason be isolated into a method.
Reference:
My site is free of ads and trackers. Was this post helpful to you? Why not
Disqus is great for comments/feedback but I had no idea it came with these gaudy ads.