如何保存解析的文本文件中的内部/外部存储在机器人 [英] How to save parsed text file in internal/external storage in android

查看:144
本文介绍了如何保存解析的文本文件中的内部/外部存储在机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我的项目我的工作,我需要解析远程文本文件,并将其存储在本地存储(内部/外部)。我能够分析文本文件,但无法将其存储在SD卡。这是我的code:

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. 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!");
    }
}

和这里是我的manifest文件 -

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 -

  • 如何存放解析的文本数据在SD卡或手机内存中的文件?
  • 还有什么办法用计时器来检查与pre定义的时间间隔的网址是什么?

任何code的例子是非常适合我。

Any code example is excellent for me.

推荐答案

第一件事情,你的的Andr​​oidManifest.xml 文件是正确的。因此,没有必要纠正。虽然我会建议组织你的权限,在同一个地方。

First thing, your AndroidManifest.xml file is correct. 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();

编写文件到外部SD卡

//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();

注意:如果在仿真器上运行,确保已启用SD卡的支持,在AVD的属性和它分配适当的空间

这篇关于如何保存解析的文本文件中的内部/外部存储在机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆