android获取本地文件名字

android获取本地文件名字,第1张

我写了个例子,你看能用吗

package comdragonredandroidutils;

import javaioBufferedWriter;

import javaioByteArrayOutputStream;

import javaioFile;

import javaioFileInputStream;

import javaioFileOutputStream;

import javaioIOException;

import javaioOutputStreamWriter;

import javaioRandomAccessFile;

import javatextSimpleDateFormat;

import javautilDate;

import androidcontentContext;

import androidcontentSharedPreferences;

import androidosEnvironment;

import androidutilLog;

public final class FileUtils {

public final static String PACKAGE_PATH = "comdragonredandroid";

// public final static String LOG_FILE_NAME = "smartprinttxt";

// public final static String LOG_FILE_PATH = STORE_DIRECTORY_PATH + FileseparatorChar + 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 = contextgetSharedPreferences(

PACKAGE_PATH, 0);

return settingsgetString(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 = contextgetSharedPreferences(

PACKAGE_PATH, 0);

SharedPreferencesEditor editor = settingsedit();

editorputString(keyName, keyValue);

editorcommit();

}

/

delete key from preference by key name

@param context

@param keyName

/

public final static void deletePreperence(Context context, String keyName) {

SharedPreferences settings = contextgetSharedPreferences(

PACKAGE_PATH, 0);

SharedPreferencesEditor editor = settingsedit();

editorremove(keyName);

editorcommit();

}

public final static String getContextFilePath(Context context, String fileName) {

return contextgetFilesDir()getAbsolutePath() + FileseparatorChar + fileName;

}

public final static boolean existContextFile(Context context, String fileName) {

String filePath = contextgetFilesDir()getAbsolutePath() + FileseparatorChar + fileName;

Logd("filePath", filePath);

File file = new File(filePath);

if (fileexists()) {

return true;

}

return false;

}

public final static void saveContextFile(Context context, String fileName, String content) throws Exception {

FileOutputStream outputStream = contextopenFileOutput(fileName, ContextMODE_PRIVATE);

outputStreamwrite(contentgetBytes());

outputStreamclose();

}

public final static void saveAppendContextFile(Context context, String fileName, String content) throws Exception {

FileOutputStream outputStream = contextopenFileOutput(fileName, ContextMODE_APPEND);

outputStreamwrite(contentgetBytes());

outputStreamclose();

}

public final static void deleteContextFile(Context context, String fileName) throws Exception {

contextdeleteFile(fileName);

}

public final static String readContextFile(Context context, String fileName) throws Exception {

FileInputStream inputStream = contextopenFileInput(fileName);

byte[] buffer = new byte[1024];

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

int len = -1;

while ((len = inputStreamread(buffer)) != -1) {

byteArrayOutputStreamwrite(buffer, 0, len);

}

byte[] data = byteArrayOutputStreamtoByteArray();

byteArrayOutputStreamclose();

inputStreamclose();

return new String(data);

}

/

delete file or folders

@param file

/

public final static void deleteFile(File file) {

if (fileexists()) {

if (fileisFile()) {

filedelete();

} else if (fileisDirectory()) {

File files[] = filelistFiles();

for (int i = 0; i < fileslength; i++) {

deleteFile(files[i]);

}

}

filedelete();

} else {

Logd("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(!dirisDirectory()) {

if (dirmkdirs()) {

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 = sdfformat(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);

// writerwrite(content);

// } catch (IOException e) {

// eprintStackTrace();

// } finally {

// try {

// writerclose();

// } catch (IOException e) {

// eprintStackTrace();

// }

// }

RandomAccessFile randomFile = null;

try {

randomFile = new RandomAccessFile(filePath, "rw");

long fileLength = randomFilelength();

randomFileseek(fileLength);

randomFilewrite(contentgetBytes());

} catch (IOException e) {

eprintStackTrace();

} finally {

try {

randomFileclose();

} catch (IOException e) {

eprintStackTrace();

}

}

// BufferedWriter out = null;

// try {

// out = new BufferedWriter(new OutputStreamWriter(

// new FileOutputStream(filePath, true), "UTF-8"));

// outwrite(content);

// } catch (Exception e) {

// eprintStackTrace();

// } finally {

// try {

// outclose();

// } catch (IOException e) {

// eprintStackTrace();

// }

// }

}

/

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);

// writerwrite(content);

// } catch (IOException e) {

// eprintStackTrace();

// } finally {

// try {

// writerclose();

// } catch (IOException e) {

// eprintStackTrace();

// }

// }

BufferedWriter out = null;

try {

out = new BufferedWriter(new OutputStreamWriter(

new FileOutputStream(filePath, false), "UTF-8"));

outwrite(content);

} catch (Exception e) {

eprintStackTrace();

} finally {

try {

outclose();

} catch (IOException e) {

eprintStackTrace();

}

}

}

/

check SD card whether or not exist

@param context

@return

/

public final static boolean checkSDCard(Context context) {

if (EnvironmentMEDIA_MOUNTEDequals(Environment

getExternalStorageState())) {

return true;

} else {

// ToastmakeText(context, "Please check your SD card! ",

// ToastLENGTH_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 (!fileexists()) {

return null;

}

raf = new RandomAccessFile(filePath, "r");

long len = raflength();

if (len == 0L) {

return "";

} else {

long pos = len - 1;

while (pos > 0) {

pos--;

rafseek(pos);

if (rafreadByte() == '\n') {

break;

}

}

if (pos == 0) {

rafseek(0);

}

byte[] bytes = new byte[(int) (len - pos)];

rafread(bytes);

return new String(bytes);

}

} catch (Exception e) {

eprintStackTrace();

} finally {

if (raf != null) {

try {

rafclose();

} catch (Exception e2) {

}

}

}

return null;

}

}

转:

JAR 文件可以用于

□ 用于发布和使用类库

□ 作为应用程序和扩展的构建单元

□ 作为组件、applet 或者插件程序的部署单位

□ 用于打包与组件相关联的辅助资源

asec文件:/asec 目录就是从SD卡分出来的存放安装在SD卡内应用游戏apk的目录,不要删。android_secure这个文件夹,打开ES文件浏览器隐藏文件查看功能,从手机上看到此文件夹为空,其实是特别处理的,其中含apk的内容是挂载到了/mnt/asec目录下,你可以在此找到现成的apk文件,这个目录是SD卡中的内容,不过不是挂载在/sdcard目录下,而是单独挂载了一个目录,就像是给SD卡分区一样,同样都是SD卡的不同分区,所以不要认为/mnt/sdcard目录是SD卡的真身,其实它不过是SD卡的一个分区,而/mnt/asec是另外的分区,它们都是SD卡的一部分,这样应该好解释些吧。

如果你直接用电脑查看你的SD卡,就会看到SD卡中文件夹android_secure里面不是非空的,其中都是asec文件,就是你安装在SD卡中的应用游戏,但在PC上你是无法看到apk文件的。所以说安装在SD卡中滴应用,绝对不会占内存。

以上就是关于android获取本地文件名字全部的内容,包括:android获取本地文件名字、android中 rc 后缀jar 后缀asec后缀的文件是什么、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/9579189.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-29
下一篇2023-04-29

发表评论

登录后才能评论

评论列表(0条)

    保存