Skip to content

Instantly share code, notes, and snippets.

@trplll
Created September 18, 2016 20:46
Show Gist options
  • Save trplll/b1d970578cb0471c7959733d18c84481 to your computer and use it in GitHub Desktop.
Save trplll/b1d970578cb0471c7959733d18c84481 to your computer and use it in GitHub Desktop.
IO Buffers try with resources. How to leverage try with resources to allow try statement to open and handle the buffer readers/writers
package com.jwhh;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.Paths;
/**
* Created by Jim on 1/9/2016.
*/
public class Helper {
static public Reader openReader(String fileName) throws IOException {
return Files.newBufferedReader(Paths.get(fileName));
}
static public Writer openWriter(String fileName) throws IOException {
return Files.newBufferedWriter(Paths.get(fileName));
}
}
package com.jwhh;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
public class Main {
public static void main(String[] args) {
//doTryCatchFinally();
// doTryWithResources();
// doTryWithResourcesMulti();
doCloseThing();
}
public static void doTryCatchFinally() {
char[] buff = new char[8];
int length;
Reader reader = null;
try {
reader = Helper.openReader("file1.txt");
while((length = reader.read(buff)) >= 0) {
System.out.println("\nlength: " + length);
for(int i=0; i < length; i++)
System.out.print(buff[i]);
}
} catch(IOException e) {
System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
} finally {
try {
if (reader != null)
reader.close();
} catch(IOException e2) {
System.out.println(e2.getClass().getSimpleName() + " - " + e2.getMessage());
}
}
}
public static void doTryWithResources() {
char[] buff = new char[8];
int length;
try (Reader reader = Helper.openReader("file1.txt")) {
while((length = reader.read(buff)) >= 0) {
System.out.println("\nlength: " + length);
for(int i=0; i < length; i++)
System.out.print(buff[i]);
}
} catch(IOException e) {
System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
}
}
public static void doTryWithResourcesMulti() {
char[] buff = new char[8];
int length;
try (Reader reader = Helper.openReader("file1.txt");
Writer writer = Helper.openWriter("file2.txt")) {
while((length = reader.read(buff)) >= 0) {
System.out.println("\nlength: " + length);
writer.write(buff, 0, length);
}
} catch(IOException e) {
System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
}
}
private static void doCloseThing() {
try(MyAutoCloseable ac = new MyAutoCloseable()) {
ac.saySomething();
} catch (IOException e) {
System.out.println(e.getClass().getSimpleName() + " - " + e.getMessage());
for(Throwable t:e.getSuppressed())
System.out.println("Suppressed: " + t.getMessage());
}
}
}
package com.jwhh;
import java.io.IOException;
/**
* Created by Jim on 1/9/2016.
*/
public class MyAutoCloseable implements AutoCloseable {
public void saySomething() throws IOException{
throw new IOException("Exception from saySomething");
// System.out.println("Something");
}
@Override
public void close() throws IOException {
throw new IOException("Exception from close");
// System.out.println("close");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment