package com.android.write;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
public class WriteFileActivity extends Activity {
/** Called when the activity is first created. */
private String filePath = "/mnt/";
private String fileName = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String str1 = "aa:" + currentTime() + "\r\n";
String str2 = "a:" + "\r\n";
String str3 = "a" + "\r\n";
String str4 = "a" + "\r\n";
String str5 = "desc" + "\r\n\r\n";
// writeTxtFile(str1 + str2 + str3 + str4 + str5, filePath
}
private final Timer timer = new Timer();
private TimerTask task;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
deleteFile();
break;
}
super.handleMessage(msg);
}
};
public void taskDelFile() {
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer.schedule(task, 1000, 1000 * 60 * 60);
}
public void deleteFile() {
int flag = 0;
File file = new File(filePath);
File[] fs = file.listFiles();
Arrays.sort(fs, new WriteFileActivity.CompratorByLastModified());
for (int i = 0; i < fs.length; i++) {
if (fs[i].getName().length() == 18
&& fs[i].getName().substring(12).toString()
.equals("00.txt")) {
if (flag == 0) {
flag++;
continue;
} else {
fs[i].delete();
}
} else {
fs[i].delete();
}
}
}
static class CompratorByLastModified implements Comparator<File> {
@Override
public int compare(File lhs, File rhs) {
// TODO Auto-generated method stub
long diff = rhs.lastModified() - lhs.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return super.equals(o);
}
}
public String currentTime() {
long time = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
return format.format(new Date(time)) + "00";
}
public String createFileName() {
long time = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
String strTime = format.format(new Date(time)).toString();
int mm = Integer.parseInt(strTime.substring(10, 12));
if (mm >= 0 && mm < 16) {
strTime = strTime.substring(0, 10) + "00";
} else if (mm > 15 && mm < 31) {
strTime = strTime.substring(0, 10) + "15";
} else if (mm > 31 && mm < 46) {
strTime = strTime.substring(0, 10) + "30";
} else if (mm > 45 && mm <= 59) {
strTime = strTime.substring(0, 10) + "45";
}
fileName = strTime + "00.txt";
return fileName;
}
/**
* FileOutputStream
*
* @param strContent
* @param strPath
*/
public void writeTxtFile(String strContent, String strPath) {
BufferedWriter bw = null;
try {
File file = new File(strPath);
if (!file.exists()) {
file.createNewFile();
}
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(strPath, true)));
bw.write(strContent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* FileWriter
*
* @param strContent
* @param strPath
*/
public void writeTxtFile1(String strContent, String strPath) {
try {
File file = new File(strPath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(strPath, true);
writer.write(strContent);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* RandomAccessFile
*
* @param strContent
* @param strPath
*/
public void writeTxtFile2(String strContent, String strPath) {
try {
RandomAccessFile randomFile = new RandomAccessFile(strPath, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(strContent);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
}
return sdDir.toString();
}
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
public class WriteFileActivity extends Activity {
/** Called when the activity is first created. */
private String filePath = "/mnt/";
private String fileName = "";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String str1 = "aa:" + currentTime() + "\r\n";
String str2 = "a:" + "\r\n";
String str3 = "a" + "\r\n";
String str4 = "a" + "\r\n";
String str5 = "desc" + "\r\n\r\n";
// writeTxtFile(str1 + str2 + str3 + str4 + str5, filePath
// + createFileName());
}
private final Timer timer = new Timer();
private TimerTask task;
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch (msg.what) {
case 1:
deleteFile();
break;
}
super.handleMessage(msg);
}
};
public void taskDelFile() {
task = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
}
};
timer.schedule(task, 1000, 1000 * 60 * 60);
}
public void deleteFile() {
int flag = 0;
File file = new File(filePath);
File[] fs = file.listFiles();
Arrays.sort(fs, new WriteFileActivity.CompratorByLastModified());
for (int i = 0; i < fs.length; i++) {
if (fs[i].getName().length() == 18
&& fs[i].getName().substring(12).toString()
.equals("00.txt")) {
if (flag == 0) {
flag++;
continue;
} else {
fs[i].delete();
}
} else {
fs[i].delete();
}
}
}
static class CompratorByLastModified implements Comparator<File> {
@Override
public int compare(File lhs, File rhs) {
// TODO Auto-generated method stub
long diff = rhs.lastModified() - lhs.lastModified();
if (diff > 0)
return 1;
else if (diff == 0)
return 0;
else
return -1;
}
@Override
public boolean equals(Object o) {
// TODO Auto-generated method stub
return super.equals(o);
}
}
public String currentTime() {
long time = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
return format.format(new Date(time)) + "00";
}
public String createFileName() {
long time = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
String strTime = format.format(new Date(time)).toString();
int mm = Integer.parseInt(strTime.substring(10, 12));
if (mm >= 0 && mm < 16) {
strTime = strTime.substring(0, 10) + "00";
} else if (mm > 15 && mm < 31) {
strTime = strTime.substring(0, 10) + "15";
} else if (mm > 31 && mm < 46) {
strTime = strTime.substring(0, 10) + "30";
} else if (mm > 45 && mm <= 59) {
strTime = strTime.substring(0, 10) + "45";
}
fileName = strTime + "00.txt";
return fileName;
}
/**
* FileOutputStream
*
* @param strContent
* @param strPath
*/
public void writeTxtFile(String strContent, String strPath) {
BufferedWriter bw = null;
try {
File file = new File(strPath);
if (!file.exists()) {
file.createNewFile();
}
bw = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(strPath, true)));
bw.write(strContent);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* FileWriter
*
* @param strContent
* @param strPath
*/
public void writeTxtFile1(String strContent, String strPath) {
try {
File file = new File(strPath);
if (!file.exists()) {
file.createNewFile();
}
FileWriter writer = new FileWriter(strPath, true);
writer.write(strContent);
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* RandomAccessFile
*
* @param strContent
* @param strPath
*/
public void writeTxtFile2(String strContent, String strPath) {
try {
RandomAccessFile randomFile = new RandomAccessFile(strPath, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.writeBytes(strContent);
randomFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String getSDPath() {
File sdDir = null;
boolean sdCardExist = Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED); // 判断sd卡是否存在
if (sdCardExist) {
sdDir = Environment.getExternalStorageDirectory();// 获取跟目录
}
return sdDir.toString();
}
}
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />