Internal Storage:
package file.com.kumar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FileActivity extends Activity {
Button write,read;
EditText edit;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
write=(Button)findViewById(R.id.Button01);
read=(Button)findViewById(R.id.Button02);
edit=(EditText)findViewById(R.id.EditText01);
tv=(TextView)findViewById(R.id.TextView01);
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
writetofile(edit.getText().toString());
}
});
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
readtofile(tv);
}
});
}
public void writetofile(String write){
String path=Environment.getDataDirectory().getAbsolutePath();
path=path +"/data/file.com.kumar/newfile";
File f=new File(path);
if(!f.exists()){
f.mkdir();
}
path=path +"/filetext.txt";
File myfile=new File(path);
FileWriter writer;
try {
writer = new FileWriter(myfile);
writer.write(write);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readtofile(TextView tv){
String path=Environment.getDataDirectory().getAbsolutePath();
path=path +"/data/file.com.kumar/newfile/filetext.txt";
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(new File(path)), "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
tv.setText(text.toString());
}
File storage path:
Output:
package file.com.kumar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FileActivity extends Activity {
Button write,read;
EditText edit;
TextView tv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
write=(Button)findViewById(R.id.Button01);
read=(Button)findViewById(R.id.Button02);
edit=(EditText)findViewById(R.id.EditText01);
tv=(TextView)findViewById(R.id.TextView01);
write.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
writetofile(edit.getText().toString());
}
});
read.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
readtofile(tv);
}
});
}
public void writetofile(String write){
String path=Environment.getDataDirectory().getAbsolutePath();
path=path +"/data/file.com.kumar/newfile";
File f=new File(path);
if(!f.exists()){
f.mkdir();
}
path=path +"/filetext.txt";
File myfile=new File(path);
FileWriter writer;
try {
writer = new FileWriter(myfile);
writer.write(write);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void readtofile(TextView tv){
String path=Environment.getDataDirectory().getAbsolutePath();
path=path +"/data/file.com.kumar/newfile/filetext.txt";
StringBuilder text = new StringBuilder();
String NL = System.getProperty("line.separator");
Scanner scanner = null;
try {
scanner = new Scanner(new FileInputStream(new File(path)), "UTF-8");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (scanner.hasNextLine()){
text.append(scanner.nextLine() + NL);
}
}
finally{
scanner.close();
}
}
}
File storage path:
Output:
No comments:
Post a Comment