package com.dragonred.android.utils
import java.io.BufferedWriter
import java.io.ByteArrayOutputStream
import java.io.File
import java.io.FileInputStream
import java.io.FileOutputStream
import java.io.IOException
import java.io.OutputStreamWriter
import java.io.RandomAccessFile
import java.text.SimpleDateFormat
import java.util.Date
import android.content.Context
import android.content.SharedPreferences
import android.os.Environment
import android.util.Log
public final class FileUtils {
public final static String PACKAGE_PATH = "com.dragonred.android"
// public final static String LOG_FILE_NAME = "smartprint.txt"
// public final static String LOG_FILE_PATH = STORE_DIRECTORY_PATH + File.separatorChar + LOG_FILE_NAME
/**
* read key value from preference by key name
*
* @param context
* @param keyName
* @return
*/
public final static String readPreperence(Context context, String keyName) {
SharedPreferences settings = context.getSharedPreferences(
PACKAGE_PATH, 0)
return settings.getString(keyName, "")
}
/**
* write key name and key value into preference
*
* @param context
* @param keyName
* @param keyValue
*/
public final static void writePreperence(Context context, String keyName,
String keyValue) {
SharedPreferences settings = context.getSharedPreferences(
PACKAGE_PATH, 0)
SharedPreferences.Editor editor = settings.edit()
editor.putString(keyName, keyValue)
editor.commit()
}
/**
* delete key from preference by key name
*
* @param context
* @param keyName
*/
public final static void deletePreperence(Context context, String keyName) {
SharedPreferences settings = context.getSharedPreferences(
PACKAGE_PATH, 0)
SharedPreferences.Editor editor = settings.edit()
editor.remove(keyName)
editor.commit()
}
public final static String getContextFilePath(Context context, String fileName) {
return context.getFilesDir().getAbsolutePath() + File.separatorChar + fileName
}
public final static boolean existContextFile(Context context, String fileName) {
String filePath = context.getFilesDir().getAbsolutePath() + File.separatorChar + fileName
Log.d("filePath", filePath)
File file = new File(filePath)
if (file.exists()) {
return true
}
return false
}
public final static void saveContextFile(Context context, String fileName, String content) throws Exception {
FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_PRIVATE)
outputStream.write(content.getBytes())
outputStream.close()
}
public final static void saveAppendContextFile(Context context, String fileName, String content) throws Exception {
FileOutputStream outputStream = context.openFileOutput(fileName, Context.MODE_APPEND)
outputStream.write(content.getBytes())
outputStream.close()
}
public final static void deleteContextFile(Context context, String fileName) throws Exception {
context.deleteFile(fileName)
}
public final static String readContextFile(Context context, String fileName) throws Exception {
FileInputStream inputStream = context.openFileInput(fileName)
byte[] buffer = new byte[1024]
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream()
int len = -1
while ((len = inputStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len)
}
byte[] data = byteArrayOutputStream.toByteArray()
byteArrayOutputStream.close()
inputStream.close()
return new String(data)
}
/**
* delete file or folders
* @param file
*/
public final static void deleteFile(File file) {
if (file.exists()) {
if (file.isFile()) {
file.delete()
} else if (file.isDirectory()) {
File files[] = file.listFiles()
for (int i = 0i <files.lengthi++) {
deleteFile(files[i])
}
}
file.delete()
} else {
Log.d("deleteFile", "The file or directory does not exist!")
}
}
/**
* make directory on SD card
* @param dirPath
* @return
*/
public final static boolean makeDir(String dirPath) {
File dir = new File(dirPath)
if(!dir.isDirectory()) {
if (dir.mkdirs()) {
return true
}
} else {
return true
}
return false
}
/**
* write log file
* @param filePath
* @param tag
* @param content
*/
public final static void writeLog(String filePath, String tag, String content) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
String logDateTime = sdf.format(new Date())
writeFileByAppend(filePath, logDateTime + "---[" + tag + "]---" + content + "\n")
}
/**
* write file by append mode
* @param filePath
* @param content
*/
public final static void writeFileByAppend(String filePath, String content) {
// FileWriter writer = null
//try {
//writer = new FileWriter(filePath, true)
//writer.write(content)
// } catch (IOException e) {
// e.printStackTrace()
// } finally {
// try {
// writer.close()
// } catch (IOException e) {
// e.printStackTrace()
// }
// }
RandomAccessFile randomFile = null
try {
randomFile = new RandomAccessFile(filePath, "rw")
long fileLength = randomFile.length()
randomFile.seek(fileLength)
randomFile.write(content.getBytes())
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
randomFile.close()
} catch (IOException e) {
e.printStackTrace()
}
}
// BufferedWriter out = null
// try {
// out = new BufferedWriter(new OutputStreamWriter(
// new FileOutputStream(filePath, true), "UTF-8"))
// out.write(content)
// } catch (Exception e) {
// e.printStackTrace()
// } finally {
// try {
// out.close()
// } catch (IOException e) {
// e.printStackTrace()
// }
// }
}
/**
* write file by overwrite mode
* @param filePath
* @param content
*/
public final static void writeFile(String filePath, String content) {
// FileWriter writer = null
//try {
//writer = new FileWriter(filePath, true)
//writer.write(content)
// } catch (IOException e) {
// e.printStackTrace()
// } finally {
// try {
// writer.close()
// } catch (IOException e) {
// e.printStackTrace()
// }
// }
BufferedWriter out = null
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filePath, false), "UTF-8"))
out.write(content)
} catch (Exception e) {
e.printStackTrace()
} finally {
try {
out.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
/**
* check SD card whether or not exist
* @param context
* @return
*/
public final static boolean checkSDCard(Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
return true
} else {
// Toast.makeText(context, "Please check your SD card! ",
// Toast.LENGTH_SHORT).show()
return false
}
}
/**
* read last line from file
* @param filePath
* @return
*/
public final static String readLastLinefromFile(String filePath) {
RandomAccessFile raf = null
try {
File file = new File(filePath)
if (!file.exists()) {
return null
}
raf = new RandomAccessFile(filePath, "r")
long len = raf.length()
if (len == 0L) {
return ""
} else {
long pos = len - 1
while (pos >0) {
pos--
raf.seek(pos)
if (raf.readByte() == '\n') {
break
}
}
if (pos == 0) {
raf.seek(0)
}
byte[] bytes = new byte[(int) (len - pos)]
raf.read(bytes)
return new String(bytes)
}
} catch (Exception e) {
e.printStackTrace()
} finally {
if (raf != null) {
try {
raf.close()
} catch (Exception e2) {
}
}
}
return null
}
}
亲爱的,我是搞Android开发的,我想告诉你的是,这两个名字,其实并不是在一个地方命名的。1、也就是说,你下载的那个OK_5462.apk我可以把这个文件重命名。这个.apk的后缀其实是一个压缩文件而已,我可以任意命名,
2、然后里面的应用的名字,我实在Strings的app_name里面命名的,这个是编程的时候做的,你可以理解为这个是内部命名的,第一条是外部命名的。
所以,懂了吗?
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)