アプリケーションを開くときは、外部から読み取り、連絡先からすべての番号をそのファイルにコピーする必要がありますが、常に強制的に閉じようとするとき。
public class anti_spam extends TabActivity {
/** Called when the activity is first created. */
BufferedReader reader;
public String[] Number;
int x=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//------------------------
getListNumber();
File logFile = new File("root/log.txt");
//Hitung Jumlah isi File
try {
reader = new BufferedReader(new FileReader("root/log.txt"));
try {
while (reader.readLine()!= null)
{
x++;
}
reader.close();
} catch (IOException e) {}
} catch (FileNotFoundException e1) {}
//Hapus File Jika Jumlah Kontak Beda
if (x-1 != Number.length){
logFile.delete();
}
if (!logFile.exists())
{
try
{
logFile.createNewFile();
Toast.makeText(getBaseContext(), "Data Berhasil Dibuat", Toast.LENGTH_LONG).show();
//Isi File
try
{
//BufferedWriter for performance, true to set append to file flag
BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
String[] numTemp;
String numBaru;
for(int i=0;i<Number.length;i++)
{
numTemp = Number[i].split("-");
numBaru = (numTemp[0]+numTemp[1]+numTemp[2]);
if (numBaru.substring(0, 1).equals("0"))
{
numBaru = numBaru.replaceFirst("0","+62");
}
buf.append(numBaru + "\n");
}
buf.newLine();
buf.close();
}
catch (IOException e)
{
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
Toast.makeText(getBaseContext(), e.toString(), Toast.LENGTH_LONG).show();
}
}}
マニフェストで
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"> </uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"> </uses-permission>