Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

Java help (groovicus)


  • Please log in to reply
8 replies to this topic

#1 raw

raw

    Bleeping Hacker


  • Members
  • 2,577 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Texas
  • Local time:07:01 PM

Posted 13 October 2011 - 11:51 PM

So i thought this was something simple. Turns out i was wrong.
I have a folder on my SD card (android phone) and i want a simple
app/widget on my home screen to delete the files in that folder.
The android/linux OS is not my problem, it's the java api that's killing me.
Can you point me to a decent tutorial? I don't need to know if the dir exists
and i need a wildcard, eg: String fileName = "*.zip";
This is what i am hoping to accomplish: Icon on Android home screen, push button, "rm -rf /path/to/folder/*" (the folder only contains zips)

I'm trying to follow this: http://www.java2s.com/Code/Java/File-Input-Output/DeletefileusingJavaIOAPI.htm
import java.io.File;

public class Delete {
  public static void main(String[] args) {
    String fileName = "file.txt";
    // A File object to represent the filename
    File f = new File(fileName);

---snipped---

// Attempt to delete it
    boolean success = f.delete();


rawsig.png

 rawcreations.net          @raw_creations


Current systems: WHAT OS, BackTrack-raw, PCLinuxOS, Peppermint OS 6, Kali Linux

and a custom Linux From Scratch server hosting a bunch of top secret stuff.


BC AdBot (Login to Remove)

 


#2 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:01 PM

Posted 14 October 2011 - 08:53 AM

Paths on mobile devices are fussy, and sometimes hardware specific. Since you didn't tell me what is happening when you try to run that, I have a few suggestions. First, are you able to create a test program that will simply find the directory and report success or failure? The example you have there does not ask for a directory, so depending on where your app is running in relation to to where the file is stored, it won't find it. The sample program looks to me like the file that is to be deleted is in the same directory as the java executable.

The first thing you need to figure out is the path to the directory. You may have already discovered that unlike PCs, mobile devices do not assign letters to storage media. They just simply use a path name. My Window's based devices have a file explorer, so I can hunt around to find the path I want. I don't know if Androids have something like that or not. Once you do that, write an app to see if you can find the directory. Once you can find the directory, then the rest is trivial.

That help any? I am assuming that you are already using the Android developer's API documents?

#3 raw

raw

    Bleeping Hacker

  • Topic Starter

  • Members
  • 2,577 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Texas
  • Local time:07:01 PM

Posted 15 October 2011 - 12:06 PM

Haven't tried running any of it yet. The path is /mnt/sdcard/z7logs/
The phone stores a ton of log files with no option to turn logging off.
I have been using the file manager to open the z7 folder, select all, delete ad naseum :)
I have my frame work started and i'm trying to follow the reference:
http://developer.android.com/reference/java/io/File.html
File file = new File(/mnt/sdcard/z7logs/*.zip);
boolean deleted = file.delete();
I don't know if the path needs to be quoted,
if wildcards work with this API or in java or
if that code is even right. This is my first attempt at Java so be gentle. lol
And my first time using Eclipse.

rawsig.png

 rawcreations.net          @raw_creations


Current systems: WHAT OS, BackTrack-raw, PCLinuxOS, Peppermint OS 6, Kali Linux

and a custom Linux From Scratch server hosting a bunch of top secret stuff.


#4 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:01 PM

Posted 16 October 2011 - 08:42 AM

The file path has to be quoted; the constructor takes a string as an argument, and the string will be interpreted literally. In other words, it will look for a file named '*'. Basically, here are the steps for the app:
1. See if the directory exists.
2. If the directory exists, build a list of files in the directory.
3. Loop through the list and delete the files. If you only want to delete .zip files, then do a string match to see if the filename contains that extension.

A bit of advice; do not test the app on your Android. That is a good way to accidentally brick your device. Use an emulator. The only caveat to this is that the emulator will not have a directory structure per se, so you may also have to write an additional bit of code to create a directory, and to create some mock files to go in that directory. You can also just test it on your PC using a window's style path (or Unix, whatever you may be using). If it works on your PC, then it should work on your mobile device by changing the path name.

#5 raw

raw

    Bleeping Hacker

  • Topic Starter

  • Members
  • 2,577 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Texas
  • Local time:07:01 PM

Posted 17 October 2011 - 01:35 PM

I am using the emulator, it interfaces with Eclipse very nice.
So no wildcards (*) in Java? Ok i'm catching on now.
The zips are all date coded: z7-android-logs-2011-10-17-timeofday.zip
1 the directory ALWAYS exists ( i have considered removing write privileges or deleting it completely)
2 & 3 By the end of a day there are 15 or more of these zips
They are only zips, no other files to worry about.
I'll give it a shot tonight and let you know. TY

rawsig.png

 rawcreations.net          @raw_creations


Current systems: WHAT OS, BackTrack-raw, PCLinuxOS, Peppermint OS 6, Kali Linux

and a custom Linux From Scratch server hosting a bunch of top secret stuff.


#6 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:01 PM

Posted 17 October 2011 - 08:03 PM

Just one point of argument. What if at some point Android makes some major update to its OS and they decide to rename that folder, or move it to a different location? I am not saying that they will, but from a developer standpoint it is a good idea to consider the possibility.

As for the rest, it is pretty straight forward.

#7 raw

raw

    Bleeping Hacker

  • Topic Starter

  • Members
  • 2,577 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Texas
  • Local time:07:01 PM

Posted 18 October 2011 - 01:16 PM

I gave that some thought and for the past few days I have
been deleting the entire folder instead of just the .zips
inside it. Nothing breaks and the app happily creates a new
folder to store new logs in. So I've decided to simplify and
just have my app/widget do: "rm -rf /mnt/sdcard/z7logs"
I'm gonna give that a shot tonight or tomorrow, let you know
how it goes.

rawsig.png

 rawcreations.net          @raw_creations


Current systems: WHAT OS, BackTrack-raw, PCLinuxOS, Peppermint OS 6, Kali Linux

and a custom Linux From Scratch server hosting a bunch of top secret stuff.


#8 raw

raw

    Bleeping Hacker

  • Topic Starter

  • Members
  • 2,577 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Texas
  • Local time:07:01 PM

Posted 06 November 2011 - 12:57 AM

Update: I decided i didn't need an app to do this,
merely a cron job that runs hourly. :thumbup2:

rawsig.png

 rawcreations.net          @raw_creations


Current systems: WHAT OS, BackTrack-raw, PCLinuxOS, Peppermint OS 6, Kali Linux

and a custom Linux From Scratch server hosting a bunch of top secret stuff.


#9 groovicus

groovicus

  • Security Colleague
  • 9,963 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Centerville, SD
  • Local time:06:01 PM

Posted 08 November 2011 - 01:07 PM

Probably a good idea. Usually the most simple solution is the best.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users