首先,我是英语的废话,我希望你能理解一切。 我正在尝试创建一个创建简单文件的应用程序" lol.txt"其中包含" hello world!"。我有点像菜鸟,我试着在互联网上关注tutos。
问题是,我成功到达了调试行"创建了文件?"但我找不到我的应用程序的文件夹,也找不到我创建的.txt文件。
我的代码是错误的吗?当我使用eclipse将其上传到手机上时,我无法找到我应用的任何文件夹。
MainActivity.java
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) {
}
}
activity_main.xml中
<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>
如果你有任何想法,比如默认文件夹(我搜索&#34; android / data / com.example。(...)/&#34;)或者我的代码中可能有错误?
非常感谢你。我帮助你。
答案 0 :(得分:0)
我之所以这样做是因为其他原因,但是通过分享这个,我希望你可以自己试一试并尝试一下......我希望它能奏效。根据您的要求进行修改。
我正在显示一个示例日志file.txt,以便在SD卡中创建并读取该文件。这是代码。
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" />
快乐的编码......