import java.io.File;
import java.io.RandomAccessFile;
import android.os.Environment;
import android.util.Log;
public class RandomFileWriter {
public long writeData(String text,String path,long seek){
try {
// example path : "/data/data/com.example.test/file"
// example path : "/data/data/com.example.test/file"
RandomAccessFile file = new RandomAccessFile(path, "rw");
long i=file.length()-seek,j;
Log.i("File length",""+i);
file.seek(i);
file.writeBytes(text);
j=file.getFilePointer();
file.close();
return j;
}catch (Exception e) {
// TODO Auto-generated catch block
Log.d("RandomFileWrite.java","Exception: "+e.getMessage());
return 0;
}
}
public void seekWrite(String text,String path,long seek) {
try {
RandomAccessFile file = new RandomAccessFile(path, "rw");
file.seek(seek);
file.writeBytes(text);
file.close();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.d("RandomFileWrite.java seekwrite","Exception: "+e.getMessage());
}
}
String getExternalPath(){
return Environment.getExternalStorageDirectory().toString();
}
boolean isMediaReadOnly(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
return true;
}
return false;
}
boolean isMediaMounted(){
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.i("Exter", "media found: "+Environment.getExternalStorageDirectory());
return true;
}
return false;
}
public void createDir(String path,String dir){
new File(path+dir).mkdir();
}
public void createDir(File dirPath){
dirPath.mkdir();
}
public boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; ilength; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
public boolean delFile(File f){
return f.delete();
}
void copy(File src, File dst) throws Exception{
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
RandomAccessFile in = new RandomAccessFile(src, "r");
RandomAccessFile out = new RandomAccessFile(dst, "rw");
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
}
No comments:
Post a Comment