Android TXT文件读写

package com.wirelessqa.helper;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.http.util.EncodingUtils;

import android.app.Activity;

public class FileAccess extends Activity {

	/**
	 * 一、私有文件夹下的文件存取(/data/data/包名/files)
	 * 
	 * @param fileName
	 * @param message
	 */
	public void writeFileData(String fileName, String message) {
		try {
			FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);
			byte[] bytes = message.getBytes();
			fout.write(bytes);
			fout.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * //读文件在./data/data/包名/files/下面
	 * 
	 * @param fileName
	 * @return
	 */
	public String readFileData(String fileName) {
		String res = "";
		try {
			FileInputStream fin = openFileInput(fileName);
			int length = fin.available();
			byte[] buffer = new byte[length];
			fin.read(buffer);
			res = EncodingUtils.getString(buffer, "UTF-8");
			fin.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput
	 * 不同点:openFileOutput是在raw里编译过的,FileOutputStream是任何文件都可以
	 * @param fileName
	 * @param message
	 */
	// 写在/mnt/sdcard/目录下面的文件
	public void writeFileSdcard(String fileName, String message) {

		try {

			// FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE);

			FileOutputStream fout = new FileOutputStream(fileName);

			byte[] bytes = message.getBytes();

			fout.write(bytes);

			fout.close();

		}

		catch (Exception e) {

			e.printStackTrace();

		}

	}

	// 读在/mnt/sdcard/目录下面的文件

	public String readFileSdcard(String fileName) {

		String res = "";

		try {

			FileInputStream fin = new FileInputStream(fileName);

			int length = fin.available();

			byte[] buffer = new byte[length];

			fin.read(buffer);

			res = EncodingUtils.getString(buffer, "UTF-8");

			fin.close();

		}

		catch (Exception e) {

			e.printStackTrace();

		}

		return res;

	}


	/**
	 * 二、从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
	 * 
	 * @param fileInRaw
	 * @return
	 */
	public String readFromRaw(int fileInRaw) {
		String res = "";
		try {
			InputStream in = getResources().openRawResource(fileInRaw);
			int length = in.available();
			byte[] buffer = new byte[length];
			in.read(buffer);
			res = EncodingUtils.getString(buffer, "GBK");
			// res = new String(buffer,"GBK");
			in.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}

	/**
	 * 三、从asset中获取文件并读取数据(资源文件只能读不能写)
	 * 
	 * @param fileName
	 * @return
	 */
	public String readFromAsset(String fileName) {
		String res = "";
		try {
			InputStream in = getResources().getAssets().open(fileName);
			int length = in.available();
			byte[] buffer = new byte[length];
			in.read(buffer);
			res = EncodingUtils.getString(buffer, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return res;
	}



}

  • 11
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
### 回答1: Android Studio可以通过Java的File类来读写txt文件。具体步骤如下: 1. 在Android Studio中创建一个新的Java类,例如FileUtils。 2. 在该类中添加读取txt文件的方法,例如readTxtFile: ``` public static String readTxtFile(String filePath) { StringBuilder result = new StringBuilder(); try { File file = new File(filePath); BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { result.append(line); } br.close(); } catch (IOException e) { e.printStackTrace(); } return result.toString(); } ``` 3. 在该类中添加写入txt文件的方法,例如writeTxtFile: ``` public static void writeTxtFile(String filePath, String content) { try { File file = new File(filePath); FileWriter fw = new FileWriter(file); fw.write(content); fw.close(); } catch (IOException e) { e.printStackTrace(); } } ``` 4. 在需要读写txt文件的地方调用这些方法即可。 注意:在使用File类读写文件时,需要添加读写文件的权限。在AndroidManifest.xml文件中添加以下代码: ``` <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ``` ### 回答2: Android Studio是谷歌推出的一款用于开发Android应用程序的集成开发环境。在Android应用程序开发中,读写txt文件是非常常见的需求,因此,本文将介绍如何在Android Studio中实现读写txt文件的操作。 一、读取文件 读取txt文件可以使用Java IO包提供的FileReader和BufferedReader类。具体步骤如下: 1.在res文件夹下创建一个raw文件夹,将要读取的txt文件保存到该文件夹中。 2.在MainActivity类中,使用以下代码读取raw文件夹下的txt文件: ```Java InputStream inputStream = getResources().openRawResource(R.raw.textfile); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); String line = null; while ((line = reader.readLine()) != null) { // 在这里对读取到的文本进行处理 } ``` 在以上代码中,我们先用getResources().openRawResource()方法获取输入流,然后再用InputStreamReader和BufferedReader类读取txt文件中的内容。 二、写入文件 写入txt文件可以使用Java IO包提供的FileWriter和BufferedWriter类。具体步骤如下: 1.在MainActivity类中创建一个名为writeToFile()的方法,用于写入文本: ```Java private void writeToFile(String text) { try { FileOutputStream fileOutputStream = openFileOutput("textfile.txt", MODE_PRIVATE); OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream); BufferedWriter writer = new BufferedWriter(outputStreamWriter); writer.write(text); writer.close(); } catch (IOException e) { Log.e("Exception", "File write failed: " + e.toString()); } } ``` 在以上代码中,我们先使用openFileOutput()方法获取输出流,然后使用OutputStreamWriter和BufferedWriter类将text写入txt文件中。 2.调用writeToFile()方法写入文本: ```Java writeToFile("Hello world!"); ``` 以上就是在Android Studio中读写txt文件的方法。读取txt文件时需要注意文件的编码格式,而写入txt文件时则需要注意写入的权限问题。当然,在实际使用中,还要根据具体的需求进行适当的修改。 ### 回答3: Android Studio 是一个十分强大的开发环境,不仅可以支持编写 Android 应用程序的代码,还可以便捷地读写本地的 txt 文件。下面将从以下几个方面,详细介绍在 Android Studio 中如何进行 txt 文件读写操作。 1. 如何读取 txt 文件? 在 Android Studio 中读取 txt 文件有多种方法,其中比较常用的方法是使用 FileInputStream 和 InputStreamReader 两个类进行操作。可以按照以下步骤进行: 首先,定义一个 File 类型的变量,指定要读取的文件的路径: File file = new File("/sdcard/test.txt"); 接着,创建 FileInputStream 对象并传入文件的路径: FileInputStream fis = new FileInputStream(file); 最后,将 FileInputStream 对象封装成 InputStreamReader 对象并用 BufferedReader 进行读取: InputStreamReader isr = new InputStreamReader(fis); BufferedReader br = new BufferedReader(isr); String line = br.readLine(); 通过循环读取每一行,即可将 txt 文件中的内容全部读取出来。 2. 如何写入 txt 文件? 写入 txt 文件同样也可以使用 FileOutputStream 和 OutputStreamWriter 两个类进行操作。可以按照以下步骤进行: 首先,定义一个 File 类型的变量,指定要写入的文件的路径: File file = new File("/sdcard/test.txt"); 接着,创建 FileOutputStream 对象并传入文件的路径: FileOutputStream fos = new FileOutputStream(file); 然后,将 FileOutputStream 对象封装成 OutputStreamWriter 对象并用 BufferedWriter 进行写入: OutputStreamWriter osw = new OutputStreamWriter(fos); BufferedWriter bw = new BufferedWriter(osw); bw.write("hello world"); bw.flush(); 最后,记得要使用 flush() 方法将缓冲区的数据写入文件中。 总结: 以上就是在 Android Studio 中读写 txt 文件的操作方法。不过,需要注意的是,在进行文件操作时要确保有相应的文件读写权限。因此,在 AndroidManifest.xml 文件中声明如下权限: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 同时,在执行文件操作前需要先判断是否有相应的权限,如果没有则需要进行请求权限的操作。
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

毕小烦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值