Module 4: Files and extras Flashcards
How do you create a file?
Use the File class, then create a file object and specify the path on your computer. File is part of the java.io package.
Ex:
File x = new File(“C:\user\text.txt”); if(x.exists()) {System.out.println(x.getName() + “Exists”);} else {System.out.println(“Does not exist”);}
Some file methods:
- canRead() (Tests if the file is readable or not)
- canWrite() (Tests if the file is writable or not)
- createNewFile() (Creates an empty file)
- delete() (Deletes a file)
- length() (Returns the file size in bytes)
- mkdir() (Creates a directory)
- getName() (Returns the file name, if one exists)
- exists() (Checks if a file exists at the specified location)
How do you read from a file?
Use the Scanner class, then pass the file object to the scanner object.
Ex:
try {
File x = new File(“C:\user\text.txt”);
Scanner sc = new Scanner(x);}
catch (FileNotFoundException e) {}
To then read from the file, use the scanner methods to read and close the file.
Ex:
try {
File x = new File(“C:\user\text.txt”);
Scanner sc = new Scanner(x); while(sc.hasNext()) {System.out.println(sc.next());} sc.close();}
catch (FileNotFoundException e) {System.out.println(“Error”);}
It’s a good practice to close a file once you are done working with it.
How do you write to a file?
Use the FileWriter class, create an object then specify the path where you want the file to be created in the parameters. FileWriter is part of the java.io package.
Ex:
import java.io.FileWriter;
try {
FileWriter f = new FileWriter(“C:\user\test.txt”);} catch (Exception e) {System.out.println(“Error”);}
You can then write content to it using the FileWriter objects write() method.
Ex:
FileWriter f = new FileWriter(“test.txt”);
f.write(“Hello there”); f.close();
What is a wrapper class?
A way to use primitive data types as objects. When you use any kind of list/collection you must use a wrapper object.
Wrapper object types:
- Byte (byte)
- Short (short)
- Integer (int)
- Long (long)
- Float (float)
- Double (double)
- Boolean (boolean)
- Character (char)
To create a wrapper object, use the wrapper class instead of the primitive type.
Ex:
Integer myInt = 5;
Wrapper classes are all objects, methods can be performed on them.
Some wrapper class methods:
- intValue()
- floatValue()
- charValue()
What is a regular expression?
A sequence of characters that forms a search pattern. Regular expressions are used to perform all kinds of text search and text replace operations. To use it, import the java.util.regex package. The following classes are in the regex package:
- Pattern (Defines a pattern to be used in search)
- Matcher (Used to search for the pattern)
- PatternSyntaxException (Indicates syntax error in a regular expression pattern)
To create a text search, import Pattern and Matcher, then specify the pattern to be searched for.
Ex:
Pattern pattern = Pattern.compile(“Hello”, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(“Hello world”); boolean matchFound = matcher.find(); if(matchFound) {System.out.println(“Match found”);} else {System.out.println(“Match not found”);}
The compile() methods first parameter is the pattern being searched for, and the second parameter is an optional flag.
The compile() method has flags, which affect how the search is done.
Some compile() flags:
- Pattern.CASE_INSENSITIVE (Letter case is ignored during search)
- Pattern.LITERAL (Special characters in the search will be treated as ordinary characters during search)
- Pattern.UNICODE_CASE (Use with CASE_INSENSITIVE to ignore the case of letters outside of english)
What is a lambda expression?
A short block of code that takes in parameters and returns a value. They are similar to methods but don’t require a name and can be implemented in the body of a method.
Syntax:
(parameter1, parameter2) -> expression
Expressions cannot contain assignments, variables or statements such as if and for and they must immediately return a value. To do more complex operations, use a code block in the expression section.
Ex:
(parameter1, parameter2) -> {Code block}
Lambda expressions are usually passed as parameters to a method.
Ex:
ArrayList<Integer> numbers = new ArrayList<Integer>(); numbers.add(5); numbers.add(9); numbers.add(8); numbers.add(1); numbers.forEach((n) -> {System.out.println(n);});</Integer></Integer>
Lambda expressions can be stored in variables if the variables type is an interface which only has one method. The lambda expression should have the same number of parameters and return the same type as that method.
Ex:
Consumer<Integer> method = (n) -> {System.out.println(n);}; numbers.forEach(method);}}</Integer>