[英]How to save parsed text file in internal/external storage in android
Currently I am working in project where I need to parse a remote text file and store it in local storage (internal/external). 目前我在项目中工作,我需要解析远程文本文件并将其存储在本地存储(内部/外部)。 I am able to parse the text file but unable to store it in SDCARD. 我能够解析文本文件但无法将其存储在SDCARD中。 Here is my code : 这是我的代码:
package com.exercise.AndroidInternetTxt;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;
public class AndroidInternetTxt extends Activity {
TextView textMsg, textPrompt;
final String textSource = "http://sites.google.com/site/androidersite"
+ "/text.txt";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textPrompt = (TextView) findViewById(R.id.textprompt);
textMsg = (TextView) findViewById(R.id.textmsg);
textPrompt.setText("Wait...");
URL textUrl;
try {
textUrl = new URL(textSource);
BufferedReader bufferReader = new BufferedReader(
new InputStreamReader(textUrl.openStream()));
String StringBuffer;
String stringText = "";
while ((StringBuffer = bufferReader.readLine()) != null) {
stringText += StringBuffer;
}
bufferReader.close();
textMsg.setText(stringText);
// Saving the parsed data.........
File myFile = new File("sdcard/mysdfile.txt");
if (!myFile.exists()) {
myFile.mkdirs();
}
myFile = new File("sdcard/mysdfile.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
myOutWriter.append(textMsg.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(), "Done writing SD 'mysdfile.txt'",
Toast.LENGTH_SHORT).show();
} catch (MalformedURLException e) {
e.printStackTrace();
textMsg.setText(e.toString());
} catch (IOException e) {
e.printStackTrace();
textMsg.setText(e.toString());
}
textPrompt.setText("Finished!");
}
}
And here is my manifest file - 这是我的清单文件 -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.exercise.AndroidInternetTxt"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="4" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:label="@string/app_name">
<activity android:name=".AndroidInternetTxt"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
</manifest>
However, My Question is - 但是,我的问题是 -
Any code example is excellent for me. 任何代码示例对我来说都很好。
First thing, your AndroidManifest.xml
file is correct. 首先,您的AndroidManifest.xml
文件是正确的。 So no need to correct that. 所以没有必要纠正这一点。 Though I would recommend organizing your permissions in one place. 虽然我建议您在一个地方组织您的权限。
FileOutputStream fOut = openFileOutput("myinternalfile.txt",MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//---write the contents to the file---
osw.write(strFileData);
osw.flush();
osw.close();
//This will get the SD Card directory and create a folder named MyFiles in it.
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/MyFiles");
directory.mkdirs();
//Now create the file in the above directory and write the contents into it
File file = new File(directory, "mysdfile.txt");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
osw.write(strFileData);
osw.flush();
osw.close();
Note: If running on an emulator, ensure that you have enabled SD Card support in the AVD properties and allocated it appropriate space. 注意:如果在模拟器上运行,请确保已在AVD属性中启用了SD卡支持并为其分配了适当的空间。
Replace this lines 替换这一行
File myFile = new File("sdcard/mysdfile.txt");
if(!myFile.exists()){
myFile.mkdirs();
}
myFile = new File("sdcard/mysdfile.txt");
with 同
//Saving the parsed data......... //保存解析的数据.........
File myFile = new File(Environment.getExternalStorageDirectory()+"/mysdfile.txt");
myFile.createNewFile();
File myFile = new File("sdcard/mysdfile.txt");
应该改为
File myFile = new File("/mnt/sdcard/mysdfile.txt");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.