0

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 :

  1. Code to write into file ( file to be saved because will be used until the app will be installed )
  2. Code to read that file line by line ( so to be added in array, odd lines in one array, other lines in another array )
  3. 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 )
  4. To add lines on list-activity

Any help will be very very appreciated, Thanks...

1
  • This question seems almost as if you're asking us to do your work instead of you. Commented Dec 7, 2014 at 23:29

1 Answer 1

1

First of all, you should use .getFilesDir().getPath() on your app's context, instead of hardcoding the path. That's commented in your first block. Second, create an OutputStream like this:

OutputStream out = new FileOutputStream(filePath); 

If you have an InputStream called in, you'll be able to write it to a file using this code:

byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) out.write(buf, 0, len); in.close(); out.close(); 

When you do create a file, check the rest (I didn't look) and get back to StackOverlow, if it fails. Don't make any of us do all the work, okay? Rip it to small part and make an effort.

Good luck with your work.

Sign up to request clarification or add additional context in comments.

3 Comments

but with getFilesDir it getin alot of files not just one.. just my file
@Web.11: Yes. That's the path to your app's folder. And then you add + "filename.txt" to get this file you want.
@Web.11: You're welcome. If you face troubles with the rest of the code (I guess now it could only be reading), I suggest you make one or maybe even two "smaller" questions. Altogether, this seemed a bit intimidating. ;-) Seriously, I'm glad we found a solution that works for you.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.