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文件。
确定这么做就必须要意识到一旦放进去就成死的了。而且你说的“改一下”更好的方法是“扩展一下” 扩展下你的电纸书包含一些默认的文件。