package com.example.sadievrenseker.file_1;

import android.content.Context;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            FileOutputStream fos = openFileOutput("myfile.txt",Context.MODE_PRIVATE);
            String msg = "Mobile Programming";
            fos.write(msg.getBytes());
            fos.close();
        }catch(Exception e){
            e.printStackTrace();
        }
       try{
           FileInputStream fis = openFileInput("myfile.txt");
           InputStreamReader isr = new InputStreamReader(fis);
           //FileReader fr = new FileReader("myfile.txt");
           BufferedReader br = new BufferedReader(isr);
           TextView tv = (TextView) findViewById(R.id.msg);
           tv.setText(br.readLine());
           fis.close();
       }catch(Exception e){
           e.printStackTrace();
       }
        try{
            Resources r = getResources();
            InputStream is = r.openRawResource(R.raw.smith);
            Scanner s = new Scanner(is);
            TextView tv = (TextView) findViewById(R.id.msg);
            String text = "";
            while(s.hasNext()){
                text += " " + s.next();
            }
            tv.setText(text);
            is.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        Context c = getApplicationContext();
        TextView tv = (TextView) findViewById(R.id.msg);
        String str = c.getFilesDir().toString();
        str +="\n Total Space: "+ c.getFilesDir().getTotalSpace();
        str +="\n Free Space: "+ c.getFilesDir().getFreeSpace();
        tv.setText(str);
        File f = new File("myfile.txt");
        f.delete();



    }
}









