To upgrade Java knowledge that stopped at "1.4" I am learning about Java7,8,9. This time, I will only describe what I investigated about the functions added in "7".
You can now omit generics when instantiating.
List<Map<String, List<String>>> gyunyu = new ArrayList<>();
NIO2 A file system related API has been added.
try {
Path path = Paths.get("gyunyu.txt");
List<String> list = Files.readAllLines(path, Charset.forName("UTF-8"));
for (String gyunyu : list) {
System.out.println(gyunyu);
}
} catch(IOException e) {
e.printStackTrace();
}
try {
List<String> list = new ArrayList<>();
list.add("Lemon milk");
Path path = Paths.get("gyunyu.txt");
Files.write(path, list, Charset.forName("UTF-8"), StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
java.nio.file.Path interface. An object that can be used to identify the path of a file or directory
java.nio.file.Paths
Class for creating Path object
--Paths.get (URI uri)
: Converts the specified URI to a Path object
java.nio.file.Files
A class that collects processing related to files or directories
--Files.readAllLines (Path path, Charset cs)
: Read all lines from the file
--Files.write (Path path, Iterable <? extends CharSequence> lines, Charset cs, OpenOption ... options)
: Write text lines to a file
java.nio.file.StandardOpenOption
Enumeration class that defines how to work with files
--StandardOpenOption.APPEND
: Write at the end of the file, not at the beginning
try-with-resources
By using the try-with-resources
statement, you no longer have to write theclose ()
that was described in accessing the file database.
Preventing memory leaks due to forgetting to write, it has become possible to save a little time and effort.
try(BufferedReader reader = new BufferedReader(new FileReader("gyunyu.txt"))) {
String line = null;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You can use the try-with-resources
statement for any class that implements the java.lang.AutoCloseable
or java.io.Closeable
interface.
Gyunyu.java
public class Gyunyu implements AutoCloseable {
public void make() {
System.out.println("coffee");
}
@Override
public void close() throws Exception {
System.out.println("milk!!!");
}
}
try (Gyunyu gyunyu = new Gyunyu()) {
gyunyu.make();
} catch (Exception e) {
e.printStackTrace();
}
close ()
is being executed without calling it like gyunyu.close ()
.
Up to Java 6, I wrote as follows,
try {
} catch (IllegalArgumentException e) {
} catch (NullPointerException e) {
}
From Java7, it can be described collectively as follows.
try {
} catch (IllegalArgumentException | NullPointerException e) {
}
You can now use String in switch statements.
Up to "1.4", only ʻint can be used, and from "5" to ʻenum
can also be used for judgment.
From "7", it is now possible to judge by String.
I've always thought that it can be judged by String ...
String s = "coffee";
switch (s) {
case "ichigo":
System.out.println("ICHIGO GYUNYU !!!");
break;
case "coffee":
System.out.println("COFFEE GYUNYU !!!");
break;
}
Recommended Posts