In a dart console application, how can I tell if a file is binary (non-text)?
Asked
Active
Viewed 172 times
2 Answers
2
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
Community
- 1
- 1
Günter Zöchbauer
- 558,509
- 191
- 1,911
- 1,506
0
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 {}
Viktar K
- 978
- 1
- 12
- 19