Crazy Programmer
Crazy Programmer

Reputation: 105

Convert Text File to String in java

i have a written a code to send a text file by converting it into string and sending it into a web service. Please can someone tell me other available methods for sending the string as stream to Web Service.

public class MainActivity extends Activity {
    Button b1;
    String s;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        b1=(Button)findViewById(R.id.button1);
        b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                File upfile=new File("sdcard/text/testfile.txt");
                try {
                    FileInputStream fin=new FileInputStream(upfile);
                    byte[] buffer= new byte[(int)upfile.length()];
                    new DataInputStream(fin).readFully(buffer);
                    fin.close();
                    s=new String(buffer,"UTF-8");
                    System.out.print(buffer);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                // TODO Auto-generated method stub
                Toast.makeText(MainActivity.this, s, 20).show();

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}

Upvotes: 2

Views: 6601

Answers (2)

Dipak Keshariya
Dipak Keshariya

Reputation: 22291

try this.

public void mReadJsonData() {
    try {
        File f = new File("sdcard/text/testfile.txt");
        FileInputStream is = new FileInputStream(f);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        String text = new String(buffer);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 4

Hulk
Hulk

Reputation: 2561

Use this code to read the data from file and get it into string and do your process ahead according to your need--

File upfile=new File("sdcard/text/testfile.txt");
 try {
            final BufferedReader reader = new BufferedReader(new FileReader(upfile));
            String encoded = "";
            try {
                String line;
                while ((line = reader.readLine()) != null) {
                    encoded += line;
                }
            }
            finally {
                reader.close();
            }
                 System.out.print(encoded);
        }
        catch (final Exception e) {

        }

Upvotes: 0

Related Questions