2

In a dart console application, how can I tell if a file is binary (non-text)?

Kasper
  • 12,594
  • 12
  • 41
  • 63

2 Answers2

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
  • 623,577
  • 216
  • 2,003
  • 1,567
1

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
  • 1,409
  • 2
  • 16
  • 22