2014. 7. 8. 19:28ㆍ안드로이드 개발
<파일 쓰기>
try
{
File root = Environment.getExternalStorageDirectory();
if (root.canWrite())
{
File file = new File("/sdcard/sample.dat");
FileOutputStream fileStream = new FileOutputStream(file);
DataOutputStream dataStream = new DataOutputStream(fileStream);
try
{
dataStream.writeUTF("STRING");
dataStream.writeInt(100);
} catch(IOException e)
{
Log.e("error", e.toString());
} finally
{
fileStream.close();
}
}
} catch(IOException e)
{
Log.e("error", e.toString());
}
<파일 읽기>
String filename = "/sdcard/sample.dat";
File file = new File(filename);
if (file.isFile()) {
try
{
FileInputStream fileStream = new FileInputStream(file);
DataInputStream dataStream = new DataInputStream(fileStream);
try
{
String strData = dataStream.readUTF();
int nData = dataStream.readInt();
} catch(IOException e)
{
Log.e("error", e.toString());
} finally
{
fileStream.close();
}
} catch(IOException e)
{
Log.e("error", e.toString());
}
}