androidtxtsd
A. android怎麼刪除sdCard中的.txt文件
還要代碼是吧!我也是找的改了一點!刪的是自己建的test目錄String sdPath = Environment.getExternalStorageDirectory() + ""; File file3 = new File(sdPath + "/test");deleteAllFile(file3.toString());deleteAllFile具體如下:private static void deleteAllFile(String path) { File file = new File(path); if (!file.exists()) { return; } if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { String[] tempList = file.list(); File temp = null; for (int i = 0; i < tempList.length; i++) { if (path.endsWith(File.separator)) { temp = new File(path + tempList[i]); } else { temp = new File(path + File.separator + tempList[i]); } if (temp.isFile()&&temp.toString().endsWith(".txt")){// temp.delete(); } if (temp.isDirectory()){ // 先刪除文件夾裡面的文件 deleteAllFile(path + "/" + tempList[i]); // 再刪除空文件夾 //deleteDirectory(path + "/" + tempList[i]); } } } } 查看原帖>>
B. android編程:怎樣讀取txt文件
android 能讀取的文件都是系統裡面的(這是系統不是開發壞境系統,而是你程序運行的環境系統,也就是avd或者真實的手機設備的sd卡),這就需要你把文件導入你的環境中,mnt目錄底下,然後按到讀取sd卡的路徑讀取即可。
C. android開發中,做一個了注冊的界面後,如何將注冊的信息以TXT文件形式保存在SD卡中
你需要在xml文件裡面使能外部的sd卡:
<!-- 在SDCard中創建於刪除文件的許可權 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard中寫入數據的許可權 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
然後android的java code如下:
public void saveToSDCard(String filename,String content) throws Exception{
File file=new File(Environment.getExternalStorageDirectory(), filename);
OutputStream out=new FileOutputStream(file);
out.write(content.getBytes());
out.close();
}
D. android讀取TXT文件數據
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String fileName = "/sdcard/y.txt";//文件路徑
// 也可以用String fileName = "mnt/sdcard/Y.txt";
String res = "";
try {
FileInputStream fin = new FileInputStream(fileName);
// FileInputStream fin = openFileInput(fileName);
// 用這個就不行了,必須用FileInputStream
int length = fin.available();
byte[] buffer = new byte[length];
fin.read(buffer);
res = EncodingUtils.getString(buffer, "UTF-8");////依Y.txt的編碼類型選擇合適的編碼,如果不調整會亂碼
fin.close();//關閉資源
System.out.println("res--->"+res);
int a=Integer.parseInt(res.substring(3, 5));
int b=Integer.parseInt(res.substring(8, 10));
System.out.println(a+"res--->"+b);//獲取的a.b
} catch (Exception e) {
e.printStackTrace();
}
}
E. android 怎樣在絕對路徑下打開txt文件比如讀取D:\F.TXT文件
android中只能訪問android手機中的文件和SD卡中的文件,如果說你是想寫入本地D:\F.TXT到你的應用中的話,只需要簡單的操作一下io流就可以了,你的應用就可以從流中讀取數據,要想從pc上復制文件到(模擬器)真機上可以用android:fileter視圖里的工具,也可以用dos命令實現.比如將你d:\f.txt文件復制到機子上sd卡根目錄下 adb -s emulator-5554 pull d:\f.txt/sdcard e.txt,你能把具體問題說一下么?問題太含糊了!
F. android開發如何把字元串保存為txt格式,並存至SD卡還有讀取的問題
android中有讀取xml和讀取json,看你自己心情選擇一種格式保存你的字元串,寫一起放到txt文件中,在放到android項目的 res文件夾下新建一個raw文件夾(調用時候直接R.raw.文件名),讀取的方式對應。網上有很多源碼。
G. android手機里一堆txt文件可以刪嗎
可以。這里使用的手機型號為華為H60-L02,其中的具體步驟如下:
1、首先點擊搜索本地文件,如圖所示。
H. Android寫入txt文件
分以下幾個步驟:
首先對manifest注冊SD卡讀寫許可權
AndroidManifest.xml
<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="
package="com.tes.textsd"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16"/>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name="com.tes.textsd.FileOperateActivity"
android:label="@string/app_name">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>
<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>創建一個對SD卡中文件讀寫的類
FileHelper.java
/**
*@Title:FileHelper.java
*@Packagecom.tes.textsd
*@Description:TODO(用一句話描述該文件做什麼)
*@authorAlex.Z
*@date2013-2-26下午5:45:40
*@versionV1.0
*/
packagecom.tes.textsd;
importjava.io.DataOutputStream;
importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.FileWriter;
importjava.io.FileInputStream;
importjava.io.FileNotFoundException;
importjava.io.IOException;
importandroid.content.Context;
importandroid.os.Environment;
publicclassFileHelper{
privateContextcontext;
/**SD卡是否存在**/
privatebooleanhasSD=false;
/**SD卡的路徑**/
privateStringSDPATH;
/**當前程序包的路徑**/
privateStringFILESPATH;
publicFileHelper(Contextcontext){
this.context=context;
hasSD=Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED);
SDPATH=Environment.getExternalStorageDirectory().getPath();
FILESPATH=this.context.getFilesDir().getPath();
}
/**
*在SD卡上創建文件
*
*@throwsIOException
*/
publicFilecreateSDFile(StringfileName)throwsIOException{
Filefile=newFile(SDPATH+"//"+fileName);
if(!file.exists()){
file.createNewFile();
}
returnfile;
}
/**
*刪除SD卡上的文件
*
*@paramfileName
*/
publicbooleandeleteSDFile(StringfileName){
Filefile=newFile(SDPATH+"//"+fileName);
if(file==null||!file.exists()||file.isDirectory())
returnfalse;
returnfile.delete();
}
/**
*寫入內容到SD卡中的txt文本中
*str為內容
*/
publicvoidwriteSDFile(Stringstr,StringfileName)
{
try{
FileWriterfw=newFileWriter(SDPATH+"//"+fileName);
Filef=newFile(SDPATH+"//"+fileName);
fw.write(str);
FileOutputStreamos=newFileOutputStream(f);
DataOutputStreamout=newDataOutputStream(os);
out.writeShort(2);
out.writeUTF("");
System.out.println(out);
fw.flush();
fw.close();
System.out.println(fw);
}catch(Exceptione){
}
}
/**
*讀取SD卡中文本文件
*
*@paramfileName
*@return
*/
publicStringreadSDFile(StringfileName){
StringBuffersb=newStringBuffer();
Filefile=newFile(SDPATH+"//"+fileName);
try{
FileInputStreamfis=newFileInputStream(file);
intc;
while((c=fis.read())!=-1){
sb.append((char)c);
}
fis.close();
}catch(FileNotFoundExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returnsb.toString();
}
publicStringgetFILESPATH(){
returnFILESPATH;
}
publicStringgetSDPATH(){
returnSDPATH;
}
publicbooleanhasSD(){
returnhasSD;
}
}寫一個用於檢測讀寫功能的的布局
main.xml
<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="@+id/hasSDTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"/>
<TextView
android:id="@+id/SDPathTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"/>
<TextView
android:id="@+id/FILESpathTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="hello"/>
<TextView
android:id="@+id/createFileTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="false"/>
<TextView
android:id="@+id/readFileTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="false"/>
<TextView
android:id="@+id/deleteFileTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="false"/>
</LinearLayout>就是UI的類了
FileOperateActivity.class
/**
*@Title:FileOperateActivity.java
*@Packagecom.tes.textsd
*@Description:TODO(用一句話描述該文件做什麼)
*@authorAlex.Z
*@date2013-2-26下午5:47:28
*@versionV1.0
*/
packagecom.tes.textsd;
importjava.io.IOException;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.widget.TextView;
{
privateTextViewhasSDTextView;
privateTextViewSDPathTextView;
;
;
;
;
privateFileHelperhelper;
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hasSDTextView=(TextView)findViewById(R.id.hasSDTextView);
SDPathTextView=(TextView)findViewById(R.id.SDPathTextView);
FILESpathTextView=(TextView)findViewById(R.id.FILESpathTextView);
createFileTextView=(TextView)findViewById(R.id.createFileTextView);
readFileTextView=(TextView)findViewById(R.id.readFileTextView);
deleteFileTextView=(TextView)findViewById(R.id.deleteFileTextView);
helper=newFileHelper(getApplicationContext());
hasSDTextView.setText("SD卡是否存在:"+helper.hasSD());
SDPathTextView.setText("SD卡路徑:"+helper.getSDPATH());
FILESpathTextView.setText("包路徑:"+helper.getFILESPATH());
try{
createFileTextView.setText("創建文件:"
+helper.createSDFile("test.txt").getAbsolutePath());
}catch(IOExceptione){
e.printStackTrace();
}
deleteFileTextView.setText("刪除文件是否成功:"
+helper.deleteSDFile("xx.txt"));
helper.writeSDFile("1213212","test.txt");
readFileTextView.setText("讀取文件:"+helper.readSDFile("test.txt"));
}
}
I. android開發,怎麼讀取SD卡裡面的TXT文件介紹一下思路,有代碼的更好,感謝!!
StringBuffer sb = new StringBuffer();
File file = new File("myfile.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line = "";
while((line = br.readLine())!=null){
sb.append(line);
}
br.close();
(TextView)findViewById(R.id.text1).setText(sb.toString());
第二行,創建文件對象,指向需要讀取的文件
第三行,創建文件Reader對象,讀取指定的文件
第四五行,創建一個line接受讀取的文件內容,因為是文本文件,所以一行一行讀
第八行,關閉文件讀取對象
第九行,將文本文件內容寫入到TextVIew中
J. 簡述Android中如何利用文件存儲來讀寫SD卡上的TXT文件。
確定這么做就必須要意識到一旦放進去就成死的了。而且你說的「改一下」更好的方法是「擴展一下」 擴展下你的電紙書包含一些默認的文件。