write Properties to File - Android java.util

Android examples for java.util:Properties

Description

write Properties to File

Demo Code


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import android.text.TextUtils;

public class Main {
  public static final String filePath = "cookie.prop";
  public static final String KEY1 = "mobileNo";
  public static final String KEY2 = "password";
  public static final String comment = "Account";

  public static void writeProperties(String value1, String value2) {
    if (TextUtils.isEmpty(value1) || TextUtils.isEmpty(value2)) {
      return;//ww  w  . j  a  v  a2 s  .c  o m
    }
    FileInputStream fis = null;
    FileOutputStream fos = null;
    File f = new File(filePath);
    try {
      if (!f.exists() || !f.isFile()) {
        f.createNewFile();
      }
      fis = new FileInputStream(f);
      Properties p = new Properties();
      p.load(fis);
      p.setProperty(KEY1, value1);
      p.setProperty(KEY2, value2);
      fos = new FileOutputStream(f);
      p.store(fos, comment);
    } catch (Exception e) {
    } finally {
      if (fis != null) {
        try {
          fis.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
      if (fos != null) {
        try {
          fos.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }
}

Related Tutorials