Read and Write files in Android/java

Source: Internet
Author: User

Read Memory card files

Read byte streams from images, videos, and other media files,

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();}

Read text files and save them with 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;}


You must use byte [] to write images, videos, and other media files.

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();}       }


Write text files and write log information.

public void writeFile(String filePath,String f){FileWriter fw;try {fw = new FileWriter(filePath);fw.write(f);fw.close();} catch (IOException e) {e.printStackTrace();}       }



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.