android读取data/data/包名/file路径下的txt文件

本文介绍了一个使用Java实现的读取Android应用内部data目录下文件的方法,包括打开文件输入流、读取文件内容并根据文件编码类型进行解码的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文件不能太大否则会报内存溢出
[java]  view plain copy
  1. package yu.bin;  
  2.   
  3. import java.io.FileInputStream;  
  4. import org.apache.http.util.EncodingUtils;  
  5. import android.app.Activity;  
  6. import android.os.Bundle;  
  7. import android.widget.TextView;  
  8.   
  9. public class ReaddataPathActivity extends Activity {  
  10.     TextView textView;  
  11.   
  12.     // 这个是读取data/data/包名/file路径下的文件  
  13.     // 这个目录可以用getFilesDir()方法得到  
  14.     /** Called when the activity is first created. */  
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.main);  
  19.         textView = (TextView) findViewById(R.id.tvtext);  
  20.         String txt = "";  
  21.         try {  
  22.             // 获取文件  
  23.             FileInputStream fin = openFileInput("name.txt");  
  24.             // 获得长度  
  25.             int length = fin.available();  
  26.             // 创建字节数组  
  27.             byte[] buffer = new byte[length];  
  28.             // 读取内容  
  29.             fin.read(buffer);  
  30.             // 获得编码格式  
  31.             String type = codetype(buffer);  
  32.             // 按编码格式获得内容  
  33.             txt = EncodingUtils.getString(buffer, type);  
  34.             textView.setText(txt);  
  35.         }  
  36.         catch(Exception e) {  
  37.             // TODO: handle exception  
  38.         }  
  39.     }  
  40.   
  41.     private String codetype(byte[] head) {  
  42.         String type = "";  
  43.         byte[] codehead = new byte[3];  
  44.         System.arraycopy(head, 0, codehead, 03);  
  45.         if(codehead[0] == -1 && codehead[1] == -2) {  
  46.             type = "UTF-16";  
  47.         }  
  48.         else if(codehead[0] == -2 && codehead[1] == -1) {  
  49.             type = "UNICODE";  
  50.         }  
  51.         else if(codehead[0] == -17 && codehead[1] == -69 && codehead[2] == -65) {  
  52.             type = "UTF-8";  
  53.         }  
  54.         else {  
  55.             type = "GB2312";  
  56.         }  
  57.         return type;  
  58.     }  
  59. }  



转载于:https://www.cnblogs.com/jackrex/archive/2013/01/24/3001265.html

Android Studio中读取`/data/tool/SnInfo`文件中的信息,可以通过以下步骤实现。由于`/data/tool/`目录通常需要系统权限才能访问,因此需要在Android设备上获取root权限,或者将应用声明为系统应用。 以下是实现步骤: 1. **获取读取权限**: - 如果设备已root,可以在代码中请求root权限。 - 如果应用是系统应用,可以在`AndroidManifest.xml`中声明相关权限。 2. **读取文件内容**: - 使用`FileInputStream`或`BufferedReader`读取文件内容。 以下是一个示例代码,演示如何在Android Studio中读取`/data/tool/SnInfo`文件中的信息: ```java import android.os.Bundle; import android.util.Log; import androidx.appcompat.app.AppCompatActivity; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStreamReader; public class MainActivity extends AppCompatActivity { private static final String TAG = "SnInfoReader"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); readSnInfoFile(); } private void readSnInfoFile() { // 检查设备是否root if (isDeviceRooted()) { try { File file = new File("/data/tool/SnInfo"); FileInputStream fis = new FileInputStream(file); BufferedReader reader = new BufferedReader(new InputStreamReader(fis)); StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = reader.readLine()) != null) { stringBuilder.append(line).append("\n"); } reader.close(); fis.close(); String snInfo = stringBuilder.toString(); Log.d(TAG, "SnInfo: " + snInfo); } catch (Exception e) { Log.e(TAG, "Error reading SnInfo file", e); } } else { Log.e(TAG, "Device is not rooted"); } } private boolean isDeviceRooted() { String[] paths = {"/system/bin/", "/system/xbin/", "/sbin/", "/system/sd/xbin/", "/system/bin/failsafe/", "/data/local/xbin/", "/data/local/bin/", "/data/local/"}; for (String path : paths) { File file = new File(path + "su"); if (file.exists()) { return true; } } return false; } } ``` ### 注意事项: 1. **权限问题**:读取`/data/tool/SnInfo`文件需要系统级权限,普通应用无法访问该目录。需要将应用声明为系统应用或设备已root。 2. **安全性**:处理系统文件时要小心,确保不会泄露敏感信息。 3. **错误处理**:在实际应用中,添加更多的错误处理逻辑以应对各种可能的异常情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值