
我写了个例子,你看能用吗
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;
}
}
Cursor mCursor = null;
String[] mAudiocols = new String[] {
MediaStoreAudioMediaTITLE,
MediaStoreAudioMediaDURATION,
MediaStoreAudioMediaARTIST,
MediaStoreAudioMedia_ID,
MediaStoreAudioMediaSIZE,
MediaStoreAudioMediaDATA,
MediaStoreAudioMediaDISPLAY_NAME,
MediaStoreAudioMediaBOOKMARK,
MediaStoreAudioMediaALBUM
};
mCursor =getContentResolver()query(MediaStoreAudioMediaEXTERNAL_CONTENT_URI,mAudiocols,"is_music=1",null,MediaStoreAudioMediaTITLE);
mTotalFiles = mCursorgetCount();
while(mCurrentPos < mTotalFiles){
mCurrentPos++;
mCursormoveToPosition(mCurrentPos);
mCursorgetColumnIndexOrThrow(MediaStoreAudioMediaTITLE));
}
具体没看过,大概流程:
client端的AudioSystem中调用getParameters,调用到IAudioFlinger,然后调用到libs下的AudioFlinger::getParameters,再下去就是要看每个系统了,我们是自己的硬件系统,所以无法给你更多的提示。
目测,你需要建立至少两个类 A -> dir B->dir1,每个类的字段嘛就是这个节点下的所有属性和子节点属性名 然后通过DOM、SAX或者XmlPullParser等来解析
用下面这种方式能实现查询实现查询sd卡某一个子目录下的文件详细信息 :
//selection: 指定查询条件
String selection = MediaStoreImagesMediaDATA + " like %";
//设定查询目录
String path="/mnt/sdcard/youpicpath";
//定义selectionArgs:
String[] selectionArgs = {path+"%"};
c = thisgetContentResolver()query(MediaStoreImagesMediaEXTERNAL_CONTENT_URI, null,
selection, selectionArgs, null);
其实原理就是改变了下查询语句,在查询条件中增加了MediaStoreImagesMediaDATA字段的限制条件,必须是和指定目录能匹配的才被查询,注意selection和selectionArgs参数是配合使用的。
先获取读取文件的权限,再遍历文件夹及子文件夹,直到结束就可以了。
private void getAllFiles(File root,ArrayList<File> results){
File files[] = rootlistFiles();
if(files != null){
for (File f : files){
if(fisDirectory()){
getAllFiles(f,results);
}
else{
String name = fgetName();
String extension
= namesubstring(namelastIndexOf(""));
if(extensionEqual("pdf")){
resultsadd(f);
}
}
}
}
}
您好,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问。
比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。
在raw中放入一个atxt文件,然后就可以在Activity中使用getResources()openRawResource(Rrawa);方法获取一个此文件的InputStream类,而后就可以很方便地进行读写atxt了。
以上就是关于android获取本地文件名字全部的内容,包括:android获取本地文件名字、如何在android中读取音频文件的所有信息、android xml文件解析 我要获取里面的所有信息。。求教等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)