① android写入txt文件
分以下几个步骤:
首先对manifest注册SD卡读写权限
java">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"));
}
}
② 如何在Android中从文件中读写字符串
1、通过File获取文件
2、打开输入流,读取文件
写文件:
1、创建文件
2、打开输出流,写入文件内容
示例:
12345678910111213
读文件:String content = ""; //文件内容字符串 //通过路径/sdcard/foo.txt打开文件 File file = new File("/sdcard/foo.txt"); try { InputStream instream = new FileInputStream(file);//读取输入流 InputStreamReader inputreader = new InputStreamReader(instream);//设置流读取方式 BufferedReader buffreader = new BufferedReader(inputreader); while (( line = buffreader.readLine()) != null) { content += line + "\n";//读取的文件内容 } }catch(Exception ex){ }
写文件: File file = new File("/sdcard/foo.txt");// if(!file.exists()) file.createNewFile();//如果文件不存在,创建foo.txt try { OutputStream outstream = new FileOutputStream(file);//设置输出流 OutputStreamWriter out = new OutputStreamWriter(outstream);//设置内容输出方式 out.write("文字内容");//输出内容到文件中 out.close(); } catch (java.io.IOException e) { e.printStackTrace(); }
③ android读取txt文件
您好,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问。
比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。
在raw中放入一个a.txt文件,然后就可以在Activity中使用getResources().openRawResource(R.raw.a);方法获取一个此文件的InputStream类,而后就可以很方便地进行读写a.txt了。
④ Android 如何通过java代码实现修改指定路径文件的读写权限。
请参考
publicclass MainActivity extends Activity{
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String apkRoot="chmod 777 "+getPackageCodePath();
SystemManager.RootCommand(apkRoot);
}
}
当然,运行的程序本身要有root权
⑤ Android 根目录下读写.txt文件
//根目录权限不允许,放到/data/packeg_dir下或SD卡中
packagecom.example.demo;
Filedir=Environment.getDataDirectory();//获取data目录
//Environment.getExternalStorageDirectory();//获取SD卡目录
FileoutFile=newFile(dir,"/data/com.example.demo/text.txt");//只能在自己的程序包里建立文件,这是权限问题
⑥ android如何读写/data/data文件
读写文件,和java中没有区别的Filefile=newFIle("文件绝对路径");注意这里的文件路径,windows平台下盘符是c:d:e:等android是linux内核,路径是/开头的其它的就是InputStreamoutputStream没什么区别
⑦ android中可以使用sharedpreferences类的实例完成文件读写操作吗
很多时候我们开发的软件需要向用户提供软件参数设置功能,例如我们常用的QQ,用户可以设置是否允许陌生人添加自己为好友。对于软件配置参数的保存,如果是window软件,通常会采用ini文件进行保存;如果是 j2se应用,通常会采用properties属性文件进行保存;如果是Android应用,Android 平台提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数。使用SharedPreferences保存数据,其背后是用xml文件存放数据,文件存放在/data/data//shared_prefs目录下。
因为SharedPreferences背后是使用xml文件保存数据,getSharedPreferences(name,mode)方法的第一个参数用于指定该文件的名称,名称不用带后缀,后缀会由Android自动加上。方法的第二个参数指定文件的操作模式,共有四种操作模式,这四种模式分别是:
Context.MODE_PRIVATE:为默认操作模式,代表该文件是私有数据,只能被应用本身访问,在该模式下,写入的内容会覆盖原文件的内容,如果想把新写入的内容追加到原文件中。可以使用Context.MODE_APPEND
Context.MODE_APPEND:模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件。
Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用来控制其他应用是否有权限读写该文件。
MODE_WORLD_READABLE:表示当前文件可以被其他应用读取;
MODE_WORLD_WRITEABLE:表示当前文件可以被其他应用写入。
如果希望文件被其他应用读和写,可以传入:
openFileOutput("123.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);
android有一套自己的安全模型,当应用程序(.apk)在安装时系统就会分配给它一个userid,当该应用要去访问其它资源(比如文件),就需要userid匹配。默认情况下,任何应用创建的文件,sharedpreferences数据库都应该是私有的(位于/data/data //files),其它程序无法访问。除非在创建时指定了Context.MODE_WORLD_READABLE或者 Context.MODE_WORLD_WRITEABLE ,只有这样其它程序才能正确访问。
另外Activity还提供了另一个getPreferences(mode)方法操作SharedPreferences,这个方法默认使用当前类不带包名的类名作为文件的名称。
如果访问其它应用中的Preference,前提条件是该preference创建时指定了Context.MODE_WORLD_READABLE或 者Context.MODE_WORLD_WRITEABLE权限。如有个为cn.itcast.action的应用使用下面语句创建了preference:
getSharedPreferences("123", Context.MODE_WORLD_READABLE);
其它应用要访问上面应用的preference,首先需要创建上面应用的Context,然后通过Context 访问preference ,访问preference时会在应用所在包下的shared_prefs目录找到preference。如果不通过创建Context访问其他应用的preference,可以以读取xml文件方式直接访问其他应用preference对应的xml文件,如:
File xmlFile = new File(“/data/data//shared_prefs/itcast.xml”);//应替换成应用的包名
⑧ 如何在安卓/data目录下进行文件的读写操作
/** * 存储文件 * @param context 设备上下文 * @param btimap 位图 * @param bitmapName 位图名称 * @return */ @SuppressLint("WorldWriteableFiles") @SuppressWarnings("deprecation") private static boolean saveBitmap( Context context , Bitmap btimap , String bitmapName ) { try { FileOutputStream fOut = contextpress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); return true; } catch (Exception e) { e.printStackTrace(); } return false; } 以上代码仅供参考。 通过以上代码可以在data文件夹下的应用的包名文件夹下新建文件。 希望能够帮到你
⑨ android 读写文件需要哪些权限
<!--往sdcard中写入数据的权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission><!--在sdcard中创建/删除文件的权限 --><uses-permission android:name="android.permission.MOUNT_U
android中的apk必须签名
这种签名不是基于权威证书的,不会决定某个应用允不允许安装,而是一种自签名证书。
重要的是,android系统有的权限是基于签名的。比如:system等级的权限有专门对应的签名,签名不对,权限也就获取不到。默认生成的APK文件是debug签名的。
获取system权限时用到的签名,见:如何使Android应用程序获取系统权限。基于UserID的进程级别的安全机。这种签名不是基于权威证书的,不会决定某个应用允不允许安装,而是一种自签名证书。重要的是,android系统有的权限是基于签名的。