问题是写入outputstreamswriter的字符串具有正确的值但是在onclick()方法中的try / catch块中,对话框输出流写入器的方法不起作用。我的文件名是default.txt我确定文件名与问题无关。 outputstreamwriter不会改变文件的内容,无论我尝试过bufferwriter还是fileoutputstream,但似乎没有任何工作。请帮助我从几天开始失去理智!
这里是代码。
package zafus.personalitymeter;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import android.R.integer;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout.LayoutParams;
public class ChoseAnswers extends Activity {
private final String TAG="";
public String vale;
public List<String> obj;
public int numOfAnswers=0;
//public RelativeLayout mylayout =(RelativeLayout)findViewById(R.id.calayout);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chose_answers);
obj=null;
obj=readFromFile("answer.txt", this);
numOfAnswers=Integer.valueOf(obj.get(0).toString());
int i=1;
LinearLayout mylayout =(LinearLayout)findViewById(R.id.linearLayout1);
while(i<=numOfAnswers){
Button tt = new Button(this);
tt.setText(obj.get(i));
tt.setId(i+1);
tt.setPadding(5, 10, 5, 10);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
tt.setOnClickListener( new OnClickListener() {
@Override
public void onClick(View v) {
vale=((Button)v).getText().toString();
AlertDialog.Builder dlgAlert1 = new AlertDialog.Builder(ChoseAnswers.this);
dlgAlert1.setMessage("Show this answer as a result of next test?");
dlgAlert1.setPositiveButton("Yes",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
obj=null;
obj=readFromFile("default.txt", ChoseAnswers.this);
obj.set(1,vale);
String receiveString = "";
int num=Integer.valueOf(obj.get(0))+2;
StringBuilder ab = new StringBuilder();
int i=0;
for(;i<num;i++){
ab.append(obj.get(i)+"\n");
}
receiveString=ab.toString();
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(openFileOutput("default.txt", Context.MODE_PRIVATE));
//outputStreamWriter.write("");
outputStreamWriter.write(receiveString);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
}
ChoseAnswers.this.finish();
}});
dlgAlert1.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}});
dlgAlert1.show();
}
});
if (i == 1){
params.setMargins(0, 150, 0, 20);
}
else{
params.setMargins(0, 20, 0, 20);
}
tt.setLayoutParams(params);
tt.setBackgroundResource(R.drawable.bxml);
tt.setTextColor(Color.parseColor("#330011"));
mylayout.addView(tt);
i++;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.choseanswers, menu);
return true;
}
public static List<String> readFromFile(String fileName, Context context) {
List<String> words = new ArrayList<String>();
InputStream fIn = null;
InputStreamReader isr = null;
BufferedReader input = null;
try {
fIn = context.getResources().getAssets().open(fileName, context.MODE_WORLD_READABLE);
isr = new InputStreamReader(fIn);
input = new BufferedReader(isr);
String line = "";
while ((line = input.readLine()) != null) {
words.add(line);
}
} catch (Exception e) {
e.getMessage();
} finally {
try {
if (isr != null)
isr.close();
if (fIn != null)
fIn.close();
if (input != null)
input.close();
} catch (Exception e2) {
e2.getMessage();
}
}
return words;
} }
答案 0 :(得分:0)
在onClick中,您从资源中读取文件并将其保存到内部存储器中。 在下一次单击时,同样的情况发生。 如果要检查写入是否成功,也应该从内部存储器中读取文件。