转换时文件被截断

时间:2013-04-09 08:50:26

标签: java file io type-conversion

当我将包含许多文件的文件夹从ANSI(windows-1252)转换为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();
}
}
}

任何指针都会有所帮助。

谢谢, 阿希什

0 个答案:

没有答案