创建文本文件 Android IO

发布于 2024-12-29 01:16:00 字数 1956 浏览 2 评论 0 原文

我正在尝试在 Android 手机上创建一个文本文件,并能够在需要时填充它。显然,我遇到了问题,并且会看看是否有人可以看看我到目前为止所拥有的内容。

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ViewLog extends Activity {

    private TextView tv;
    private static final String TAG = "MEDIA";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        CreateExternalLogFile("I am adding something into the text file!");
    }

private void CreateExternalLogFile(String s){
    File root = android.os.Environment.getExternalStorageDirectory(); 
    tv.append("\nExternal file system root: "+root);
    File dir = new File (root.getAbsolutePath() + "/logs");
    dir.mkdirs(); 
    File file = new File(dir,"log.txt");
    try {
        FileOutputStream f = new FileOutputStream(file,true); //True = Append to file, false = Overwrite
        PrintWriter pw = new PrintWriter(f);
        System.out.println(s);
        pw.println(s);
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                        " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\nFile written to \n"+file);


}
}

任何帮助将不胜感激。我已经使用 adb 来查找该文件,但该文件并未被创建。

I am trying to make a text file on an android phone and be able to populate it when needed. I am having problems, obviously, and would see if anyone can take a look at what I have so far.

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class ViewLog extends Activity {

    private TextView tv;
    private static final String TAG = "MEDIA";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        CreateExternalLogFile("I am adding something into the text file!");
    }

private void CreateExternalLogFile(String s){
    File root = android.os.Environment.getExternalStorageDirectory(); 
    tv.append("\nExternal file system root: "+root);
    File dir = new File (root.getAbsolutePath() + "/logs");
    dir.mkdirs(); 
    File file = new File(dir,"log.txt");
    try {
        FileOutputStream f = new FileOutputStream(file,true); //True = Append to file, false = Overwrite
        PrintWriter pw = new PrintWriter(f);
        System.out.println(s);
        pw.println(s);
        pw.flush();
        pw.close();
        f.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                        " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\nFile written to \n"+file);


}
}

Any help would be appreciated. I have used adb to look for the file, but the file is not being created.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠佳期 2025-01-05 01:16:00

编辑:这有效 - 刚刚测试过。

确保您拥有: >也在你的清单中。

 package com.example.fileio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class FilioActivity extends Activity {

    private TextView tv;
    private static final String TAG = "MEDIA";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        CreateExternalLogFile("I am adding something into the text file!");
    }

private void CreateExternalLogFile(String s){
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "filename.txt");
    tv.append("\nExternal file system root: "+ dir);
    try {
        FileOutputStream f = new FileOutputStream(file,false); //True = Append to file, false = Overwrite
        PrintStream p = new PrintStream(f);
        p.print(s);
        p.close();
        f.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                        " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\nFile written to \n"+file);


}
}

Edit: This works - Just tested it.

Make sure you have: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your manifest as well.

 package com.example.fileio;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.TextView;

public class FilioActivity extends Activity {

    private TextView tv;
    private static final String TAG = "MEDIA";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        tv = (TextView) findViewById(R.id.TextView01);

        CreateExternalLogFile("I am adding something into the text file!");
    }

private void CreateExternalLogFile(String s){
    File sdCard = Environment.getExternalStorageDirectory();
    File dir = new File (sdCard.getAbsolutePath());
    dir.mkdirs();
    File file = new File(dir, "filename.txt");
    tv.append("\nExternal file system root: "+ dir);
    try {
        FileOutputStream f = new FileOutputStream(file,false); //True = Append to file, false = Overwrite
        PrintStream p = new PrintStream(f);
        p.print(s);
        p.close();
        f.close();


    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.i(TAG, "******* File not found. Did you" +
                        " add a WRITE_EXTERNAL_STORAGE permission to the manifest?");
    } catch (IOException e) {
        e.printStackTrace();
    }   
    tv.append("\nFile written to \n"+file);


}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文