Я следил за учебником, чтобы создать этот код, после поиска и поиска файла text.txt я не могу найти его нигде! Я что-то упускаю? Как я могу изменить, где хранится файл?Где находится этот файл "text.txt"? Как я могу изменить, где файл сохраняется и просматривать содержимое?
Это 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=".MainActivity" >
<EditText
android:id="@+id/msg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Enter a message"
>
<requestFocus />
</EditText>
<Button
android:id="@+id/LOAD"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/SAVE"
android:layout_below="@+id/SAVE"
android:layout_marginTop="46dp"
android:text="LOAD DATA" />
<Button
android:id="@+id/SAVE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/msg"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="SAVE DATA" />
</RelativeLayout>
Это является MainActivity.java
package com.example.conor.filedemo;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.io.*;
import java.util.*;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
Button save,load;
EditText message;
String Message;
int data_block = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
save=(Button) findViewById(R.id.SAVE);
load = (Button) findViewById(R.id.LOAD);
message = (EditText) findViewById(R.id.msg);
save.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Message = message.getText().toString();
try {
FileOutputStream fou = openFileOutput("text.txt", MODE_WORLD_READABLE);
OutputStreamWriter osw = new OutputStreamWriter(fou);
try {
osw.write(Message);
osw.flush();
osw.close();
Toast.makeText(getBaseContext(), "Data saved", Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
load.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
FileInputStream fis = openFileInput("text.txt");
InputStreamReader isr = new InputStreamReader(fis);
char[] data = new char[data_block];
String final_data="";
int size;
try {
while((size = isr.read(data))>0)
{
String read_data = String.copyValueOf(data, 0, size);
final_data+= read_data;
data = new char[data_block];
}
Toast.makeText(getBaseContext(), "Message :"+final_data, Toast.LENGTH_LONG).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
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;
// }
}
метод 'openFileOutput' создать файл в каталоге файлов pf Ваша папка пакета приложений. Например, '/ data/data//Files'. –
user370305
@qcGold - наоборот, этот код создает файл! –