Example usage for java.util StringTokenizer StringTokenizer

List of usage examples for java.util StringTokenizer StringTokenizer

Introduction

In this page you can find the example usage for java.util StringTokenizer StringTokenizer.

Prototype

public StringTokenizer(String str, String delim) 

Source Link

Document

Constructs a string tokenizer for the specified string.

Usage

From source file:Main.java

public static int findProcessIdWithPS(String command) throws Exception {

    int procId = -1;

    Runtime r = Runtime.getRuntime();

    Process procPs = null;/*from   w w  w .ja  v a  2 s.co  m*/

    procPs = r.exec(SHELL_CMD_PS);

    BufferedReader reader = new BufferedReader(new InputStreamReader(procPs.getInputStream()));
    String line = null;

    while ((line = reader.readLine()) != null) {
        if (line.indexOf(' ' + command) != -1) {

            StringTokenizer st = new StringTokenizer(line, " ");
            st.nextToken(); //proc owner

            procId = Integer.parseInt(st.nextToken().trim());

            break;
        }
    }

    return procId;

}

From source file:com.thruzero.common.core.utils.StringUtilsExt.java

/**
 * Converts the given token stream of keyValuePairs, using the given separator, into a StringMap. Leading and trailing spaces of the keys and values are
 * trimmed.//from  www .  j a  va  2  s.c o  m
 * <p>
 * Example input: "booleanTrue=true ,booleanFalse=false, integerOne=1,longOne=1234567890"
 */
public static StringMap tokensToMap(final String keyValuePairs, final String separator) {
    StringMap result = new StringMap();

    if (StringUtils.isNotEmpty(keyValuePairs)) {
        StringTokenizer st = new StringTokenizer(keyValuePairs, separator);

        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            StringTokenizer st2 = new StringTokenizer(token, "=");

            result.put(StringUtils.trimToEmpty(st2.nextToken()),
                    st2.hasMoreTokens() ? StringUtils.trimToNull(st2.nextToken()) : null);
        }
    }

    return result;
}

From source file:Main.java

public static String[] getStorageDirectories() {
    String[] dirs = null;/*w ww.j  ava 2 s.  c o m*/
    BufferedReader bufReader = null;
    ArrayList<String> list = new ArrayList<String>();
    list.add(Environment.getExternalStorageDirectory().getPath());

    List<String> typeWL = Arrays.asList("vfat", "exfat", "sdcardfs", "fuse");
    List<String> typeBL = Arrays.asList("tmpfs");
    String[] mountWL = { "/mnt", "/Removable" };
    String[] mountBL = { "/mnt/secure", "/mnt/shell", "/mnt/asec", "/mnt/obb", "/mnt/media_rw/extSdCard",
            "/mnt/media_rw/sdcard", "/storage/emulated" };
    String[] deviceWL = { "/dev/block/vold", "/dev/fuse", "/mnt/media_rw/extSdCard" };

    try {
        bufReader = new BufferedReader(new FileReader("/proc/mounts"));
        String line;
        while ((line = bufReader.readLine()) != null) {

            StringTokenizer tokens = new StringTokenizer(line, " ");
            String device = tokens.nextToken();
            String mountpoint = tokens.nextToken();
            String type = tokens.nextToken();

            // skip if already in list or if type/mountpoint is blacklisted
            if (list.contains(mountpoint) || typeBL.contains(type) || StartsWith(mountBL, mountpoint))
                continue;

            // check that device is in whitelist, and either type or mountpoint is in a whitelist
            if (StartsWith(deviceWL, device) && (typeWL.contains(type) || StartsWith(mountWL, mountpoint)))
                list.add(mountpoint);
        }

        dirs = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            dirs[i] = list.get(i);
        }
    } catch (FileNotFoundException e) {
    } catch (IOException e) {
    } finally {
        if (bufReader != null) {
            try {
                bufReader.close();
            } catch (IOException e) {
            }
        }
    }
    return dirs;
}

From source file:net.sf.zekr.common.commandline.CleanCommandHandler.java

public void execute() throws CommandException {
    if (options.length <= 0)
        throw new CommandException("No clean target specified. "
                + "Enter either `view-cache', `playlist-cache', `config', `index-me', `index-all',"
                + " `all' (for cleaning all the targets), or a combination of cleaning targets separated with comma.");
    String targets = options[0];/*ww  w .  java2 s.c o  m*/
    StringTokenizer st = new StringTokenizer(targets, ",");
    List<String> delList = new ArrayList<String>();
    while (st.hasMoreTokens()) {
        String target = st.nextToken();
        if (target.trim().equals("view-cache")) {
            delList.add(Naming.getViewCacheDir());
        } else if (target.trim().equals("playlist-cache")) {
            delList.add(Naming.getAudioCacheDir());
        } else if (target.trim().equals("config")) {
            delList.add(Naming.getConfigDir());
        } else if (target.trim().equals("index-me")) {
            delList.add(Naming.getQuranIndexDir());
        } else if (target.trim().equals("index-all")) {
            delList.add(ApplicationPath.QURAN_INDEX_DIR);
        } else if (target.trim().equals("all")) {
            delList.add(Naming.getViewCacheDir());
            delList.add(Naming.getAudioCacheDir());
            delList.add(Naming.getConfigDir());
            delList.add(Naming.getQuranIndexDir());
            delList.add(ApplicationPath.QURAN_INDEX_DIR);
        } else {
            throw new CommandException("Invalid clean target: " + target);
        }
    }

    for (Iterator<String> iter = delList.iterator(); iter.hasNext();) {
        String path = iter.next();
        try {
            File f = new File(path);
            stdout.print("-> Deleting \"" + path + "\"");
            if (!f.exists()) {
                stdout.println(" failed. Path does not exist!");
                continue;
            }
            FileUtils.deleteDirectory(f);
            stdout.println(" done.");
        } catch (IOException e) {
            throw new CommandException("Cleaning error: " + e);
        }
    }
}

From source file:net.anthonychaves.bookmarks.service.TagService.java

public void addTags(Bookmark bookmark) {
    StringTokenizer st = new StringTokenizer(bookmark.getTags(), ", ");

    try {/*  w ww. ja  v  a  2s .  co m*/
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            jredis.lpush(token, bookmark.getId());
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.salesmanager.core.util.StringUtil.java

public static Map deformatUrlResponse(String payload) throws Exception {
    HashMap nvp = new HashMap();
    StringTokenizer stTok = new StringTokenizer(payload, "&");
    while (stTok.hasMoreTokens()) {
        StringTokenizer stInternalTokenizer = new StringTokenizer(stTok.nextToken(), "=");
        if (stInternalTokenizer.countTokens() == 2) {
            String key = URLDecoder.decode(stInternalTokenizer.nextToken(), "UTF-8");
            String value = URLDecoder.decode(stInternalTokenizer.nextToken(), "UTF-8");
            nvp.put(key.toUpperCase(), value);
        }/*  w  ww .j av a  2 s .c o  m*/
    }
    return nvp;
}

From source file:TokenizerUtil.java

public static Map<String, String> convertCSVStringToMap(String s) {
    TreeMap<String, String> map = new TreeMap<String, String>();
    StringTokenizer st = new StringTokenizer(s, "|");
    while (st.hasMoreTokens()) {
        String keyToken = st.nextToken();
        String valueToken = st.nextToken();
        map.put(keyToken, valueToken);//from  www .  j a  va  2s.c  o m
    }
    return map;
}

From source file:StringUtil.java

/**
 * Parses a list according to the specified delimiter into an
 * array of Strings.//from   w w  w  .ja v a2s  .com
 * @see java.util.StringTokenizer
 * @param list a string of token seperated values
 * @param delim the delimiter character(s).  Each character in the string is a
 * single delimeter.
 * @return an array of strings
 */
public static String[] parseList(String list, String delim) {
    List result = new ArrayList();
    StringTokenizer tokenizer = new StringTokenizer(list, delim);
    while (tokenizer.hasMoreTokens()) {
        result.add(tokenizer.nextToken());
    }
    return (String[]) result.toArray(new String[0]);
}

From source file:edu.illinois.cs.cogcomp.wikifier.utils.io.InFile.java

public static List<String> tokenize(String s) {
     if (s == null)
         return null;
     List<String> res = new ArrayList<String>();
     StringTokenizer st = new StringTokenizer(s, " \n\t\r");
     while (st.hasMoreTokens())
         res.add(st.nextToken());/*from ww  w. ja v a  2s .  com*/
     return res;
 }

From source file:Main.java

/**
 * /*  w  w w . j av  a 2  s .  co  m*/
 * @param value
 * @return double[]
 */
public static double[] toDoubleArray(final String value) {
    if (value == null) {
        return new double[] {};
    }

    final int BRACKET_LENGTH = 1;
    final String strippedValue = value.substring(BRACKET_LENGTH, value.length() - BRACKET_LENGTH);
    final StringTokenizer tokenizer = new StringTokenizer(strippedValue, ELEMENT_SEPARATOR);
    final Collection<Double> doubleCollection = new ArrayList<>();

    while (tokenizer.hasMoreTokens()) {
        doubleCollection.add(Double.valueOf(tokenizer.nextToken().trim()));
    }

    return toDoubleArray(doubleCollection);
}