The below code is quoted from : http://examples.javacodegeeks.com/core-java/io/fileoutputstream/java-io-fileoutputstream-example/
Although the OutputStream is an abstract method, at the below code, OutputStream object is used for writing into the file.
Files.newOutputStream(filepath)) returns OutputStream. Then, the type of out is OutputStream, and out references OutputStream.
How can this be possible while OutputStream is an abstract class?
package com.javacodegeeks.core.io.outputstream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileOutputStreamExample {
private static final String OUTPUT_FILE = "C:\\Users\\nikos\\Desktop\\TestFiles\\testFile.txt";
public static void main(String[] args) {
String content = "Hello Java Code Geeks";
byte[] bytes = content.getBytes();
Path filepath = Paths.get(OUTPUT_FILE);
try ( OutputStream out = Files.newOutputStream(filepath)) {
out.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}
}