在我的应用程序中,我正在尝试编写一个文件,然后我可以在内部阅读。
有人会举例说明如何将文件写入app的安装文件夹吗?
答案 0 :(得分:0)
使用以下代码从Edittext
写入文件并从文件读取到Edittext
将以下权限设置为AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
JAVA CODE
EditText LoadedText;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadedText=(EditText) findViewById(R.id.LoadedText);
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File("/sdcard/RouterSetup.txt");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}
LoadedText.setText(text.toString());
}
public void saveFile() {
try {
File myFile = new File("/sdcard/RouterSetup.txt");
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(LoadedText.getText());
myOutWriter.close();
fOut.close();
Toast.makeText(getBaseContext(),
"Done writing SD " + LoadedText.getText() + ".txt",
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}
getFilesDir()vs Environment.getDataDirectory() 公共文件getFilesDir()
返回文件系统上存储用openFileOutput(String, int)
创建的文件的目录的绝对路径。
public static File getExternalStorageDirectory()
返回主外部存储目录。如果用户已在其计算机上安装此目录,已从设备中删除或发生其他一些问题,则该目录当前可能无法访问。您可以使用getExternalStorageState()
确定其当前状态。
如果您想要使用getFilesDir()
获取应用程序路径,则会为您提供路径/data/data/your package/files
您可以使用
使用数据/包的Environment var获取路径 getExternalFilesDir(Environment.getDataDirectory().getAbsolutePath()).getAbsolutePath();
将返回外部存储根目录中的路径/storage/sdcard/Android/data/your pacakge/files/data
要访问外部资源,您必须在清单中提供WRITE_EXTERNAL_STORAGE
和READ_EXTERNAL_STORAGE
的权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
最简单的解决方案是
String filename = "stuff.xml";
<强> READ 强>
FileInputStream stream = ctx.openFileInput(filename);
(以下是解析该文件的一个非常简单的例子)
BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = buffReader.readLine()) != null) {
...
// do something cool
...
}
} catch (IOException e) {
Log.e("MyApp", "SCREAAAAAM", e);
}
<强> WRITE 强>
OutputStreamWriter outputStreamWriter = new OutputStreamWriter( ctx.openFileOutput(filename, Context.MODE_PRIVATE) );
outputStreamWriter.write("this is text not xml :(");
因此,需要注意的两个主要事项是openFileInput
的{{1}}和openFileOutput
方法(即您的活动),它们负责繁重的工作
答案 2 :(得分:-1)
您在谈论哪种类型的文件? 对于数据存储,您可以使用shared preferences。 对于媒体类型,您可以使用media storage。
答案 3 :(得分:-1)
//here i am downloading file from server and writing
String download_link = "some server location";
URL url = new URL(download_link);
HttpsURLConnection c = null;
if (url.getProtocol().toLowerCase().equals("https")) {
trustAllHosts();
c = (HttpsURLConnection) url.openConnection();
c.setHostnameVerifier(DO_NOT_VERIFY);
}
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = Environment.getExternalStorageDirectory() + "/download/";
File file = new File(PATH);
file.mkdirs();
File outputFile = new File(file, "Mobi.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
you need to add permission in manifest file:
uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>