public static string readusenio (File file) { FileInputStream fin; string string = null; try { fin = New fileinputstream (file); filechannel channel = null; Channel = fin.getchannel (); // file content size int size = (int) channel.size (); // get channel filechannel fc = fin.getchannel (); // Create buffer ByteBuffer buffer = Bytebuffer.allocate (1024 * 1024 * 1); // read data to buffer Fc.read (buffer); // buffer bf = buffer.flip (); // System.out.println ("LIMT:" + bf.limit ()); byte[] bt = buffer.array () ; string&Nbsp;= new string (Bt, 0, size, "UTF-8"); // system.out.println (new string (bt, 0, size)); // fileutil.appendstring ("F:/html/22.html", New string (bt, 0, // size)); buffer.clear (); buffer = null; fin.close (); } catch ( Filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace (); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace (); } return string; }
Read a file using Java NiO