java - File getting truncated while conversion -
when converting folder many files ansi(windows-1252) utf8, of files getting truncated while file small size blank. tried converting files reducing buffer size, didn't succeed. 1 has any idea on this???
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>(); (final file fileentry : inputfolder.listfiles()) { filelist.add(fileentry); } inputstream in; reader reader = null; writer writer = null; try { (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(); } } }
any pointer helpful.
thanks, ashish
Comments
Post a Comment