Strings and IO Flashcards
How can ranges be extracted from a string?
using string.subString(start, end).
Note that the second parameter is the index of the character AFTER the last character.
What is the result of “abcd”.subString(1, 3)?
“bc” - the second index is of the character AFTER
How are strings compared?
Using string.compareTo(other).
This returns 0 if they are the same, negative value if the parameter precedes the string alphabetically, and a positive value if the parameter comes after the string in the alphabet.
How are substrings found in a string?
With string.indexOf(substring)
This returns the index of the first occurrence. A second parameter can be used to start the search from a particular character index
How can regular expressions be matched to a whole string?
use string.matches(pattern) to check the format of a whole string OR test if it contains a particular substring
What is the basic regex syntax?
^ is the beginning of a string
[a-z] is a lowercase character from a to z
{3,4} isi the previous character pattern occurring 0 to 9 times
\d is the digit 0 to 9
$ is end of string
How are backspaces used in Java strings?
They are escaped with a another backslash i.e. “\” = \
How are groups represented in regex?
With brackets i.e. ([A-z][0-9]){3}
What does a full stop represent in regex?
Any character.
Hence use . to match a literal period
How is any digit matched?
with .
How are 0 or 1 repetitions matched?
With ?
How are 1 or more repetitions matched?
With +
What does a ? represent?
The previous character / pattern may occur 0 or 1 time
What does a + represent?
The previous character / pattern may occur one of more times
What does a * represent?
The previous character / pattern may occur any number of times
How are any number of repetitions matched?
With *
What does [^a] match?
Anything but a
How is whitespace / non-whitespace matched?
\s matches any whitespace character; \S matches anything that isn’t whitespace
How is any letter matched?
[a-zA-Z]
How is anything buy x, y, or z matched?
[^xyz]
How can strings be replaced based on regex?
With string.replaceAll(pattern, substring)
Alternatively using .replaceFirst()
How is regex implemented behind the scenes?
The pattern is compiled into a Pattern object.
Together with the string that is to be matched, this pattern is used to generate a Matcher object that does the actual matching
How many bytes is a character?
Two
How are file paths represented and what behaviours do they have?
They are represented with Path. To create one from a string use the Paths.get(string) static method.
Path objects implement .getFileName(), .isAbsolute() and .toAbsolutePath()
How are files represent and what behaviour does this allow?
The Files class provides static methods to access information about objects. Note that path objects are used for the inputs. For example :
Files.exists(path) Files.isDirectory(path) Files.size(path) Files.getLastModifiedTime(path) Files.newDirectoryStream(path)
What does a DirectoryStream path do?
Provide an iterable data structure of objects with the Path interface
How are files written to?
Create a Formatter with the file path (string) as its parameter.
use .format(…) to write and then .close() to finish with it
How are files read?
Create a scanner Scaner(path). Then use the scanner as usual.
What is the process of converting an object to a series of bytes called?
Serialisation.
This is commonly used to save objects to files
How can an object be made serialisable?
By implementing the Serializable interface. Extra work must be done only if the class contains instance variables that are no themselves serialisable.
Specifically, ObjectsStreamField[] is a static instance variable that identifies which fields should be serialised. Then the methods writeObject and readObject are implemented.
How is a serialisable object written to a file?
By creating an ObjectOutputStream using Files.newOutputStream(path). Note that .bin is often used as the file extension.
Each object can then be written using output_stream.writeObject(). Finally, .close() is used to close it
How is a serialisable object read from a file?
An input stream is created using Files.newInputStream(path).
The object is then read using (ClassType) input.readObject(). Note that the result must be case to the appropriate type.
Finally the stream is closed with .close()
How is information streamed to and from disk?
Using a DataOutputStream and DataInputStream.
These have readByte(), readInt(), readDouble() and corresponding write methods.
Behind the scenes this is stored as a binary file but one that doesn’t store any Java objects