Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
		}
	}
});
Posted
Updated 11-Jun-18 19:34pm
v2
Comments
David Crow 12-Jun-18 10:49am    
So have you used the debugger to step through the code to find which of those six statements is in error? Is an exception being thrown?

1 solution

You should verify the length of spdata 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);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900