|
Post by Admin on Aug 2, 2017 10:53:41 GMT 7
[java] hàm viết file dùng để ghi dữ liệu vào file nhập đường dẫn đến file cần ghi String, dữ liệu ghi dạng bytearray hàm thực thi không trả lại kết quả.
public void write(String fn, byte[] dt) { try { File f = new File(fn); FileOutputStream fos = new FileOutputStream(f); fos.write(dt); fos.close(); System.out.println("OK\t" + fn); } catch (Exception e) { System.out.println("ERR\t" + fn); } } hàm đọc file, nhận đường dẫn đến file cần đọc, trả về dữ liệu dạng bytearray
public byte[] read(String fn) { try { File f = new File(fn); FileInputStream fis = new FileInputStream(f); ByteArrayOutputStream bos=new ByteArrayOutputStream(); DataOutputStream dos=new DataOutputStream(bos); byte[] bf=new byte[12020]; int c; while((c=fis.read(bf))>0){ dos.write(bf,0,c); } fis.close(); bf=bos.toByteArray(); dos.close(); bos.close(); return bf; } catch (Exception e) { System.out.println("ERR\t" + fn); } return null; }
|
|