发表评论
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。
1.需要引用的空间:
import android.content.Context;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
2.相关的代码
private void writeFileToSD() {
String sdStatus = Environment.getExternalStorageState();
if(!sdStatus.equals(Environment.MEDIA_MOUNTED)) {
Log.d("TestFile", "SD card is not avaiable/writeable right now.");
return;
}
try {
String pathName="/sdcard/";
String fileName="config.txt";
File path = new File(pathName);
File file = new File(pathName + fileName);
if( !path.exists()) {
Log.d("TestFile", "Create the path:" + pathName);
path.mkdir();
}
if( !file.exists()) {
Log.d("TestFile", "Create the file:" + fileName);
file.createNewFile();
}
FileOutputStream stream = new FileOutputStream(file);
String s = "AudioLocalIP=192.168.0.231:8005\r\nAudioRemoteIP=192.168.0.230:8000\r\nAudioCodecName=\"G.711 A-law\"";
byte[] buf = s.getBytes();
stream.write(buf);
stream.close();
} catch(Exception e) {
Log.e("TestFile", "Error on writeFilToSD.");
e.printStackTrace();
}
}
private void writeFile() {
try {
FileOutputStream stream = openFileOutput("config.txt", Context.MODE_WORLD_WRITEABLE);
String s = "AudioLocalIP=192.168.0.230:8000\r\nAudioRemoteIP=192.168.0.231:8000\r\nAudioCodecName=\"G.711 A-law\"";
byte[] buf = s.getBytes();
stream.write(buf);
stream.close();
Log.d("TestFile", s);
}
catch (FileNotFoundException e) {
Log.d("TestFile", "File not found.");
}
catch (IOException e) {
Log.d("TestFile", "File write error.");
}
}
3.需要的权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。