连续写入android studio中的文件

时间:2016-10-21 17:10:32

标签: java android android-studio

我正在尝试打开.txt文件,并使用scheduleFixedRate每10毫秒连续写入文件。

执行文件时,我收到一条消息,显示在catch(调试时):

  

java.io.FileNotFoundException:/storage/emulated/0/test.txt(权限被拒绝)。

我添加了在外部存储器上写入的权限。没有创建文件。

outputstream = new FileOutputStream ( file1 );之后执行尝试时,控件显示上面的消息。

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    Button bt1;
    TextView text;
    Timer timer = new Timer ();
    File file1 = new File ( Environment.getExternalStorageDirectory (), "Test.txt" );


    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate ( savedInstanceState );
        setContentView ( R.layout.activity_main );
        final Button bt1 = (Button) findViewById ( R.id.button );
        final TextView text = (TextView) findViewById ( R.id.textView );
        assert bt1 != null;
        bt1.setText ( "Start" );
        bt1.setOnClickListener ( new View.OnClickListener () {
            @Override
            public void onClick (View v) {
                if (bt1.getText ().equals ( "Stop" )) {
                    bt1.setText ( "Start" );
                } else {
                    bt1.setText ( "Stop" );
                    timer.scheduleAtFixedRate ( task, 10, 10 );
                }
            }
        } );
    }

    TimerTask task = new TimerTask () {
        @Override
        public void run () {
            writetime ();

        }
    };

    public void writetime () {
        FileOutputStream outputstream;
        try {

            outputstream = new FileOutputStream ( file1 );
            OutputStreamWriter oswriter = new OutputStreamWriter ( outputstream );
            BufferedWriter bwriter = new BufferedWriter ( oswriter );
            bwriter.append ( "Repetition" );
            bwriter.newLine ();

            bwriter.close ();
            outputstream.close ();

        } catch (IOException e) {
            e.printStackTrace ();
        }
    }

}

manifest.xml文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.kolla.usingtimer">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,因为我使用android marshmallow清单权限是不够的。与此同时,我们需要在手机中安装应用后为设备提供存储权限,或者我们可以使用run time permission。它工作,现在我可以创建该文件。