android 读中文文本文件
AndroidManifest.xml中
加入:
<!-- 在SDCard中创建与删除文件权限 --> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!-- 往SDCard写入数据权限 --> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | package com.example.yanlei.wifi; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.text.method.ScrollingMovementMethod; import android.view.Menu; import android.widget.TextView; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; public class MainActivity extends AppCompatActivity { TextView txt = null ; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); txt = (TextView)findViewById(R.id.txt_show); txt.setMovementMethod(ScrollingMovementMethod.getInstance()); ShowMessage( "ok2" ); //readtxt(); readExcel(); ShowMessage( "ok3" ); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. //getMenuInflater().inflate(R.menu.main, menu); return true ; } /** * 获取内置SD卡路径 * * @return */ public String getInnerSDCardPath() { File sdDir = null ; boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 if (sdCardExist) { sdDir = Environment.getExternalStorageDirectory(); //获取跟目录 } String Path = sdDir.toString(); return Path; } public String getSDPath() { List<String> lResult = new ArrayList<String>(); try { Runtime rt = Runtime.getRuntime(); Process proc = rt.exec( "mount" ); InputStream is = proc.getInputStream(); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); String line; while ((line = br.readLine()) != null ) { if (line.contains( "extSdCard" )) { String[] arr = line.split( " " ); String path = arr[ 1 ]; File file = new File(path); if (file.isDirectory()) { lResult.add(path); } } } isr.close(); } catch (Exception e) { } int num=lResult.size(); //ShowMessage("外盘的个数:"+num); if (num> 0 ) { return lResult.get( 0 ).toString(); } else { return "" ; } } /* @param path 文件夹路径 */ public static boolean isExist(String path) { File file = new File(path); //判断文件夹是否存在,如果不存在则创建文件夹 if (!file.exists()) { //file.mkdir(); return false ; } return true ; } //判断文件是否存在 public boolean fileIsExists(String strFile) { try { File f = new File(strFile); if (!f.exists()) { return false ; } } catch (Exception e) { return false ; } return true ; } public void ShowMessage(String str) { // new AlertDialog.Builder( this ) .setMessage(str) .setPositiveButton( "确定" , new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialoginterface, int i) { //按钮事件 } }) .show(); } public String getFilePath() { String path = getSDPath(); if (path!= "" ) { if (isExist(path + "/qqq.txt" )) { return path; } } path = getInnerSDCardPath(); if (isExist(path + "/qqq.txt" )) { return path; } return path; } public static String readFile(String filePathAndName) { String fileContent = "" ; try { File f = new File(filePathAndName); if (f.isFile()&&f.exists()){ InputStreamReader read = new InputStreamReader( new FileInputStream(f), "gbk" ); BufferedReader reader= new BufferedReader(read); String line; while ((line = reader.readLine()) != null ) { fileContent += line; } read.close(); } } catch (Exception e) { System.out.println( "读取文件内容操作出错" ); e.printStackTrace(); } return fileContent; } public void readtxt() { { String path=getFilePath(); ShowMessage( "路径:" +path); if (path== "" ) { ShowMessage( "文件:" +path+ "/qqq.txt" + "不存在" ); return ; } ShowMessage( "11111111111111111111" ); ShowMessage(readFile(path + "/qqq.txt" )); ShowMessage( "222222222" ); } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理