java - File created but returns false -
i don't understand why don't enter in second if. first check if file exists (no, logic), create it, , check again still returns false. tried hour find problem , i'm sure it's stupid error. sorry poor english
here code:
package com.example.testcreatefileonclick; import java.io.file; import java.io.fileoutputstream; import java.io.outputstreamwriter; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class main extends activity implements android.view.view.onclicklistener{ button button; button addteam; boolean append = true; string name = "nomfichier.txt"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); button = (button)findviewbyid(r.id.button1); button.setonclicklistener((onclicklistener) this); addteam = (button)findviewbyid(r.id.button2); addteam.setonclicklistener((onclicklistener) this); } @override public void onclick(view v) { try { file fichier = new file(name); if (!fichier.exists()) { system.out.println("file doesn't exists"); } fileoutputstream fout = openfileoutput(name, mode_world_readable); outputstreamwriter osw = new outputstreamwriter(fout); osw.write("text"); osw.flush(); osw.close(); //why don't go in if ? if (fichier.exists()) { system.out.println("file exists"); } } catch (exception e) { // todo: handle exception } } }
edit: working code
package com.example.testcreatefileonclick; import java.io.file; import java.io.filewriter; import java.io.ioexception; import android.app.activity; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; public class main extends activity implements android.view.view.onclicklistener{ button button; button addteam; boolean append = true; string name = "nomfichier.txt"; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); button = (button)findviewbyid(r.id.button1); button.setonclicklistener((onclicklistener) this); addteam = (button)findviewbyid(r.id.button2); addteam.setonclicklistener((onclicklistener) this); } @override public void onclick(view v) { try { string filepath = (this.getfilesdir().getpath().tostring()); file fichier = new file(filepath + name); if (!fichier.exists()) { system.out.println("file doesn't exists"); } fichier.createnewfile(); filewriter file = new filewriter(filepath + name); file.write("text"); file.flush(); file.close(); if (fichier.exists()) { system.out.println("file exists"); } } catch (ioexception e) { system.out.println("exception"); e.printstacktrace(); } } }
try replacing :
fileoutputstream fout = openfileoutput(name, mode_world_readable); outputstreamwriter osw = new outputstreamwriter(fout); osw.write("text"); osw.flush(); osw.close();
by :
fichier.createnewfile(); filewriter file = new filewriter(name); file.write("text"); file.flush(); file.close();
Comments
Post a Comment