当前位置: 首页 > news >正文

西宁网站建设价格低拉新推广一手接单平台

西宁网站建设价格低,拉新推广一手接单平台,做美团类似的网站,沈阳中联做网站1 实验名称 文件的读写 2 实验目的 掌握Android中读写文件的实现方法。 3 实验源代码 布局文件代码&#xff1a; <?xml version"1.0" encoding"utf-8"?> <LinearLayout xmlns:android"http://schemas.android.com/apk/res/android&quo…

1 实验名称

       文件的读写

2 实验目的

       掌握Android中读写文件的实现方法。

3 实验源代码

布局文件代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/main"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"tools:context=".MainActivity"><EditTextandroid:id="@+id/inputET"android:layout_width="match_parent"android:layout_height="wrap_content"android:hint="请输入你想写入的内容"android:minLines="2"/><Buttonandroid:id="@+id/inputBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="写入"/><EditTextandroid:id="@+id/outputET"android:layout_width="match_parent"android:layout_height="wrap_content"android:enabled="false"/><Buttonandroid:id="@+id/outputBtn"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="读取"/>
</LinearLayout>

Java代码:

package com.example.fileiotest;import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;public class MainActivity extends AppCompatActivity {private EditText inputET = null;private EditText outputET = null;private Button inputBtn = null;private Button outputBtn = null;private String fileName = "content.txt";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);inputET = findViewById(R.id.inputET);outputET = findViewById(R.id.outputET);inputBtn = findViewById(R.id.inputBtn);outputBtn = findViewById(R.id.outputBtn);inputBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {try {//将用户输入的信息写入到文件中String inputContent = inputET.getText().toString().trim();//创建输出流管道FileOutputStream fos = openFileOutput(fileName, Context.MODE_APPEND);//向文件中写入数据fos.write(inputContent.getBytes());inputET.setText("");fos.close();} catch (IOException e) {throw new RuntimeException(e);}}});outputBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {try {//从文件中读取数据//创建一个连接文件的输入流管道FileInputStream fis = openFileInput(fileName);//创建一个存放读取过来的数据是缓存数组byte[] bufffer = new byte[256];//创建可变长度的缓存字符串StringBuffer stringBuffer = new StringBuffer("");int hasRead = 0;//记录读取量while ((hasRead=fis.read(bufffer)) != -1){stringBuffer.append(new String(bufffer,0,hasRead));}outputET.setText(stringBuffer.toString());fis.close();} catch (IOException e) {throw new RuntimeException(e);}}});}}

4 实验运行结果图

5 实验总结

       第一步,写布局文件。主要是两个按钮,一个文本框,一个文本编辑框;两个按钮分别是写入和读取。

       第二步,写Java代码。关于写入,将用户输入的信息写入到文件中,创建输出流管道,向文件中写入数据;关于读取,从文件中读取数据,创建一个连接文件的输入流管道和一个存放读取过来的数据是缓存数组,创建可变长度的缓存字符串,记录下读取量。

       这个实验存在些许不足,个人认为主要是在于一次只能读取一个先前写入的内容,这个部分应该还可以继续优化。

http://www.15wanjia.com/news/36692.html

相关文章:

  • 专门做尾单的那个网站叫啥网络优化工程师有多累
  • wdcp 无法访问此网站怎么在百度上推广自己
  • ps切片做网站网络营销师资格证
  • java做的网站源码谷歌浏览器下载电脑版
  • 网站开发功能书舆情信息在哪里找
  • 男人和女人在床上做那个网站网络营销推广论文
  • wordpress user login网站seo完整seo优化方案
  • 好的素材下载网站论坛软文案例
  • 做借贷网站平台深圳seo推广培训
  • 做户外商城网站sem公司
  • wordpress网页后台安卓优化大师官方下载
  • 网站seo搜索引擎优化教程郑州网站推广效果
  • 爱网之家惠州seo优化服务
  • 电商购物网站开发需求分析谷歌优化推广
  • 广州网站建设服务哪家好网站seo排名优化工具在线
  • 西乡移动网站建设百度在线客服人工服务
  • 游戏系统网站开发说明书杭州关键词排名工具
  • 阿卯网站建设百度官方官网
  • 网站版块策划网络舆情监控
  • 东丰在线网站建设免费网站推广群发软件
  • 网站中验证码如何做的建设网站推广
  • 做网站抬头天津搜索引擎seo
  • 网站未建设完善是什么意思青岛网站seo分析
  • 网站文章超链接怎么做百度排名怎么做
  • 电子商务网站平台建设sem竞价推广代运营收费
  • 高并发网站建设网络平台宣传方式有哪些
  • 推广软件是什么意思网站优化的关键词
  • 做网站之前要安装什么广州网站排名推广
  • 淘宝客怎么自己做网站及APP新闻类软文营销案例
  • 网站做缓存seo课程多少钱