problem in wrting user input to file

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

problem in wrting user input to file

Postby rag13 » Wed Oct 07, 2009 6:16 am

hi,
i am facing the problem in writing user input to file i.e,
i got some sample code of user input below i have mentioned the code
in the code user inputs some data, that data i need to wirte to file,
if i use out.write() its giving error, i am not able to type cast edittext to byte
please help
how to write to user input to file ? or
how to pass the user input to native code funtion ?

java code :

package co.readfile.ex1;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;

import android.app.Activity;
import android.content.Context;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class ReadFileActivity extends Activity
{
private static String FILENAME = "ex1.txt";

private static String PATH = "/data/data/co.readfile.ex1/files/" + FILENAME;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button okButton = (Button) findViewById(R.id.ok);
okButton.setOnClickListener(okOnClickListener);
Button cancelButton = (Button) findViewById(R.id.cancel);
cancelButton.setOnClickListener(cancelOnClickListener);

TextView tv = new TextView(this);
}

private Button.OnClickListener okOnClickListener = new Button.OnClickListener()
{
public void onClick(View v)
{
EditText edit_text = (EditText)
findViewById(R.id.message_text);
CharSequence edit_text_value = edit_text.getText();
setTitle("Hello:"+edit_text_value);

ObjectOutputStream objectOut = null;
try
{
FileOutputStream out = openFileOutput(FILENAME, Context.MODE_PRIVATE);
objectOut = new ObjectOutputStream(new BufferedOutputStream(out));

out.write(edit_text_value); // error at this line ?
}
catch (IOException ex)
{
//.append("Unable to write test file from Java, see logcat for details");
}

}
};

private Button.OnClickListener cancelOnClickListener = new Button.OnClickListener()
{
public void onClick(View v)
{
finish();
}
};
}

main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Who you are?"
/>
<EditText
android:id = "@+id/message_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id = "@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK" />
<Button
android:id = "@+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel" />
</LinearLayout>
rag13
Freshman
Freshman
 
Posts: 5
Joined: Wed Oct 07, 2009 4:44 am

Top

Postby blackgin » Wed Oct 07, 2009 1:24 pm

I think you must .close() the file if not, any change is not saved.
The line you commented:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. out.write(edit_text_value);
  2.  
  3. out.close();
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


PS (use java and xml tags when you're posting a code)
User avatar
blackgin
Developer
Developer
 
Posts: 33
Joined: Tue Apr 21, 2009 1:21 pm

Postby SlumberMachine » Mon Oct 19, 2009 9:42 pm

If you are saving to your apps data directory you do not have to specify the directory, just use this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String fileName = "blah.txt";
  2.  
  3. String fullFileName = getFileStreamPath(fileName).getAbsolutePath();
  4.  
  5.  
Parsed in 0.030 seconds, using GeSHi 1.0.8.4


That will avoid any file path errors.
SlumberMachine
Developer
Developer
 
Posts: 39
Joined: Tue Feb 24, 2009 9:00 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 4 guests