当前位置:首页 » 安卓系统 » android读写txt

android读写txt

发布时间: 2022-09-10 09:03:59

Ⅰ android编程:怎样读取txt文件

android 能读取的文件都是系统里面的(这是系统不是开发坏境系统,而是你程序运行的环境系统,也就是avd或者真实的手机设备的sd卡),这就需要你把文件导入你的环境中,mnt目录底下,然后按到读取sd卡的路径读取即可。

Ⅱ Android 根目录下读写.txt文件

java">//根目录权限不允许,放到/data/packeg_dir下或SD卡中

packagecom.example.demo;

Filedir=Environment.getDataDirectory();//获取data目录
//Environment.getExternalStorageDirectory();//获取SD卡目录
FileoutFile=newFile(dir,"/data/com.example.demo/text.txt");//只能在自己的程序包里建立文件,这是权限问题

Ⅲ 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();
}
}
希望能解决您的问题。

Ⅳ android 开发怎么读取并显示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中

Ⅳ android读取txt文件

您好,Android的res文件夹是用来存储资源的,可以在res文件夹下建立一个raw文件夹,放置在raw文件夹下的内容会被原样打包,而不会被编译成二进制文件,并且可以通过R文件进行很方便地访问
比如我们可以将更新信息、版权信息等放到txt文件中,然后放到raw文件中,然后很方便地进行访问。
在raw中放入一个a.txt文件,然后就可以在Activity中使用getResources().openRawResource(R.raw.a);方法获取一个此文件的InputStream类,而后就可以很方便地进行读写a.txt了。

Ⅵ android 如何读写文件

读文件:

1、通过File获取文件

2、打开输入流,读取文件

写文件:

1、创建文件

2、打开输出流,写入文件内容


示例:

读文件:
Stringcontent="";//文件内容字符串
//通过路径/sdcard/foo.txt打开文件
Filefile=newFile("/sdcard/foo.txt");
try{
InputStreaminstream=newFileInputStream(file);//读取输入流
InputStreamReaderinputreader=newInputStreamReader(instream);//设置流读取方式
BufferedReaderbuffreader=newBufferedReader(inputreader);
while((line=buffreader.readLine())!=null){
content+=line+" ";//读取的文件内容
}
}catch(Exceptionex){
}
写文件:
Filefile=newFile("/sdcard/foo.txt");//
if(!file.exists())
file.createNewFile();//如果文件不存在,创建foo.txt
try{
OutputStreamoutstream=newFileOutputStream(file);//设置输出流
OutputStreamWriterout=newOutputStreamWriter(outstream);//设置内容输出方式
out.write("文字内容");//输出内容到文件中
out.close();
}catch(java.io.IOExceptione){
e.printStackTrace();
}

Ⅶ android怎么读取txt文件

File urlFile = new File("/sdcard/test.txt");
InputStreamReader isr = new InputStreamReader(new FileInputStream(urlFile), "UTF-8");
BufferedReader br = new BufferedReader(isr);
String str = "";
String mimeTypeLine = null ;
while ((mimeTypeLine = br.readLine()) != null) {
str = str+mimeTypeLine;
}

Ⅷ 简述Android中如何利用文件存储来读写SD卡上的TXT文件。

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

Ⅸ Android写入txt文件

分以下几个步骤:

  1. 首先对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>
  2. 创建一个对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;
    }
    }
  3. 写一个用于检测读写功能的的布局

    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>
  4. 就是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如何读取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();
}
}

热点内容
ubuntuphp版本 发布:2024-05-19 21:59:12 浏览:928
解压文案馆 发布:2024-05-19 21:58:54 浏览:870
苏宁访问数 发布:2024-05-19 21:53:49 浏览:580
湿地下载ftp 发布:2024-05-19 21:46:10 浏览:487
java二分查找算法 发布:2024-05-19 21:37:38 浏览:347
所有编程语言 发布:2024-05-19 21:33:55 浏览:665
c语言1到10的阶乘的和 发布:2024-05-19 21:32:25 浏览:628
php匹配标点符号 发布:2024-05-19 21:14:49 浏览:753
可以拍照输入的c语言编译器 发布:2024-05-19 21:09:47 浏览:182
解压升降机 发布:2024-05-19 20:51:11 浏览:968