I want to increment fileno every time I save in android

  • Replies:0
Dhananjay mks
  • Forum posts: 1

Nov 22, 2017, 5:58:34 AM via Website

Hi sir,
I need a help ,
i have two textview
i want to save them in a file.
and fileno should increment every time i save(it should create a new file.

please help me to code with
1.write into same file from 2 textview
2. creating new file every time i click save button.with new file no

example.
my folder is "ET"
i am saving file "et1.txt"
on closing app and when i click save it should be "et2.txt"

Thank you and hoping for great help

What I have tried:

public void bt1Onclick2(View v) {
bt1 = (Button) findViewById(R.id.buttonSend);
tv1 = (TextView) findViewById(R.id.textView1);
editText = (EditText) findViewById(R.id.editText1);

try {

String h = "r";
int n =1;

File root = new File(Environment.getExternalStorageDirectory(), "ET");

// if external memory exists and folder with name Notes

if (!root.exists()) {

root.mkdirs();// this will create folder.

}

File filepath = new File(root, "h"); // file path to save

File myDir = new File("ET");
String[] files = myDir.list();

// File file = new File(myDir, at.getText().toString() + ".jpg");

for (int num = 0; root.exists(); num++) {
root = new File(filepath, tv1.getText().toString() + num + ".txt");

}

FileWriter writer = new FileWriter(filepath);
// writer.append(tv1.getText().toString());
writer.append(locationText.getText().toString());
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: "
+ "\nLong: " , Toast.LENGTH_LONG).show();
writer.flush();

writer.close();

String m = h + ".txt";

editText.setText(m);
tv1.setText("");

}

catch (IOException e) {

e.printStackTrace();

editText.setText(e.getMessage().toString());

}
}

Reply