ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/util/FileUtil.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.util; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.os.Environment; |
| TMBOY | 45:2aa9f933c8d2 | 19 | import android.text.TextUtils; |
| TMBOY | 45:2aa9f933c8d2 | 20 | |
| TMBOY | 45:2aa9f933c8d2 | 21 | import java.io.BufferedWriter; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import java.io.File; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import java.io.FileOutputStream; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import java.io.IOException; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import java.io.OutputStreamWriter; |
| TMBOY | 45:2aa9f933c8d2 | 26 | |
| TMBOY | 45:2aa9f933c8d2 | 27 | /** |
| TMBOY | 45:2aa9f933c8d2 | 28 | * FileUtil |
| TMBOY | 45:2aa9f933c8d2 | 29 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 30 | * Created by guxiuzhong@baidu.com on 2017/5/31. |
| TMBOY | 45:2aa9f933c8d2 | 31 | */ |
| TMBOY | 45:2aa9f933c8d2 | 32 | public class FileUtil { |
| TMBOY | 45:2aa9f933c8d2 | 33 | public static final String TEMP_POSTFIX = ".download"; |
| TMBOY | 45:2aa9f933c8d2 | 34 | private static final String LOG_FILE = "LogAll.txt"; |
| TMBOY | 45:2aa9f933c8d2 | 35 | private static final String APP_DIR = "/DCS"; |
| TMBOY | 45:2aa9f933c8d2 | 36 | private static final String SPEAK = APP_DIR + "/Speak"; |
| TMBOY | 45:2aa9f933c8d2 | 37 | private static final String ALERT = APP_DIR + "/Alert"; |
| TMBOY | 45:2aa9f933c8d2 | 38 | private static final String ALARM_FILE = "alarms.json"; |
| TMBOY | 45:2aa9f933c8d2 | 39 | |
| TMBOY | 45:2aa9f933c8d2 | 40 | private static String getSpeakDirPath() { |
| TMBOY | 45:2aa9f933c8d2 | 41 | String dirPath = ""; |
| TMBOY | 45:2aa9f933c8d2 | 42 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| TMBOY | 45:2aa9f933c8d2 | 43 | dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + SPEAK; |
| TMBOY | 45:2aa9f933c8d2 | 44 | File dir = new File(dirPath); |
| TMBOY | 45:2aa9f933c8d2 | 45 | if (!dir.exists()) { |
| TMBOY | 45:2aa9f933c8d2 | 46 | dir.mkdirs(); |
| TMBOY | 45:2aa9f933c8d2 | 47 | } |
| TMBOY | 45:2aa9f933c8d2 | 48 | } |
| TMBOY | 45:2aa9f933c8d2 | 49 | return dirPath; |
| TMBOY | 45:2aa9f933c8d2 | 50 | } |
| TMBOY | 45:2aa9f933c8d2 | 51 | |
| TMBOY | 45:2aa9f933c8d2 | 52 | private static String getAlertDirPath() { |
| TMBOY | 45:2aa9f933c8d2 | 53 | String dirPath = ""; |
| TMBOY | 45:2aa9f933c8d2 | 54 | if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { |
| TMBOY | 45:2aa9f933c8d2 | 55 | dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + ALERT; |
| TMBOY | 45:2aa9f933c8d2 | 56 | File dir = new File(dirPath); |
| TMBOY | 45:2aa9f933c8d2 | 57 | if (!dir.exists()) { |
| TMBOY | 45:2aa9f933c8d2 | 58 | dir.mkdirs(); |
| TMBOY | 45:2aa9f933c8d2 | 59 | } |
| TMBOY | 45:2aa9f933c8d2 | 60 | } |
| TMBOY | 45:2aa9f933c8d2 | 61 | return dirPath; |
| TMBOY | 45:2aa9f933c8d2 | 62 | } |
| TMBOY | 45:2aa9f933c8d2 | 63 | |
| TMBOY | 45:2aa9f933c8d2 | 64 | public static File getSpeakFile() { |
| TMBOY | 45:2aa9f933c8d2 | 65 | String dirPath = getSpeakDirPath(); |
| TMBOY | 45:2aa9f933c8d2 | 66 | if (TextUtils.isEmpty(dirPath)) { |
| TMBOY | 45:2aa9f933c8d2 | 67 | return null; |
| TMBOY | 45:2aa9f933c8d2 | 68 | } |
| TMBOY | 45:2aa9f933c8d2 | 69 | return new File(dirPath, |
| TMBOY | 45:2aa9f933c8d2 | 70 | "dcs_" + System.currentTimeMillis() + ".mp3" + TEMP_POSTFIX); |
| TMBOY | 45:2aa9f933c8d2 | 71 | } |
| TMBOY | 45:2aa9f933c8d2 | 72 | |
| TMBOY | 45:2aa9f933c8d2 | 73 | public static File getAlarmFile() { |
| TMBOY | 45:2aa9f933c8d2 | 74 | String dirPath = getAlertDirPath(); |
| TMBOY | 45:2aa9f933c8d2 | 75 | if (TextUtils.isEmpty(dirPath)) { |
| TMBOY | 45:2aa9f933c8d2 | 76 | return null; |
| TMBOY | 45:2aa9f933c8d2 | 77 | } |
| TMBOY | 45:2aa9f933c8d2 | 78 | return new File(dirPath, ALARM_FILE); |
| TMBOY | 45:2aa9f933c8d2 | 79 | } |
| TMBOY | 45:2aa9f933c8d2 | 80 | |
| TMBOY | 45:2aa9f933c8d2 | 81 | public static String getLogFilePath() { |
| TMBOY | 45:2aa9f933c8d2 | 82 | return Environment.getExternalStorageDirectory().getAbsolutePath() |
| TMBOY | 45:2aa9f933c8d2 | 83 | + APP_DIR + File.separator + LOG_FILE; |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | /** |
| TMBOY | 45:2aa9f933c8d2 | 87 | * 日志追加文件 |
| TMBOY | 45:2aa9f933c8d2 | 88 | * |
| TMBOY | 45:2aa9f933c8d2 | 89 | * @param content 追加的内容 |
| TMBOY | 45:2aa9f933c8d2 | 90 | */ |
| TMBOY | 45:2aa9f933c8d2 | 91 | public static void appendStrToFile(String content) { |
| TMBOY | 45:2aa9f933c8d2 | 92 | File file = new File(getLogFilePath()); |
| TMBOY | 45:2aa9f933c8d2 | 93 | if (!file.isFile()) { |
| TMBOY | 45:2aa9f933c8d2 | 94 | file.delete(); |
| TMBOY | 45:2aa9f933c8d2 | 95 | } |
| TMBOY | 45:2aa9f933c8d2 | 96 | if (!file.exists()) { |
| TMBOY | 45:2aa9f933c8d2 | 97 | try { |
| TMBOY | 45:2aa9f933c8d2 | 98 | file.createNewFile(); |
| TMBOY | 45:2aa9f933c8d2 | 99 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 100 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 101 | } |
| TMBOY | 45:2aa9f933c8d2 | 102 | } |
| TMBOY | 45:2aa9f933c8d2 | 103 | |
| TMBOY | 45:2aa9f933c8d2 | 104 | BufferedWriter out = null; |
| TMBOY | 45:2aa9f933c8d2 | 105 | try { |
| TMBOY | 45:2aa9f933c8d2 | 106 | out = new BufferedWriter(new OutputStreamWriter( |
| TMBOY | 45:2aa9f933c8d2 | 107 | new FileOutputStream(file, true))); |
| TMBOY | 45:2aa9f933c8d2 | 108 | out.write(content); |
| TMBOY | 45:2aa9f933c8d2 | 109 | out.flush(); |
| TMBOY | 45:2aa9f933c8d2 | 110 | } catch (Exception e) { |
| TMBOY | 45:2aa9f933c8d2 | 111 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 112 | } finally { |
| TMBOY | 45:2aa9f933c8d2 | 113 | try { |
| TMBOY | 45:2aa9f933c8d2 | 114 | out.close(); |
| TMBOY | 45:2aa9f933c8d2 | 115 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 116 | e.printStackTrace(); |
| TMBOY | 45:2aa9f933c8d2 | 117 | } |
| TMBOY | 45:2aa9f933c8d2 | 118 | } |
| TMBOY | 45:2aa9f933c8d2 | 119 | } |
| TMBOY | 45:2aa9f933c8d2 | 120 | } |
