-1

I have to xml.file

first :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".Text1" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="36dp"
        android:text="Button1" />



</RelativeLayout>

second :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="20dp"
        android:layout_marginTop="28dp"
        android:text="TextView" />

</RelativeLayout> 

i have a txt.file in asset folder that its name is 12.txt i want when i press button1 in first xml.file i go to second xml.file and textview on it set with my txt.file in asset folder

code is :

public class Text2 extends Activity {
private TextView txt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text2);
        txt=(TextView)findViewById(R.id.textView1);


        FileInputStream fis=null;
        final StringBuffer buffer= new StringBuffer();

        try {
            fis = openFileInput("12.txt");
            DataInputStream dataIO = new DataInputStream(fis);
            String strLine = null;

            if ((strLine = dataIO.readLine()) != null) {
                buffer.append(strLine);
            }

            dataIO.close();
            fis.close();
        }
        catch  (Exception e) {  
        }
        txt.setText(buffer.toString());





    }





}

why it doesnt work ?? when i press button1 i go to second xml.file but textview is empty !!!

user3410344
  • 43
  • 13
  • 2
    Remove the try/catch block - or probably more applicable, log the exception. If there is an exception, you'll never know about it. – user2864740 Mar 29 '14 at 07:02
  • i'm very biginner .... can u explain it in more detail ? – user3410344 Mar 29 '14 at 07:06
  • `try { throw new Exception("Oops"); doImportantStuff() } catch (Exception e) {}` - `doImportantStuff` is never called, but we never find out because the exception was ignored! Now, I'm not saying that an exception *is* generated, but if it was, *you'd never know* (and could possibly end up with an empty StringBuffer). – user2864740 Mar 29 '14 at 07:07
  • so what should i do ? – user3410344 Mar 29 '14 at 07:11
  • Try to appropriately recover from the exception - and if you can't (or maybe even if you can), [log the exception](http://stackoverflow.com/questions/4341363/android-print-full-exception-backtrace-to-log). Be very careful about "blindly swallowing" exceptions. – user2864740 Mar 29 '14 at 07:15

5 Answers5

0

use like this code:

          FileInputStream fstream = new FileInputStream(Environment.getExternalStorageDirectory()+filenames));
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          while ((strLine = br.readLine()) != null)   
          {
              arrayOfarray.add(strLine);
          }
prakash
  • 1,428
  • 1
  • 21
  • 33
0

My best guess is that program's running directory is not what you think it is, and openFileInput() is throwing an IOException because it can't find 12.txt. Your catch-all-do-nothing block is probably hiding an error. You probably want to log exception and abort. If you don't care about exceptional conditions crashing your program, you can use the following cheap trick to get a backtrace:

catch(Exception e)
{
    throw(new RuntimeException(e));
}

When I'm writing code that's supposed to be part of a project with high standards (as opposed to a one-off experiment or something), or that someone else will see, I would make MyIOException which subclasses RuntimeException (possibly through intermediate MyRuntimeException) and has an IOException member, then when I catch an IOException I throw a wrapped MyIOException, which has the benefit of letting my caller use class-based exception dispatch.

picomancer
  • 1,706
  • 13
  • 15
0

Sorry Dude !! I have edited it again, now it will run perfectly!!

 public class Text2 extends Activity {
        private TextView txt;

            @Override
            protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.text2);
                txt=(TextView)findViewById(R.id.textView1);  
         String mLine ="";  
        try {
                BufferedReader reader = new BufferedReader(
                    new InputStreamReader(getAssets().open("12.txt")));

                // do reading, usually loop until end of file reading  
                mLine = reader.readLine();


                reader.close();
            } catch (IOException e) {
                //log the exception
            }
            txt.setText(mLine)

         }





        }
sandy
  • 201
  • 1
  • 7
0

Try this..

    txt=(TextView)findViewById(R.id.textView1);        

    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    try {
         inputStream = assetManager.open("12.txt");
         BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
         StringBuilder total = new StringBuilder();
         String line;
         while ((line = r.readLine()) != null) {
              total.append(line);
         }
         txt.setText(total.toString());
    }
    catch (IOException e){
         e.printStackTrace();
    }
Hariharan
  • 28,756
  • 7
  • 51
  • 55
0
Try this code. it works for me. so it will help you.

package com.example.answerquestion;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.PrintWriter;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        btn = (Button) findViewById(R.id.btn1);
        btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                File dir = new File(getFilesDir().getAbsolutePath());
                try{
                    PrintWriter out = new PrintWriter(new FileWriter(dir + "/test.txt"));
                    for (int i = 0; i < 20; i++) {
                        out.println("omg");
                    }
                    out.close();
                    File file = new File(getFilesDir().getAbsolutePath(), "/test.txt");
                    FileInputStream in = new FileInputStream(file);
                  //  FileInputStream fis=null;
                    final StringBuffer buffer= new StringBuffer();

                       // fis = openFileInput("12.txt");
                        DataInputStream dataIO = new DataInputStream(in);
                        String strLine = null;

                        if ((strLine = dataIO.readLine()) != null) {
                            buffer.append(strLine);
                        }

                        dataIO.close();
                        in.close();
                        System.out.print(buffer.toString());
                    }
                    catch  (Exception e) {  
                    }

                }

        });
    }

}
Sethu
  • 430
  • 2
  • 13