当前位置:网站首页>FileOutputStream(文件储存)与FileInputStream(文件读取)
FileOutputStream(文件储存)与FileInputStream(文件读取)
2022-07-27 17:02:00 【Ashurol】
public class MainActivity extends ActionBarActivity {
Button bt;
TextView tv;
EditText et;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt=(Button) findViewById(R.id.button1);
tv=(TextView) findViewById(R.id.textView1);
et=(EditText) findViewById(R.id.editText1);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
save(et.getText().toString());
tv.setText(Reader());
}
});
}
//保存文件
public void save(String content)
{
try {
FileOutputStream fos = openFileOutput("a.txt", MODE_PRIVATE);
fos.write(content.getBytes());//将一个string类型的字符串中包含的字符转换成byte类型并且存入一个byte数组中。
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//读取文件
public String Reader()
{
String content=null;
try {
FileInputStream fis=openFileInput("a.txt");
ByteArrayOutputStream bos=new ByteArrayOutputStream();
byte[] b=new byte[1024];
int len=0;
while((len=fis.read(b))!=-1)//读取数据并写入数组buffer,判断当fis是否读取完毕,起到缓存作用
{
bos.write(b, 0, len);//将数组字节写入ByteArrayOutputStream对象内
}
content=bos.toString();
fis.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return content;
}
}


边栏推荐
- Map and set
- Chinese character search Pinyin wechat applet project source code
- 坚持软硬一体化,一恒科发力智能化教育机器人市场
- Golang sets the domestic image, vscode configures the golang development environment, and vscode debugs the golang code
- Web3.0热浪之下 门萨Mensa生态震撼来袭
- 应用程序池已被禁用
- It is said that Apple plans to buy some JDI factories with us $200million
- Cumulative output data of kettle Excel
- [deep learning target detection series - 01] what is target detection
- Samsung will promote a number of risc-v architecture chips, and 5g millimeter wave RF chips will be the first to be adopted
猜你喜欢

Low code implementation exploration (45) business parameters

Release Samsung 3J1 sensor: the code implies that the safety of pixel 7 face recognition will be greatly increased

成本高、落地难、见效慢,开源安全怎么办?

jvisualvm的使用

C language: 9. Return in main function

Complex number proof of solvability of regular 17 sided ruler and gauge drawing
技术实践干货 | 初探大规模 GBDT 训练

Complete source code of E-commerce mall applet project (wechat applet)

Web3.0热浪之下 门萨Mensa生态震撼来袭

原厂PW4203降压型1-3节锂电池充电芯片
随机推荐
The valuation exceeds 15.6 billion yuan! Huaqin communication completed the round B financing of 1billion yuan! Qualcomm venture capital, Intel Capital led investment
IIS 发生未知FastCGI错误:0x80070005
S32k series chips -- Introduction
成本高、落地难、见效慢,开源安全怎么办?
[daily accumulation - 07] CUDA multi version switching
HDU1323_ Perfection [water question]
Flink 算子简介
c语言:5、多维数组
三星将推多款RISC-V架构芯片,5G毫米波射频芯片会率先采用
Yanghui triangle
坚持软硬一体化,一恒科发力智能化教育机器人市场
Dry goods of technical practice | preliminary exploration of large-scale gbdt training
Make your chat bubbles colorful
I want to consult. Our maxcompute spark program needs to access redis, development environment and production environment redis
[basic knowledge of deep learning - 46] Bayesian theorem and conditional probability formula
反超华为?爱立信已拿下超过75份5G商用合同
估值超156亿元!华勤通讯完成10亿元B轮融资!高通创投、英特尔资本领投
C language: 11. Pipeline
Intel's process roadmap for the next 10 years is exposed: 1.4nm process will be launched in 2029! How?
原厂PW4203降压型1-3节锂电池充电芯片