Skip to content

Instantly share code, notes, and snippets.

@nherbaut
Created March 13, 2015 09:33
Show Gist options
  • Select an option

  • Save nherbaut/8e9712a2038bf199d560 to your computer and use it in GitHub Desktop.

Select an option

Save nherbaut/8e9712a2038bf199d560 to your computer and use it in GitHub Desktop.
file type test (without file name)
package testtype;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) throws IOException {
Stream<Path> files = Files.list(new File("/home/nherbaut/Downloads")
.toPath());
files.forEach(new Consumer<Path>() {
@Override
public void accept(Path t) {
try {
Path tempFile = Files.createTempFile(null, null);
FileOutputStream fos = new FileOutputStream(tempFile.toFile());
Files.copy(t, fos);
fos.close();
System.out.println(t.toAbsolutePath() + "\tDetected Type:"
+ Files.probeContentType(tempFile));
Files.delete(tempFile);
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}
/*
/home/nherbaut/Downloads/mp1_assignment.zip Detected Type:application/zip
/home/nherbaut/Downloads/Game of Thrones - 4x05 - First of His Name.HDTV.KILLERS.fr.srt Detected Type:text/plain
/home/nherbaut/Downloads/06_03.srt Detected Type:text/plain
/home/nherbaut/Downloads/10_03.srt Detected Type:text/plain
/home/nherbaut/Downloads/Game of Thrones - 4x04 - Oathkeeper.HDTV.REPACK.KILLERS.fr.srt Detected Type:text/plain
/home/nherbaut/Downloads/Game of Thrones - 4x01 - Two Swords.720p HDTV.fr.srt Detected Type:application/x-subrip
/home/nherbaut/Downloads/05_03.srt Detected Type:application/x-subrip
/home/nherbaut/Downloads/Parks.and.Recreation.S05E08.HDTV.x264-LOL.mp4 Detected Type:video/mp4
/home/nherbaut/Downloads/Game.of.Thrones.S04E10.HDTV.x264-KILLERS.mp4 Detected Type:video/mp4
/home/nherbaut/Downloads/big_buck_bunny_480p_surround-fix.avi Detected Type:video/x-msvideo
/home/nherbaut/Downloads/Parks.and.Recreation.S05E03.HDTV.x264-LOL.mp4 Detected Type:video/mp4
/home/nherbaut/Downloads/Game.of.Thrones.S03E01.HDTV.x264-2HD.mp4 Detected Type:video/mp4
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment