Java tutorial
//package com.java2s; /** * Copyright 2014 University of Helsinki * * Licensed under the Apache License, Version 2.0 (the "License"); you may * not use this file except in compliance with the License. You may obtain * a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the * License for the specific language governing permissions and limitations * under the License. **/ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintWriter; import android.os.Environment; import android.util.Log; public class Main { public static void writeToExternalFile(String data, String logTag, String fileName) { if (!isExternalStorageWritable()) { Log.e(logTag, "failed to find external storage"); } else { File path = Environment.getExternalStorageDirectory(); File dir = new File(path.getAbsolutePath() + "/SDNController"); if (!dir.isDirectory()) { if (!dir.mkdirs()) { Log.e(logTag, "sdn directory can not be created"); return; } } File file = new File(dir, fileName); try { FileOutputStream f = new FileOutputStream(file, true); PrintWriter pw = new PrintWriter(f); pw.println(data); pw.flush(); pw.close(); f.close(); } catch (FileNotFoundException e) { Log.e(logTag, "can not find indicated file"); e.printStackTrace(); } catch (IOException e) { Log.e(logTag, "failed to write SDNController/result.txt"); e.printStackTrace(); } } } public static boolean isExternalStorageWritable() { String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { return true; } return false; } }