我有一个需要经验丰富的Java专业人士的应用程序。引擎工作得很完美,但我无法将数据保存到文本文件中;此外,每次调试文件操作时,我都必须拴好我的平板电脑,卸载旧的应用程序,复制apk并重新安装,因为我无法在模拟器中写入磁盘。
我补充说:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
到应用程序标记外部的清单中,并尝试了许多写入文件的方法。异常是(取决于当前的尝试)访问或文件不存在。我还在谷歌上搜索了一整天来寻找解决方案。有人知道吗?
// this is a canned method that should presumably work
private void writeStringToTextFile(String s, String f){
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath());
dir.mkdirs();
File file = new File(dir, f);
try{
FileOutputStream f1 = new FileOutputStream(file,false); //True = Append to file, false = Overwrite
PrintStream p = new PrintStream(f1);
p.print(s);
p.close();
f1.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
} }
// this is the save method
// it has been modified a little less than a million times today
// some lines may be unnecessary because I have tried several approaches
public void saveSystem(View view){
String s="";
String FILENAME = "data/data/TopExpertAndroid/" + fileName;
FileOutputStream f;
File file = new File(FILENAME);
if(!(file.exists())){
try{
file.createNewFile();}
catch(Exception e){
showSplash(e.getMessage() + e.getCause(), "OK");
return;}}
try{
getFile();
if(fileName.equals("")){}
else{
f = openFileOutput(FILENAME, Context.MODE_PRIVATE);
for(int temp=0; temp<50; temp++){
for(int loop=0; loop<50; loop++){
s += topExpert.nodeBank[temp][loop].activationResponse + "n";
s += topExpert.nodeBank[temp][loop].display + "n";
s += topExpert.nodeBank[temp][loop].falseLink + "n";
s += topExpert.nodeBank[temp][loop].identifier + "n";
if(topExpert.nodeBank[temp][loop].isTop){
s += "true";}
else{
s += "false";}
s += "n" + topExpert.nodeBank[temp][loop].trueLink + "n";}}
writeStringToTextFile(s,FILENAME);
}}
catch(Exception e){
showSplash(e.getMessage() + e.getCause(), "OK");}}
FILENAME
变量可能在开始时缺少/
:
String FILENAME = "/data/data/TopExpertAndroid/" + fileName;
你可能应该使用getDataDirectory或getExternalStorageDirectory来代替…
但是你说的不能写到模拟器的磁盘似乎很奇怪…?