Android/java 读写文件
读内存卡的文件
读取图片,视频等媒体文件byte流,
public static byte[] readStream(String imagepath) throws Exception { FileInputStream fs = new FileInputStream(imagepath); ByteArrayOutputStream outStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len = 0; while (-1 != (len = fs.read(buffer))) { outStream.write(buffer, 0, len); } outStream.close(); fs.close(); return outStream.toByteArray(); }
读取文本文件,用Strng保存
public String readFile(String filename) { String content = null; File file = new File(filename); //for ex foo.txt try { FileReader reader = new FileReader(file); char[] chars = new char[(int) file.length()]; reader.read(chars); content = new String(chars); reader.close(); } catch (IOException e) { e.printStackTrace(); } return content; }
public void writeFile(String filePath,byte[] f){ try { FileOutputStream out = new FileOutputStream(new File(filePath)); out.write(f); out.close(); } catch (IOException e) { e.printStackTrace(); } }
public void writeFile(String filePath,String f){ FileWriter fw; try { fw = new FileWriter(filePath); fw.write(f); fw.close(); } catch (IOException e) { e.printStackTrace(); } }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。