Reputation: 13582
In a dart console application, how can I tell if a file is binary (non-text)?
Upvotes: 3
Views: 282
Reputation: 1528
I use this code to define a binary or text file:
bool isBinary(String path) {
final file = File(path);
RandomAccessFile raf = file.openSync(mode: FileMode.read);
Uint8List data = raf.readSync(124);
for (final b in data) {
if (b >= 0x00 && b <= 0x08) {
raf.close();
return true;
}
}
raf.close();
return false;
}
try {
isBinary('/filepath.ext');
} on FileSystemException {}
Upvotes: 1
Reputation: 657278
Read the file content and check if you find non-displayable characters. An example would be \u0000
or consecutive \u0000
, which often occurs in binary files but not in text files.
See also How can I determine if a file is binary or text in c#?, https://stackoverflow.com/a/277568/217408
Upvotes: 2