Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

To skip the BOM (Byte Order Mark) character in java utils.Properties, you can use the following code:

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

public class Main {
  public static void main(String[] args) throws IOException {
    Properties props = new Properties();

    // Open the input stream with UTF-8 encoding to read the file
    FileInputStream input = new FileInputStream("file.properties");
    InputStreamReader reader = new InputStreamReader(input, StandardCharsets.UTF_8);
    BufferedReader bufferedReader = new BufferedReader(reader);

    // Skip the BOM character if present
    bufferedReader.mark(1);
    if (bufferedReader.read() != 0xfeff) {
        bufferedReader.reset();
    }

    // Load the properties file
    props.load(bufferedReader);

    // Close the streams
    bufferedReader.close();
    reader.close();
    input.close();

    // Print the properties
    System.out.println(props);
  }
}

In this code, we first open the input stream with UTF-8 encoding to read the file. We then create a BufferedReader to skip the BOM character if present. We use the mark() method to mark the current position in the stream and then read the first byte to check if it is the BOM. If it is not the BOM, we reset the stream to the marked position. Finally, we load the properties file using the load() method of the Properties class and print the properties.