Skip to content

Instantly share code, notes, and snippets.

@MostafaAnter
Created March 23, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MostafaAnter/6f40aaef45800e03a1df to your computer and use it in GitHub Desktop.
Save MostafaAnter/6f40aaef45800e03a1df to your computer and use it in GitHub Desktop.
if you want creat file and save some data the you want search about data in file use this simple example
package com.mostafaanter.file_master;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
public class MainActivity extends Activity {
private EditText editTextAdd;
private EditText searchEditText;
private Button addMessage;
private Button search;
private ImageView expression;
static final int READ_BLOCK_SIZE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextAdd = (EditText) findViewById(R.id.editText);
searchEditText = (EditText) findViewById(R.id.editText2);
addMessage = (Button) findViewById(R.id.button1);
search = (Button) findViewById(R.id.button2);
expression = (ImageView) findViewById(R.id.image);
}
public void addMessage(View view){
//---writing to files---
try {
FileOutputStream fOut =
openFileOutput("textfile.txt", MODE_PRIVATE);
//---For writing bytes to a fie, use the OutputStreamWriter class and use its write( ) method to write a
//---string to the fie. To save the changes to the fie, use its close( ) method:
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//---write the string to the file---
osw.write(editTextAdd.getText().toString());
osw.close();
//---display file saved message---
Toast.makeText(getBaseContext(), "File saved successfully! ",
Toast.LENGTH_SHORT) .show();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
public void searchProcess(View view){
//---reading from files---
try
{
FileInputStream fIn =
openFileInput("textfile.txt");
InputStreamReader isr = new
InputStreamReader(fIn);
/*---Because you do not know how many characters need to be read from the fie, you use the read( )
method from the InputStreamReader object to read in the characters in blocks of 100 bytes (you
can change this to a value reasonable to you). The read( ) method returns the number of characters
read, and reading continues until there are no more unread characters (it returns -1 when the end of
the fie is reached):
---*/
char[] inputBuffer = new char[READ_BLOCK_SIZE];
String s = "";
int charRead;
while ((charRead = isr.read(inputBuffer))>0)
{
//---convert the chars to a String---
String readString =
String.copyValueOf(inputBuffer, 0,
charRead);
s += readString;
inputBuffer = new char[READ_BLOCK_SIZE];
}
isr.close();
//-- execute search functionality
if(s.contains(searchEditText.getText().toString())){
Toast.makeText(getBaseContext(),
"File loaded successfully! " + s,
Toast.LENGTH_SHORT) .show();
expression.setImageResource(R.drawable.smile1);
}else
expression.setImageResource(R.drawable.smile2);
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment