【数据存储】利用Activity类操作数据文件
保存数据到txt文本
private static final String FILENAME = "d.txt"; private TextView tv_msg = null; /** 保存数据到txt文本 */ FileOutputStream output = null; // 接收文件输出对象 try { // 设置输出的文本名称,及文件创建方式 output = super.openFileOutput(FILENAME, Activity.MODE_PRIVATE); // 打印流包装 PrintStream out = new PrintStream(output); out.print("姓名:张三,"); out.print("年龄:18,"); out.println("地址:中国香港."); out.close(); } catch (Exception e) { // TODO: handle exception }
读取数据
FileInputStream input = null; try { // 取得输入流 input = super.openFileInput(FILENAME); Scanner scan = new Scanner(input); while (scan.hasNext()) { this.tv_msg.append(scan.next() + "\n"); } scan.close(); } catch (Exception e) { // TODO: handle exception }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步