I am wanting to pass the data added into these fields to a .txt file stored on the device.
Asked
Active
Viewed 482 times
2 Answers
0
In your xml file you can add android:onClick="bSomething"
to the properties of the button you want to click. Then on your activity class (or where you have your code that you posted) you can do something like:
public void bSomething(View view){
try{
FileOutputStream fout = openFileOutput(“yourfile.txt”,MODE_PRIVATE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(editText1.getText().toString()+" ");
osw.write(editText2.getText().toString()+" ");
osw.write(editText3.getText().toString()+" ");
osw.close();
fout.close();
}catch(Exception e){
//do the exception handling
}
}
Hope that helps.

0gravity
- 2,682
- 4
- 24
- 33
-
where did you put your text file? in what folder? – 0gravity Jul 28 '12 at 16:16
-
Oh i need to create the txt file first. What folder should i store it in? And if i'm storing the txt file in a folder do i need a path? – user1558614 Jul 28 '12 at 16:20
-
You can take a look at this post http://stackoverflow.com/questions/5771366/reading-a-simple-text-file – 0gravity Jul 28 '12 at 16:21
0
Try to do something as I show below:
Button btn11 = (Button) this.findViewById(R.id.buttonformdata);
btn11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try{
FileOutputStream fos = openFileOutput("yourFile", Context.MODE_PRIVATE);
String string1 = editText1.getText().toString();
String string2 = editText2.getText().toString();
String string3 = editText3.getText().toString();
fos.write(string1.getBytes());
fos.write(string2.getBytes());
fos.write(string3.getBytes());
fos.close();
}catch(Exception e){
Log.e("Exception", e.toString());
}
}
});
Let me know if it works!

yugidroid
- 6,640
- 2
- 30
- 45
-
I have tried to follow peoples suggestions here but i have some problems with the 'editText1' /2/3 fields. They always error out, and i think i might be putting these in the wrong file? The quick fix eclipse tells me to do - doesn't work. I wonder if you could possibly look at my project? If so the relevant files are Screen10.xml and Screen10.java. Would really appreciate some guidance, thanks. http://www.sendspace.com/file/9kzjtp – user1558614 Jul 28 '12 at 17:54
-
To get the text of an `EditText` view you have to "find" them through their Id's as you did with the `Button02` and `buttonformdata`. The quick fix doesn't work too because it tells you to declare null variables for the edittext 1-2-3. So, add the following lines to your onCreate method to resolve your issue: `EditText editText1 = (EditText) findViewById(R.id.editText1); EditText editText2 = (EditText) findViewById(R.id.editText2); EditText editText3 = (EditText) findViewById(R.id.editText3);` That will be all :) – yugidroid Jul 28 '12 at 18:48
-
Thanks for your awesome response, it doesn't error now however when i run the app and test the screen10.xml 'insert details' page it doesn't create the .txt file or record the details still. I'm looking for the file to be created in the assets folder right? Or am i looking in the wrong place? http://www.sendspace.com/file/a4oxf1 – user1558614 Jul 28 '12 at 19:19
-
You're welcome. If it really resolve your issue, please accept my answer. Now, you can't write files into assets folder. The code implemented by now only allows you to save your txt file into phone's internal memory, that means you can't see/ access the file (and using `MODE_PRIVATE` only your app can read and write to that file). Re-write the code to save the file into a sd card directory and you'll be able to access it. – yugidroid Jul 28 '12 at 20:56
-
How would i write the code to a file i could view? I'm assuming the re-writing of code you mention would be changing the file creation path to this: FileOutputStream fos = openFileOutput("assets/yourFile.txt", Context.MODE_WORLD_READABLE); instead of this FileOutputStream fos = openFileOutput("yourFile.txt", Context.MODE_PRIVATE); But i get this logcat error E/Exception(275): java.lang.IllegalArgumentException: File assets/yourFile.txt contains a path separator Would rewriting of the code to get it to a readable format require complete recoding? or a simple change? – user1558614 Jul 28 '12 at 21:30
-
Nevermind i realised the file is saved in the data/data/ file in the DDMS. It has worked correctly, thanks so much for your help aswell as everyone else - i wouldnt have got it done without you yugidroid :) :D – user1558614 Jul 28 '12 at 22:26
-
@user1558614, I'm glad to help you. Just one more thing, take a look at the [data storage](http://developer.android.com/guide/topics/data/data-storage.html) official documentation. This info should give you a better ideia of how does saving data with Android works. – yugidroid Jul 28 '12 at 22:48