Java文件编码转换

时间:2013-03-12 05:01:59

标签: java file encoding

我需要将文件的编码从ANSI(windows-1252)更改为UTF8。我写下面的程序是通过java来完成的。此程序将字符转换为UTF8,但是当我在notepade ++中打开文件时,编码类型显示为ANSI为UTF8。当我在访问数据库中导入此文件时,这会给我带来错误。仅需要具有UTF8编码的文件。此外,还要求转换文件而不在任何编辑器中打开它。

public class ConvertFromAnsiToUtf8 {

private static final char BYTE_ORDER_MARK = '\uFEFF';
private static final String ANSI_CODE = "windows-1252";
private static final String UTF_CODE = "UTF8";
private static final Charset ANSI_CHARSET = Charset.forName(ANSI_CODE);

public static void main(String[] args) {

    List<File> fileList;
    File inputFolder = new File(args[0]);
    if (!inputFolder.isDirectory()) {
        return;
    }
    File parentDir = new File(inputFolder.getParent() + "\\"
                    + inputFolder.getName() + "_converted");

    if (parentDir.exists()) {
        return;
    }
    if (parentDir.mkdir()) {

    } else {
        return;
    }

    fileList = new ArrayList<File>();
    for (final File fileEntry : inputFolder.listFiles()) {
        fileList.add(fileEntry);
    }

    InputStream in;

    Reader reader = null;
    Writer writer = null;
    try {
        for (File file : fileList) {
            in = new FileInputStream(file.getAbsoluteFile());
            reader = new InputStreamReader(in, ANSI_CHARSET);

            OutputStream out = new FileOutputStream(
                            parentDir.getAbsoluteFile() + "\\"
                                            + file.getName());
            writer = new OutputStreamWriter(out, UTF_CODE);
            writer.write(BYTE_ORDER_MARK);
            char[] buffer = new char[10];
            int read;
            while ((read = reader.read(buffer)) != -1) {
                System.out.println(read);
                writer.write(buffer, 0, read);
            }
        }
        reader.close();
        writer.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

}

任何指针都会有所帮助。

谢谢, 阿希什

2 个答案:

答案 0 :(得分:5)

发布的代码正确地从windows-1252转码为UTF-8。

Notepad ++消息令人困惑,因为“ANSI as UTF-8”没有明显的含义;它似乎是Notepad ++中的open defect。我相信Notepad ++意味着 UTF-8没有BOM (参见编码菜单。)

作为Windows程序的Microsoft Access可能希望UTF-8文件以字节顺序标记(BOM)开头。

您可以通过在文件开头写入代码点U + FEFF将BOM注入文档:

import java.io.*;
import java.nio.charset.*;

public class Ansi1252ToUtf8 {
  private static final char BYTE_ORDER_MARK = '\uFEFF';

  public static void main(String[] args) throws IOException {
    Charset windows1252 = Charset.forName("windows-1252");
    try (InputStream in = new FileInputStream(args[0]);
        Reader reader = new InputStreamReader(in, windows1252);
        OutputStream out = new FileOutputStream(args[1]);
        Writer writer = new OutputStreamWriter(out, StandardCharsets.UTF_8)) {
      writer.write(BYTE_ORDER_MARK);
      char[] buffer = new char[1024];
      int read;
      while ((read = reader.read(buffer)) != -1) {
        writer.write(buffer, 0, read);
      }
    }
  }
}

答案 1 :(得分:1)

在运行Java 8的Windows 7(64位)上,我不得不关闭每个文件。否则,文件将被截断为4 kB的倍数。关闭最后一组文件是不够的,我不得不关闭每个文件以获得所需的结果。发布我的改编版本,添加错误消息:

import java.io.*;
import java.nio.charset.*;
import java.util.ArrayList;

public class ConvertFromAnsiToUtf8 {

    private static final char BYTE_ORDER_MARK = '\uFEFF';
    private static final String ANSI_CODE = "windows-1252";
    private static final String UTF_CODE = "UTF8";
    private static final Charset ANSI_CHARSET = Charset.forName(ANSI_CODE);
    private static final String PATH_SEP = "\\";
    private static final boolean WRITE_BOM = false;

    public static void main(String[] args) 
    {
        if (args.length != 2) {
            System.out.println("Please name a source and a target directory");
            return;
        }

        File inputFolder = new File(args[0]);
        if (!inputFolder.isDirectory()) {
            System.out.println("Input folder " + inputFolder + " does not exist");
            return;
        }
        File outputFolder = new File(args[1]);

        if (outputFolder.exists()) {
            System.out.println("Folder " + outputFolder + " exists - aborting");
            return;
        }
        if (outputFolder.mkdir()) {
            System.out.println("Placing converted files in " + outputFolder);
        } else {
            System.out.println("Output folder " + outputFolder + " exists - aborting");
            return;
        }

        ArrayList<File> fileList = new ArrayList<File>();
        for (final File fileEntry : inputFolder.listFiles()) {
            fileList.add(fileEntry);
        }

        InputStream in;
        Reader reader = null;
        Writer writer = null;
        int converted = 0;

        try {
            for (File file : fileList) {
                try {
                    in = new FileInputStream(file.getAbsoluteFile());
                    reader = new InputStreamReader(in, ANSI_CHARSET);

                    OutputStream out = new FileOutputStream(outputFolder.getAbsoluteFile() + PATH_SEP + file.getName());
                    writer = new OutputStreamWriter(out, UTF_CODE);

                    if (WRITE_BOM)
                        writer.write(BYTE_ORDER_MARK);
                    char[] buffer = new char[1024];
                    int read;
                    while ((read = reader.read(buffer)) != -1) {
                        writer.write(buffer, 0, read);
                    }
                    ++converted;
                } finally {
                    reader.close();
                    writer.close();
                }
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.out.println(converted + " files converted");
    }

}