Android 数据存取试验
2010-02-27 09:45:00 来源:WEB开发网核心提示: Step2、Java代码package com.penguin7.filetest; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutput
Step2、Java代码
package com.penguin7.filetest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FileTest extends Activity {
private final String FILE_PATH = "/data/data/com.penguin7.filetest/";
private final String FILE_NAME = "filetest.txt";
// Definition file
File file;
FileOutputStream out;
FileInputStream in;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// execute store action
Button btnStore = (Button) findViewById(R.id.btn_store);
btnStore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
// Create a file
file = new File(FILE_PATH, FILE_NAME);
file.createNewFile();
// Open output stream for the file
out = new FileOutputStream(file);
// Get the content and store into the file
EditText et = (EditText) findViewById(R.id.input);
String info = et.getText().toString();
// Transform the string to byte array then store into file
out.write(info.getBytes());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// Execute read action
Button btnExtract = (Button) findViewById(R.id.btn_extract);
btnExtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
// Create a file object to prepare read file's content
file = new File(FILE_PATH, FILE_NAME);
if (!file.exists()) {
return;
}
// Open input stream for file
in = new FileInputStream(file);
// Get length of file and apply to a byte array to store file's information
int length = (int) file.length();
byte[] temp = new byte[length];
in.read(temp, 0, length);
TextView tv = (TextView) findViewById(R.id.display);
// Before display the content we should transform to UTF-8 coding way
tv.setText(EncodingUtils.getString(temp, "UTF-8"));
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class FileTest extends Activity {
private final String FILE_PATH = "/data/data/com.penguin7.filetest/";
private final String FILE_NAME = "filetest.txt";
// Definition file
File file;
FileOutputStream out;
FileInputStream in;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// execute store action
Button btnStore = (Button) findViewById(R.id.btn_store);
btnStore.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
// Create a file
file = new File(FILE_PATH, FILE_NAME);
file.createNewFile();
// Open output stream for the file
out = new FileOutputStream(file);
// Get the content and store into the file
EditText et = (EditText) findViewById(R.id.input);
String info = et.getText().toString();
// Transform the string to byte array then store into file
out.write(info.getBytes());
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
// Execute read action
Button btnExtract = (Button) findViewById(R.id.btn_extract);
btnExtract.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
try {
// Create a file object to prepare read file's content
file = new File(FILE_PATH, FILE_NAME);
if (!file.exists()) {
return;
}
// Open input stream for file
in = new FileInputStream(file);
// Get length of file and apply to a byte array to store file's information
int length = (int) file.length();
byte[] temp = new byte[length];
in.read(temp, 0, length);
TextView tv = (TextView) findViewById(R.id.display);
// Before display the content we should transform to UTF-8 coding way
tv.setText(EncodingUtils.getString(temp, "UTF-8"));
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}
[]
更多精彩
赞助商链接