Java String Encode encode(String name)

Here you can find the source of encode(String name)

Description

encode

License

Apache License

Declaration

public static String encode(String name) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static String encode(String name) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < name.length(); i++) {
            char c = name.charAt(i);
            if (c != '_' && Character.isLetterOrDigit(c))
                sb.append(c);// w  w w .  j  a v  a 2 s .c o  m
            else {
                String esc = String.format("_%02x", (int) c);
                sb.append(esc);
            }
        }
        return sb.toString();
    }

    public static void append(File file, byte[] data) {
        try (FileOutputStream stream = new FileOutputStream(file, true)) {
            stream.write(data);
            stream.getChannel().force(true);
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static void write(File file, byte[] data) {
        try (FileOutputStream stream = new FileOutputStream(file)) {
            stream.write(data);
            stream.getChannel().force(true);
            stream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. encode(final String s, final String encoding)
  2. encode(final String source, final String encoding, BitSet notEncoded)
  3. encode(String data, String encode)
  4. encode(String encoding, String string)
  5. encode(String encoding, String text)
  6. encode(String s)
  7. encode(String s)
  8. encode(String s)
  9. encode(String s, String enc)