PDA

View Full Version : متصل کردن فایل تکست



amrialiesmaili
یک شنبه 08 بهمن 1391, 15:59 عصر
سلام قبلا تاپیک زده بودم و یگی از دوستان نیمه توضیح داد ولی الان میخوام که شما کامل سورس فایل جاوا ور بزارید(با قسمت import) ممنون از شما

endexample
چهارشنبه 11 بهمن 1391, 09:41 صبح
سلام دوست من متنی رو از edit text می خونه و در یک فایل مینویسه و یا متن نوشته شده رو میخونه و در edittext قرار میده


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;


public class MainActivity extends Activity {
EditText ed;
Button btn;
Button btnr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn=(Button)findViewById(R.id.btn1);
ed=(EditText)findViewById(R.id.ed1);

btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
//

//if ed is null sould reaf\d then write
if(ed.getText().toString()!="" && ed.getText()!=null && ed.getText().toString()!=" "){
// read from text box]
String s=ed.getText().toString();
ed.setText("");
// write to txt file
try{
OutputStreamWriter out = new OutputStreamWriter(openFileOutput("myfilename.txt",0));
// write the contents on mySettings to the file
out.write(s);
// close the file
out.close();
Toast.makeText(MainActivity.this, "Write success", Toast.LENGTH_LONG).show();

}catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_LONG).show();
}


}


}
});

btnr=(Button)findViewById(R.id.btn2);
btnr.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// write to text box
try{
String ss="";
// open the file for reading
InputStream instream = openFileInput("myfilename.txt");

// if file the available for reading
if (instream != null) {
// prepare the file for reading
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);

String line;

// read every line of the file into the line-variable, on line at the time
while (( line = buffreader.readLine())!=null) {
// do something with the settings from the file
ss+=line;
}

}

// close the file again
instream.close();
ed.setText(ss);
Toast.makeText(MainActivity.this, "SET success->"+ss, Toast.LENGTH_LONG).show();

}catch (Exception e) {
// TODO: handle exception
Toast.makeText(MainActivity.this, e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
}

}
});

}


}