从textview数据保存到android中的文件 [英] Saving from textview data into file in android
本文介绍了从textview数据保存到android中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从TextView将数据保存到文本文件中。
但在保存之前我想操纵数据。
我得到的数据来自串口。
到textview。
我能够保存直接从textview转换成文件。
但我想操纵数据。
喜欢split,substring并在textview数据中添加一些字符串。
我尝试过:
Hi,
I want to save data into text file from TextView.
but before saving i want to manipulate the data.
data which i am getting is from serial port.
into textview.
I am able to save into file directly from textview.
but i want to manipulate the data.
like split,substring and adding some string to the textview data.
What I have tried:
btnSave5.setOnClickListener(new View.OnClickListener() {
String fileName1 = "n1.txt";
String fileName = "IT_"+formatter.format(now) + ".txt";
@Override
public void onClick(View v)
{
try
{
File root = new File(Environment.getExternalStorageDirectory(), "IT1");
if (!root.exists()) {
root.mkdirs();
root.createNewFile();
}
File gpxfile = new File(root, fileName);
FileWriter writer = new FileWriter(gpxfile,true);
writer.write(tvTerminal.getText()+"\n\n");
writer.flush();
writer.close();
//till this the code works good and file will be saved
File gpxfile1 = new File(root, fileName1);
FileWriter writer1 = new FileWriter(gpxfile1,true);
String r = "TIME VOLTAGE CURRENT IR-VALUE";
// The below code is not working
//when i try to save it is exiting
String spdata = tvTerminal.getText().toString();
String pdata = spdata.substring(78);
String abc = heading + r;
writer1.write(abc);
writer1.flush();
writer1.close();
// clear.setVisibility(View.VISIBLE);
}
catch(IOException e)
{
e.printStackTrace();
}
}
});
推荐答案
你应该验证<$ c的长度在获取子字符串之前$ c> spdata 。如果它短于78个字符,它将抛出异常并且不会完成写入。
You should verify the length ofspdata
before taking the substring. If it's shorter than 78 characters, it will throw an exception and won't finish writing.
if(spdata.length() < 78)
{
// Maybe show an error to the user.
return;
}
String pdata = spdata.substring(78);
这篇关于从textview数据保存到android中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文