Skip to content

Instantly share code, notes, and snippets.

@hertzsprung
Created July 28, 2011 21:48
Show Gist options
  • Save hertzsprung/1112663 to your computer and use it in GitHub Desktop.
Save hertzsprung/1112663 to your computer and use it in GitHub Desktop.
Assorted Java 7 features
package foo;
import static java.util.EnumSet.of;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
import java.nio.charset.Charset;
import java.nio.file.FileStore;
import java.nio.file.FileVisitOption;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.StandardOpenOption;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.Collection;
public class Java7Lols {
public static void main(String[] args) throws IOException {
Collection<Java7Lols> foo = new ArrayList<>();
int c = 1_234_567;
Charset utf8 = Charset.forName("UTF-8");
try (ReadableByteChannel ch = FileChannel.open(Paths.get("src/foo/Java7Lols.java"), StandardOpenOption.READ)) {
ByteBuffer buf = ByteBuffer.allocate(1024);
while (ch.read(buf) != -1) {
buf.flip();
System.out.print(utf8.decode(buf));
buf.clear();
}
} catch (InvalidPathException | ClosedChannelException e) {
e.printStackTrace();
}
System.out.println("\n----");
FileStore fileStore = new File("src/foo/Java7Lols.java")
.toPath()
.getFileSystem()
.getFileStores()
.iterator()
.next();
System.out.println(fileStore + " has " + fileStore.getUnallocatedSpace() + " free bytes");
Files.walkFileTree(
new File("src/").toPath(),
of(FileVisitOption.FOLLOW_LINKS),
Integer.MAX_VALUE,
new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs) throws IOException {
System.out.println("found " + file.toAbsolutePath() + " with inode " + attrs.fileKey());
return FileVisitResult.CONTINUE;
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment