Java 我找不到通过android应用程序创建的.txt文件
首先,我对英语一窍不通,我希望你能理解一切。 我正在尝试创建一个应用程序,创建一个简单的文件lol.txt,其中包含hello world!。我有点笨,我试着在网上跟着图托斯舞 问题是,我成功地到达了创建的调试行文件?但我找不到我的应用程序文件夹,也找不到我创建的.txt文件 我的代码错了吗?当我用eclipse将我的应用程序上传到手机上时,我找不到它的任何文件夹 MainActivity.java activity_main.xml 如果你有任何想法,比如我在android/data/com.example上搜索的默认文件夹..../或者我的代码中有错误Java 我找不到通过android应用程序创建的.txt文件,java,android,xml,eclipse,Java,Android,Xml,Eclipse,首先,我对英语一窍不通,我希望你能理解一切。 我正在尝试创建一个应用程序,创建一个简单的文件lol.txt,其中包含hello world!。我有点笨,我试着在网上跟着图托斯舞 问题是,我成功地到达了创建的调试行文件?但我找不到我的应用程序文件夹,也找不到我创建的.txt文件 我的代码错了吗?当我用eclipse将我的应用程序上传到手机上时,我找不到它的任何文件夹 MainActivity.java activity_main.xml 如果你有任何想法,比如我在android/data/com.
多谢各位。我感谢您的帮助。我之所以这样做是因为其他原因,但通过分享,我希望您可以调整它并自己尝试一下。。。我希望它能起作用。根据您的要求进行修改 我显示了一个在sd卡中创建的示例日志文件.txt,并读取该文件。这是代码 主要活动: MyLog: xml在这里: 不要忘记给予许可:
快乐编码…openFileOutput将在内部存储器中创建文件。用户在拥有根目录手机之前无法查看该存储。如果你想知道位置,那么读一下我读了,我的手机是root,但我仍然无法在data/data/…getFilesDir.getAbsolutePath中找到文件夹;将返回内部存储目录路径。也用于验证代码。尝试从文件中读取,并检查是否得到相同的字符串作为返回。您可以使用openFileInputfileName;获取输入流。谢谢Patel!我很欣赏你的提示。有用的代码片段,我希望我在编辑你的解决方案时没有太过侵入性-在我看来这是一个很好的方法!我没有SD卡,我将尝试编辑它,使其在内部存储中工作。我无法使其工作,我导入了你的工作;我有太多的错误,我不知道如何解决它们。谢谢你的帮助,伙计。我没有提到导入的文件@axelsyntes没关系,我在这里找到了解决方案:@Shadow
package com.example.brosselesdents;
import java.io.File;
import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener{
int compteur = 0;
TextView text1;
String fileName = "lol.txt";
FileOutputStream outputStream;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1 = (TextView)findViewById(R.id.textView1);
Button button1;
button1 = (Button)findViewById(R.id.button1);
final String string = "Hello world!";
button1.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v) {
try {
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
text1.setText("File created?");
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v) {
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.brosselesdentsbatards.MainActivity"
android:id="@+id/layout1">
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:text="Button" />
</RelativeLayout>
public class MainActivity extends Activity {
TextView tv;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.album_title);
b=(Button)findViewById(R.id.button1);
tv.setText("TESTING TO INSERT A LOG TXT IN SD CARD");
//I am just passing this text file in Log
String text = tv.getText().toString();
MyLog.i("Info", text);
MyLog.e("Error", text);
MyLog.d("Debug", text);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"a.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
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.textView1);
//Set the text
tv.setText(text);
}
});
}
}
public class MyLog {
public static void i(String TAG, String message){
// Printing the message to LogCat console
Log.i(TAG, message);
// Write the log message to the file
insertToSdCard(message);
}
public static void d(String TAG, String message){
Log.d(TAG, message);
insertToSdCard(message);
}
public static void e(String TAG, String message){
Log.e(TAG, message);
insertToSdCard(message);
}
// creating method to insert into sd card.
private static void insertToSdCard(String text) {
File logFile = new File("sdcard/a.txt");
if (!logFile.exists()) {
try {
logFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
}
}
try {
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
buf.append(text);
buf.newLine();
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dip">
<TextView
android:id="@+id/album_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/album_title"
android:layout_below="@+id/album_title"
android:layout_marginLeft="44dp"
android:layout_marginTop="129dp"
android:text="Read sd card" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="71dp"
android:text="Display text from sd card" />
</RelativeLayout>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />