Strings and IO Flashcards

1
Q

How can ranges be extracted from a string?

A

using string.subString(start, end).

Note that the second parameter is the index of the character AFTER the last character.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the result of “abcd”.subString(1, 3)?

A

“bc” - the second index is of the character AFTER

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How are strings compared?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are substrings found in a string?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How can regular expressions be matched to a whole string?

A

use string.matches(pattern) to check the format of a whole string OR test if it contains a particular substring

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the basic regex syntax?

A

^ 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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How are backspaces used in Java strings?

A

They are escaped with a another backslash i.e. “\” = \

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How are groups represented in regex?

A

With brackets i.e. ([A-z][0-9]){3}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does a full stop represent in regex?

A

Any character.

Hence use . to match a literal period

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How is any digit matched?

A

with .

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How are 0 or 1 repetitions matched?

A

With ?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How are 1 or more repetitions matched?

A

With +

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does a ? represent?

A

The previous character / pattern may occur 0 or 1 time

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does a + represent?

A

The previous character / pattern may occur one of more times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What does a * represent?

A

The previous character / pattern may occur any number of times

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How are any number of repetitions matched?

A

With *

17
Q

What does [^a] match?

A

Anything but a

18
Q

How is whitespace / non-whitespace matched?

A

\s matches any whitespace character; \S matches anything that isn’t whitespace

19
Q

How is any letter matched?

A

[a-zA-Z]

20
Q

How is anything buy x, y, or z matched?

A

[^xyz]

21
Q

How can strings be replaced based on regex?

A

With string.replaceAll(pattern, substring)

Alternatively using .replaceFirst()

22
Q

How is regex implemented behind the scenes?

A

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

23
Q

How many bytes is a character?

A

Two

24
Q

How are file paths represented and what behaviours do they have?

A

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()

25
Q

How are files represent and what behaviour does this allow?

A

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)
26
Q

What does a DirectoryStream path do?

A

Provide an iterable data structure of objects with the Path interface

27
Q

How are files written to?

A

Create a Formatter with the file path (string) as its parameter.

use .format(…) to write and then .close() to finish with it

28
Q

How are files read?

A

Create a scanner Scaner(path). Then use the scanner as usual.

29
Q

What is the process of converting an object to a series of bytes called?

A

Serialisation.

This is commonly used to save objects to files

30
Q

How can an object be made serialisable?

A

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.

31
Q

How is a serialisable object written to a file?

A

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

32
Q

How is a serialisable object read from a file?

A

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()

33
Q

How is information streamed to and from disk?

A

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