Package com.grt192.utils

Source Code of com.grt192.utils.GRTFileIO

package com.grt192.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import javax.microedition.io.Connector;

import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.json.me.JSONTokener;

import com.sun.squawk.io.BufferedReader;
import com.sun.squawk.microedition.io.FileConnection;

public class GRTFileIO {

  public static String getFileContents(String filename) {
    String url = "file:///" + filename;
    String contents = "";
    try {
      FileConnection c = (FileConnection) Connector.open(url);
      BufferedReader buf = new BufferedReader(new InputStreamReader(c
          .openInputStream()));
      String line = "";
      while ((line = buf.readLine()) != null) {
        contents += line + "\n";
      }
      c.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return contents;
  }

  public static JSONObject fromJSONFile(String filename) {
    try {
      return new JSONObject(new JSONTokener(getFileContents(filename)));
    } catch (JSONException e) {
      e.printStackTrace();
      return null;
    }
  }

  public static void writeToFile(String filename, String contents) {
    String url = "file:///" + filename;
    try {
      FileConnection c = (FileConnection) Connector.open(url);
      OutputStreamWriter writer = new OutputStreamWriter(c
          .openOutputStream());
      writer.write(contents);
      c.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
TOP

Related Classes of com.grt192.utils.GRTFileIO

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.