0

I am newbie in android development. Today for i was trying to display all my practiced programs of java in my application. I want the application to read the data written in .txt file.

  1. In which folder should I store all my programs? They are more than 100.
  2. I want to display the content of program 2 when I clicked the 2 on the list view or any other
  3. Can we store the text files in database? If so how can I access them ? How can I read them?
  4. Any basic ideas how can I solve this?
1

3 Answers 3

2

You can kept text file in raw / assets folder. To read them just use this code. From Assets:

BufferedReader reader = new BufferedReader( new InputStreamReader(getAssets().open("YourTextFile.txt"))); 

From Raw:

InputStream inputStream = context.getResources().openRawResource(R.id.yourresoureid); InputStreamReader inputreader = new InputStreamReader(inputStream) 

as you are a java programmer no need to tell how to read data from InputStream, if your really want then tell me I will post the rest of the code.

Saving that huge amount of data in data base is not a good idea.

Example to read data from InputStream

 BufferedInputStream bis=new BufferedInputStream(inputstream); ByteArrayBuffer baf=new ByteArrayBuffer(1000); while((k=bis.read())!=-1) { baf.append((byte)k); } String results=new String(baf.toByteArray()); 
Sign up to request clarification or add additional context in comments.

1 Comment

Actually i am c programmer. I have learnt only a basic of java. It would help me to understand the things if the rest of the code are here.
1
  1. Start with something easy and work up to the database option.
  2. Yes, the answer would be quite long, and I think a tutorial on SQLite would be a place to start on this. 2,1. Try putting your text files in the assets folder and reading them like this. This code reads a file, and dumps it line by line into the log.

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_read);

    AssetManager assetManager = getAssets(); try { BufferedReader br = new BufferedReader(new InputStreamReader( assetManager.open("hi.txt"))); // InputStream inputStream = assetManager.open("hi.txt"); // BufferedReader br = new BufferedReader( // new InputStreamReader(inputStream)); String lineIn; while ((lineIn = br.readLine()) != null) { Log.d("ReadTheDamnFile", lineIn); } assetManager.close(); } catch (IOException e) { } 

    }

Comments

0

try this its work fine :)

try { if(poslist==0) { in = this.getAssets().open("file1.txt"); iv.setBackgroundResource(R.drawable.fileimage1); } } catch (IOException e) { e.printStackTrace(); } try { reader = new BufferedReader(new InputStreamReader(in,"UTF-8")); } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } String line=""; String s =""; try { line = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } while (line != null) { s = s + line; s =s+"\n"; try { line = reader.readLine(); } catch (IOException e) { e.printStackTrace(); } } tv.setText(""+s); } public void onClick(View v){ try { line = reader.readLine(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (line != null){ tv.setText(line); } else { //you may want to close the file now since there's nothing more to be done here. } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.