How to read a struct from a file in Rust?

Here you go: use std::io::Read; use std::mem; use std::slice; #[repr(C, packed)] #[derive(Debug, Copy, Clone)] struct Configuration { item1: u8, item2: u16, item3: i32, item4: [char; 8], } const CONFIG_DATA: &[u8] = &[ 0xfd, // u8 0xb4, 0x50, // u16 0x45, 0xcd, 0x3c, 0x15, // i32 0x71, 0x3c, 0x87, 0xff, // char 0xe8, 0x5d, 0x20, 0xe7, … Read more

Read and Write Text in ANSI format

To read a text file with a specific encoding you can use a FileInputStream in conjunction with a InputStreamReader. The right Java encoding for Windows ANSI is Cp1252. reader = new BufferedReader(new InputStreamReader(new FileInputStream(csvFile), “Cp1252”)); To write a text file with a specific character encoding you can use a FileOutputStream together with a OutputStreamWriter. writer … Read more

Test if a file is an image file

This works pretty well for me. Hope I could help import javax.activation.MimetypesFileTypeMap; import java.io.File; class Untitled { public static void main(String[] args) { String filepath = “/the/file/path/image.jpg”; File f = new File(filepath); String mimetype= new MimetypesFileTypeMap().getContentType(f); String type = mimetype.split(“https://stackoverflow.com/”)[0]; if(type.equals(“image”)) System.out.println(“It’s an image”); else System.out.println(“It’s NOT an image”); } }