import java.io.*;
import android.app.*;
import android.os.*;
import android.widget.*;
public class ExternalStorageTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
String state = Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED))
{
textView.setText("There is no SD card.");
}
else
{
File externalDir = Environment.getExternalStorageDirectory();
File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt");
try
{
writeTextFile(textFile, "This is a test. Roger");
String text = readTextFile(textFile);
textView.setText(text);
if(!textFile.delete())
{
textView.setText("Couldn't remove temporary file");
}
}
catch(IOException e)
{
textView.setText("something went wrong! " + e.getMessage());
}
}
}
private String readTextFile(File file) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder text = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
text.append(line);
text.append("\n");
}
reader.close();
return text.toString();
}
private void writeTextFile(File file, String text) throws IOException
{
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
}
import android.app.*;
import android.os.*;
import android.widget.*;
public class ExternalStorageTest extends Activity
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
TextView textView = new TextView(this);
setContentView(textView);
String state = Environment.getExternalStorageState();
if(!state.equals(Environment.MEDIA_MOUNTED))
{
textView.setText("There is no SD card.");
}
else
{
File externalDir = Environment.getExternalStorageDirectory();
File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt");
try
{
writeTextFile(textFile, "This is a test. Roger");
String text = readTextFile(textFile);
textView.setText(text);
if(!textFile.delete())
{
textView.setText("Couldn't remove temporary file");
}
}
catch(IOException e)
{
textView.setText("something went wrong! " + e.getMessage());
}
}
}
private String readTextFile(File file) throws IOException
{
BufferedReader reader = new BufferedReader(new FileReader(file));
StringBuilder text = new StringBuilder();
String line;
while((line = reader.readLine()) != null)
{
text.append(line);
text.append("\n");
}
reader.close();
return text.toString();
}
private void writeTextFile(File file, String text) throws IOException
{
BufferedWriter writer = new BufferedWriter(new FileWriter(file));
writer.write(text);
writer.close();
}
}
'코드조각모음' 카테고리의 다른 글
[android009] 배경음을 위한 MediaPlayer 예제 (1) | 2011.11.06 |
---|---|
[android008] 효과음을 위한 SoundPool 예제 (0) | 2011.11.06 |
[android006] 어셋(Asset) 예제 (0) | 2011.11.06 |
[andorid005] 가속도계 예제 (0) | 2011.11.06 |
[android004] 키입력 예제 (0) | 2011.11.06 |