/**
* OpenAtlasForAndroid Project
* The MIT License (MIT) Copyright (OpenAtlasForAndroid) 2015 Bunny Blue,achellies
* <p>
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
* and associated documentation files (the "Software"), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify,
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
* <p>
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
* <p>
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
* @author BunnyBlue
**/
package com.openatlas.util;
import android.text.TextUtils;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class FileUtils {
public static boolean saveString(String str, File file, boolean append) {
if (TextUtils.isEmpty(str) || file == null) {
return false;
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
return false;
}
}
OutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file, append);
fileOutputStream.write((str + "\r\n").getBytes());
fileOutputStream.flush();
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
return true;
} catch (FileNotFoundException e4) {
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
}
return false;
}
public static boolean saveStrings(String[] dataArrs, File file) {
int i = 0;
if (dataArrs == null || dataArrs.length == 0 || file == null) {
return false;
}
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
return false;
}
}
OutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
int length = dataArrs.length;
while (i < length) {
fileOutputStream.write((dataArrs[i] + "\r\n").getBytes());
i++;
}
fileOutputStream.flush();
return true;
} catch (FileNotFoundException e4) {
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
return false;
}
public static String[] getStrings(File file) {
List<String> arrayList = new ArrayList<String>();
if (file == null || !file.exists()) {
return arrayList.toArray(new String[arrayList.size()]);
}
InputStream fileInputStream = null;
BufferedReader bufferedReader = null;
try {
fileInputStream = new FileInputStream(file);
bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
while (true) {
String readLine = bufferedReader.readLine();
if (readLine == null) {
break;
}
arrayList.add(readLine);
}
} catch (FileNotFoundException e4) {
// return arrayList.toArray(new String[arrayList.size()]);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
if (bufferedReader != null) {
try {
bufferedReader.close();
} catch (IOException e3) {
e3.printStackTrace();
}
}
}
return arrayList.toArray(new String[arrayList.size()]);
}
public static void deleteFile(String path) throws IOException {
File file = new File(path);
if (!file.exists()) {
return;
}
if (file.isDirectory()) {
File[] listFiles = file.listFiles();
if (listFiles.length == 0) {
file.delete();
return;
}
for (File absolutePath : listFiles) {
deleteFile(absolutePath.getAbsolutePath());
}
return;
}
file.delete();
}
public static boolean validationFile(String filePath) {
try {
FileInputStream fileInputStream = new FileInputStream(filePath);
fileInputStream.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
}