Класс простого ридера(javaFX)



Самый простой ридер какой может быть.

public class FXMLReaderController implements Initializable {

@FXML

private TextArea t;

@FXML

private void openButton(ActionEvent event) {

FileChooser fileChooser = new FileChooser();

fileChooser.setTitle("Открытие файла");

File file = fileChooser.showOpenDialog(null);

if (file != null) {

t.setText(readerFile(file.getAbsolutePath()));

}

}

String readerFile(String s){

String str,f="";

try{

File file=new File(s);

FileReader fr=new FileReader(file);

BufferedReader br=new BufferedReader(fr);

while((str = br.readLine()) != null){

f+=str+"\n";

}

}catch(IOException e){

e.getMessage();

}

return f;

}

@Override

public void initialize(URL url, ResourceBundle rb) {

t.setText("Программа для чтения текстовых файлов");

}

}

Вызов SMS-клиента(android)

Intent sendIntent = new Intent(Intent.ACTION_VIEW);

sendIntent.putExtra("sms_body", «текст_сообщения»);

sendIntent.setType("vnd.android-dir/mms-sms");

startActivity(sendIntent);

Текстовый квест для android

Разбиваем текст на пронумерованные части и сохраняем их в виде txt в папке assets, откуда и читаем их при помощи метода read(int n)(см. ниже).


public class Quest extends AppCompatActivity {

EditText input;

TextView text;

ScrollView s;

File sdPath,sdFile;

int m=0;

final int newGame=1,showHistory=2,closeHistory=3,exit=4;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_quest);

input=(EditText)findViewById(R.id.editText);

text=(TextView)findViewById(R.id.textView);

s=(ScrollView)findViewById(R.id.scrollView);

if (!Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED)) {

Toast.makeText(getApplicationContext(),"Память недоступна", Toast.LENGTH_SHORT).show();

return;

}

sdPath = Environment.getExternalStorageDirectory();

sdPath = new File(sdPath.getAbsolutePath() + "/" + "TextQuest");

sdFile = new File(sdPath, "history");

if (!sdPath.exists()) {

sdPath.mkdirs();

text.setText(read(1));

writeFile("0",true);

}else {

input.setText(readHistory(true));

m=1;

go(null);

m=0;

}

}

public void go(View view){

int n;

try{

n=Integer.parseInt(input.getText().toString());

}catch(NumberFormatException e){

Toast.makeText(getApplicationContext(),"введите номер пункта",Toast.LENGTH_SHORT).show();

input.setText("");

input.requestFocus();

return;

}

if(n>350||n<0){

Toast.makeText(getApplicationContext(),"номер за пределами диапазона",Toast.LENGTH_SHORT).show();

input.setText("");

input.requestFocus();

return;

}

text.setText(read(n+1));

if(m==0){

writeFile(n+"",true);

}

input.setText("");

s.scrollTo(0,0);

}

String readHistory(boolean b){

String str="",f="";

try {

BufferedReader br = new BufferedReader(new FileReader(sdFile));

while ((str = br.readLine()) != null) {

if (b)

f=str;

else

f+=str+"->";

}

} catch (FileNotFoundException e) {

e.printStackTrace();

Toast.makeText(getApplicationContext(),"Произошла ошибка", Toast.LENGTH_SHORT).show();

} catch (IOException e) {

e.printStackTrace();

Toast.makeText(getApplicationContext(),"Произошла ошибка", Toast.LENGTH_SHORT).show();

}

return f;

}

String read(int n){

String str,f="";

try {

BufferedReader reader=new BufferedReader(new InputStreamReader(getAssets().open(n+".txt")));

str=reader.readLine();

while (str!=null){

f+=str+"\n";

str=reader.readLine();

}

reader.close();

}

catch (IOException e){

Toast.makeText(getApplicationContext(),"ERROR FILE OPEN!", Toast.LENGTH_SHORT).show();

}

return f;

}

void writeFile(String s,boolean b){

try {

BufferedWriter bw = new BufferedWriter(new FileWriter(sdFile,b));

bw.write(s+"\n");

bw.close();

} catch (IOException e) {

e.printStackTrace();

Toast.makeText(getApplicationContext(),"Произошла ошибка", Toast.LENGTH_SHORT).show();

return;

}

}

public boolean onCreateOptionsMenu(Menu menu) {

getMenuInflater().inflate(R.menu.quest, menu);

menu.add(0,newGame,0,"Начать Заново");

menu.add(0,showHistory,0,"Показать историю");

menu.add(0,closeHistory,0,"Скрыть историю");

menu.add(0,exit,0,"ВЫХОД");

return true;

}

public boolean onOptionsItemSelected(MenuItem item){

switch (item.getItemId()){

case newGame:

text.setText(read(1));

writeFile("0", false);

s.scrollTo(0,0);

break;

case showHistory:

text.setText(readHistory(false));

s.scrollTo(0,0);

break;

case closeHistory:

input.setText(readHistory(true));

m=1;

go(null);

m=0;

s.scrollTo(0,0);

break;

case exit:

finish();

break;

}

return super.onOptionsItemSelected(item);

}

}


Дата добавления: 2019-02-12; просмотров: 130; Мы поможем в написании вашей работы!

Поделиться с друзьями:






Мы поможем в написании ваших работ!