Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.io.BufferedReader;
import java.io.File;

import java.io.FileReader;

public class Main {
    public static String getEncoding(String f) {
        String encoding = "UTF-8";
        File file = new File(f);
        try {
            BufferedReader reader = new BufferedReader(new FileReader(file));
            String xml = reader.readLine();
            reader.close();
            xml = xml.toLowerCase();
            int pos = xml.indexOf("encoding");
            if (pos < 0) {
                return encoding;
            }
            xml = xml.substring(pos + "encoding".length());
            pos = xml.indexOf("\"");
            xml = xml.substring(pos + 1);
            pos = xml.indexOf("\"");
            xml = xml.substring(0, pos).trim();
            return xml.toUpperCase();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return encoding;
    }
}