Reputation: 53
please give me an hand.
i'm trying to write something in a file text in Android internal storage.
i'm using the stream method like the following. By the way, it does not write .
could you help me to solve?
public class MainActivity extends ActionBarActivity {
File f=new File("box");
public void scrivi(String data)
{try {
FileOutputStream fos = new FileOutputStream(f);
PrintWriter pw = new PrintWriter(fos);
pw.write("");
}
catch(FileNotFoundException e ){
Toast t = Toast.makeText(this, ("Il FILE NON ESITE"), Toast.LENGTH_LONG);
t.show();
}
}
public void read( String word)
{try{
FileInputStream fis = new FileInputStream(f);
BufferedReader br = new BufferedReader(new InputStreamReader(fis));
String line="";
while((line=br.readLine())!=null);
println(line);
}
catch(FileNotFoundException n){Toast x = Toast.makeText(this, ("Il FILE NON SI PUO' LEGGERE"), Toast.LENGTH_LONG);
x.show();}
catch(IOException w){{Toast q = Toast.makeText(this, ("c'e' un problema"), Toast.LENGTH_LONG);
q.show();}}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList lista = new ArrayList();
Intent j = getIntent();
String nome = j.getStringExtra("nome");
lista.add(nome);
scrivi(nome);
if(nome!=null)
{
Toast toast = Toast.makeText(this, ("ciao " + " " + nome), Toast.LENGTH_LONG);
toast.show();
}}
public void go(View v){
Intent intent=new Intent(this,SecondActivity.class);
startActivity(intent);
}
public void time(View c){
Intent i=new Intent(this,ActivityPicker.class);
startActivity(i);
}
public void pass(View p){
EditText nome6=(EditText)findViewById(R.id.nome6);
String inviaDati = nome6.getText().toString();
read(inviaDati);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Views: 174
Reputation: 3818
It's because you're not using data
inside scrivi()
method. Try with this:
public void scrivi(String data, Context ctx){
try {
FileOutputStream fos = ctx.openFileOutput();
PrintWriter pw = new PrintWriter(fos);
pw.write(data);
}
catch(FileNotFoundException e ){
Log.e("ERR", e.getMessage();
}
}
As a side note, I don't recommend to use Toast in inner "business" methods, as it may not work as expected if called outside Activity context. To trace errors, just use Log.warn/info/debug
Upvotes: 0
Reputation: 40337
File f=new File("box");
FileOutputStream fos = new FileOutputStream(f);
Will attempt to open and if necessary create a file in the current working directory which is not writeable on Android
If you replace new fileOutputStream(f)
with openFileOutput(f)
called within or on an instance of an Activity or other valid Context you will get a file in your app's internal storage as suggested by your title.
openFileInput(f)
can be used on the reading side. You can also obtain this location as a path with getFilesDir()
should you want to use it explicitly.
Shine's recommendations of writing something non empty and using logging rather than Toasts for detail are also important.
I'd additionally suggest you explicitly close the output stream rather than waiting for that to happen when it is garbage collected some unpredictable time after the end of the method where it was contained.
Upvotes: 1