I need when app starts, to check if file exists, if not to be created.. I need a block of code to append files into it than I need a block of code that read that text line by line than to remove a line ....
I found this code at stackoverflow, and they said that the file will be created in that location...
//Here I have this : //Do not hardcode "/data/"; use Context.getFilesDir().getPath() instead // String filePath = "/data/data/com.example.myapp/files/text.txt"; File file = new File(filePath); if(file.exists()){ //Do nothing } else{ try { final String TESTSTRING = new String(""); FileOutputStream fOut = openFileOutput("text.txt", MODE_WORLD_READABLE); OutputStreamWriter osw = new OutputStreamWriter(fOut); osw.write(TESTSTRING); osw.flush(); osw.close(); } catch (IOException ioe) {ioe.printStackTrace();} } } To add Lines in text I made this :
private void write(){ S ="/data/data/com.example.myapp/files/text.txt"; try { writer = new FileWriter(S, true); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { writer.write(emri.getText().toString() + "\n" + link.getText().toString()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } And when I have to read them :
public class PlayList extends ListActivity { ArrayList<String> listaE = new ArrayList<String>(); ArrayList<String> listaL = new ArrayList<String>(); InputStream instream; int resh=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); lexo(); String[] mStringArray = new String[listaE.size()]; mStringArray = listaE.toArray(mStringArray); setListAdapter(new ArrayAdapter<String>(PlayList.this,android.R.layout.simple_list_item_1,mStringArray)); } private void lexo(){ String S ="/data/data/com.example.myapp/files/text.txt"; try { // open the file for reading instream = new FileInputStream(S); // if file the available for reading if (instream != null) { // prepare the file for reading InputStreamReader inputreader = new InputStreamReader(instream); BufferedReader buffreader = new BufferedReader(inputreader); String line; // read every line of the file into the line-variable, on line at the time do { line = buffreader.readLine(); if ((resh % 2) == 0) { listaL.add(line); } else { listaE.add(line); } // do something with the line } while (line != null); } } catch (Exception ex) { // print stack trace. } finally { // close the file. try { instream.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } My code does not work at all, and is missing the code to remove a line.. So everything I need is :
- Code to write into file ( file to be saved because will be used until the app will be installed )
- Code to read that file line by line ( so to be added in array, odd lines in one array, other lines in another array )
- Code to remove a line from that file ( array to be added in listview and when user touches the line, touched line to be removed )
- To add lines on list-activity
Any help will be very very appreciated, Thanks...