在android中,我能够从文本文件中读取一个字符串并将其打印出来,但是我无法将其存储在变量中以便进一步处理
Android代码:
try {
FileInputStream fileIn=openFileInput("mytextfile.txt");
InputStreamReader InputRead= new InputStreamReader(fileIn);
char[] inputBuffer= new char[READ_BLOCK_SIZE];
String s = String.valueOf(inputBuffer[0]);
int charRead;
while ((charRead=InputRead.read(inputBuffer))>0) {
String readstring=String.copyValueOf(inputBuffer,0,charRead);
s +=readstring;
}
InputRead.close();
Toast.makeText(getBaseContext(), s,Toast.LENGTH_SHORT).show();
String a = s;
StringRequest request = new StringRequest(Request.Method.POST, "http://"+a+"/try.php",
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
HashMap<String, String> map = new HashMap<>();
map.put("encoded_string", encoded_string);
map.put("image_name","IMG_Hello.jpg");
return map;
}
};
String a
应包含存储在 mytextfile.txt
任何人都可以给我任何想法吗?因为目前在上面的代码中,String a
无法存储 mytextfile.txt
答案 0 :(得分:-1)
试试这段代码..
private void writeToFile(String data,Context context) {
try {
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(context.openFileOutput("mytextfile.txt", Context.MODE_PRIVATE));
outputStreamWriter.write(data);
outputStreamWriter.close();
}
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());
}
}
上面的方法传递你的值,它将写成定义文件。 如果你想添加一个只传递
writeToFile(a)// pass string value.
我希望你在android清单文件中添加读写权限,如下所示..
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>