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
| package com.android.oneeveryday;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.TextView;
public class OneeverydayActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//variables
String FILENAME = "counter";//nom fichier
String FILENAMEB = "log";//nom fichier
String string = "0";//contenu fichier
int counter = 0;
int logdate = 0;
String chaine = "";
String chaineb = "";
Calendar rightNow = Calendar.getInstance();
String stringb = "" + rightNow.get(Calendar.DAY_OF_YEAR);//contenu fichier
String chainec = "" + rightNow.get(Calendar.DAY_OF_YEAR);
int date = Integer.valueOf(chainec).intValue();
logdate = date;
String output = "";//chaine affichée à la fin
FileInputStream fis;
FileInputStream fisb;
byte[] buffer = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
byte[] bufferb = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int byteOffset = 0;//on commence à écrire dans le buffer à l'indice 0
int byteCount = 10;//nombre maximal de bytes à écrire dans le buffer
//----------------------
try {//on essaye de lire le fichier
fis = openFileInput(FILENAME);
fis.read (buffer, byteOffset, byteCount);
fis.close();
fisb = openFileInput(FILENAMEB);
fisb.read (bufferb, byteOffset, byteCount);
fisb.close();
} catch (FileNotFoundException e) {//si le fichier n'est pas trouvé, il faut le créer puis réessayer
FileOutputStream fos;
FileOutputStream fosb;
try {//on essaye de créer le fichier
fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
fosb = openFileOutput(FILENAMEB, Context.MODE_PRIVATE);
fosb.write(stringb.getBytes());
fosb.close();
} catch (FileNotFoundException e1) {} catch (IOException e1) {}//exceptions
try {//puis on réessaye de le lire
fis = openFileInput(FILENAME);
fis.read (buffer, byteOffset, byteCount);
fis.close();
fisb = openFileInput(FILENAMEB);
fisb.read (bufferb, byteOffset, byteCount);
fisb.close();
} catch (FileNotFoundException e1) {} catch (IOException e1) {}
} catch (IOException e) {}
for(int i = 0; i < 10; i++){chaine += (char) buffer[i];}//on convertit les bytes en char et on les écrit dans la variable chaine
for(int j = 0; j < 10; j++){chaineb += (char) bufferb[j];}//on convertit les bytes en char et on les écrit dans la variable chaine
chaine = chaine.trim();//on enlève les espaces avant la conversion
chaineb = chaineb.trim();//on enlève les espaces avant la conversion
counter = Integer.valueOf(chaine).intValue() + 1;//on convertit en int puis on ajoute 1
int x = counter;
chaine = "" + x;
logdate = Integer.valueOf(chaineb).intValue();//on convertit en int
if(date != logdate){
//on écrit le nombre pour la prochaine fois
FileOutputStream fos2;
try {//on essaye de créer le fichier
fos2 = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos2.write(chaine.getBytes());
fos2.close();
} catch (FileNotFoundException e1) {} catch (IOException e1) {}//exceptions
//------------------------------------------
//
FileOutputStream fos3;
try {//on essaye de créer le fichier
fos3 = openFileOutput(FILENAMEB, Context.MODE_PRIVATE);
fos3.write(chainec.getBytes());
fos3.close();
} catch (FileNotFoundException e1) {} catch (IOException e1) {}//exceptions
//------------------------------------------
}
String[] phrase = {
"phrase1",
"phrase2",
"phrase3",
"phrase4",
"phrase5",
"phrase5",
"phrase7",
"phrase8",
"phrase9",
"phrase10"};
for(int k = 0;k < counter; k++){
output += phrase[k] + " ";}
TextView tv = new TextView(this);
tv.setText(output + " counter = " + counter + ", log = " + logdate);
setContentView(tv);
}
} |
Partager