gpt4 book ai didi

android - 创建文本文件 Android IO

转载 作者:行者123 更新时间:2023-11-29 00:41:34 26 4
gpt4 key购买 nike

我正在尝试在 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 查找文件,但未创建文件。

最佳答案

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

确保您有:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" / > 也在您的 list 中。

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


}
}

关于android - 创建文本文件 Android IO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9011692/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com