1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.mobileer.oboetester; 18 19 import android.content.Context; 20 import android.util.Log; 21 22 import java.io.File; 23 import java.io.FileOutputStream; 24 import java.io.IOException; 25 import java.io.OutputStreamWriter; 26 import java.io.Writer; 27 28 public class ExternalFileWriter { 29 private static final String TAG = "OboeTester"; 30 private Context mContext; 31 ExternalFileWriter(Context context)32 public ExternalFileWriter(Context context) { 33 mContext = context; 34 } 35 writeStringToExternalFile(String result, String fileName)36 public File writeStringToExternalFile(String result, String fileName) throws IOException { 37 File dir = mContext.getExternalFilesDir(null); 38 File resultFile = new File(dir, fileName); 39 Log.d(TAG, "EXTFILE = " + resultFile.getAbsolutePath()); 40 Writer writer = null; 41 try { 42 writer = new OutputStreamWriter(new FileOutputStream(resultFile)); 43 writer.write(result); 44 } finally { 45 if (writer != null) { 46 try { 47 writer.close(); 48 } catch (IOException e) { 49 e.printStackTrace(); 50 } 51 } 52 } 53 return resultFile; 54 } 55 } 56