1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
| package com.example.hfzd;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class NoteActivity extends Activity{
/** Called when the activity is first created.
* @return */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_note);
final EditText text = (EditText) findViewById(R.id.texte_note);
Button valider = (Button) findViewById(R.id.save);
Button vider = (Button) findViewById(R.id.vide);
TextView letitre = (TextView) findViewById(R.id.titre_note);
final String titre = letitre.getText().toString();
/*
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy");
final String dateStr = simpleDateFormat.format(new Date());
*/
setContentView(R.layout.activity_note);
Button btvoir = (Button) findViewById(R.id.vide);
Button btecrire = (Button) findViewById(R.id.save);
btvoir.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Context lecontext = getBaseContext();
Read(lecontext);
}
});
btecrire.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
TextView datatext = (TextView) findViewById(R.id.texte_note);
String sQuantite = datatext.getText()+"\n";
Context lecontext = getBaseContext();
Write(lecontext,sQuantite);
}
});
/*
//////////////////////SAUVEGARDER FICHIER//////////////////////
valider.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
ecrireFichier(titre+"_"+dateStr+".txt", text.getText().toString());
}
});
//////////////////////VIDER CHAMP DE TEXTE//////////////////////
vider.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
text.setText("");
}
});
}
//////////////////////FONCTION ECRITURE FICHIER//////////////////////
public void ecrireFichier(String nomFichier,String monText) {
BufferedWriter writer = null;
try {
String FILENAME = nomFichier;
String string = monText;
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(string.getBytes());
fos.close();
} catch (Exception e) {
Log.e("NoteActivity","Failed to write file",e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
Log.e("NoteActivity","Failed to gracefully close output stream",e);
}
}
}
}
*/
}
public void Write(Context context, String data){
FileOutputStream fOut = null;
OutputStreamWriter osw = null;
try{
fOut = context.openFileOutput("settings.dat",MODE_APPEND);
osw = new OutputStreamWriter(fOut);
osw.write(data);
osw.flush();
//popup surgissant pour le résultat
Toast.makeText(context, "Settings saved",Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
finally {
try {
osw.close();
fOut.close();
} catch (IOException e) {
Toast.makeText(context, "Settings not saved",Toast.LENGTH_SHORT).show();
}
}
}
public String Read(Context context){
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[255];
String data = null;
try{
fIn = context.openFileInput("settings.dat");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
//affiche le contenu de mon fichier dans un popup surgissant
Toast.makeText(context, " "+data,Toast.LENGTH_SHORT).show();
}
catch (Exception e) {
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
/*finally {
try {
isr.close();
fIn.close();
} catch (IOException e) {
Toast.makeText(context, "Settings not read",Toast.LENGTH_SHORT).show();
}
} */
return data;
}
} |
Partager