Example usage for java.lang String String

List of usage examples for java.lang String String

Introduction

In this page you can find the example usage for java.lang String String.

Prototype

public String(StringBuilder builder) 

Source Link

Document

Allocates a new string that contains the sequence of characters currently contained in the string builder argument.

Usage

From source file:MainClass.java

public static void main(String[] args) {

    String phrase = new String("www.java2s.com API \n");
    File aFile = new File("test.txt");

    FileOutputStream file = null;
    try {/*from w  w w  . ja  v a  2 s.c  o m*/
        file = new FileOutputStream(aFile, true);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel outChannel = file.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(phrase.length());
    byte[] bytes = phrase.getBytes();
    buf.put(bytes);
    buf.flip();

    try {
        outChannel.write(buf);
        file.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:Base64Encode.java

public static void main(String[] args) {
    String hello = "Hello World";

    byte[] encoded = Base64.encodeBase64(hello.getBytes());

    System.out.println(Arrays.toString(encoded));

    String encodedString = new String(encoded);
    System.out.println(hello + " = " + encodedString);
}

From source file:MainClass.java

public static void main(String args[]) {

    Vector vector = new Vector();
    vector.addElement(new Integer(5));
    vector.addElement(new Float(-14.14f));
    vector.addElement(new String("Hello"));

    Enumeration e = vector.elements();
    while (e.hasMoreElements()) {
        Object obj = e.nextElement();
        System.out.println(obj);// ww w  .j ava2  s . c  o m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("C:/String.txt");
    FileInputStream fin = new FileInputStream(file);

    byte fileContent[] = new byte[(int) file.length()];
    fin.read(fileContent);//from w  ww .j a  va  2s  .  c  o m
    System.out.println(new String(fileContent));
}

From source file:Login.java

public static void main(String[] args) throws Exception {
    Console console = System.console();
    String username = console.readLine("Username:");
    char[] pwd = console.readPassword("Password:");

    System.out.println("Username = " + username);
    System.out.println("Password = " + new String(pwd));

    username = "";
    for (int i = 0; i < pwd.length; i++)
        pwd[i] = 0;/*from  ww w .  j ava 2 s . c  o m*/
}

From source file:MainClass.java

public static void main(String[] args) throws ClassNotFoundException, SQLException {
    Console console = System.console();
    if (console == null) {
        System.err.println("sales: unable to obtain console");
        return;/* w w  w . j a v a2s  .  com*/
    }

    String password = new String(console.readPassword("Enter password: "));
    System.out.println(password);
}

From source file:Main.java

public static void main(String[] args) {

    try {//from   w ww .  j ava 2  s  .c  o  m
        String str1 = "java2s.com";
        System.out.println("string1 = " + str1);
        // copy the contents of the String to a byte array
        byte[] arr = str1.getBytes(Charset.forName("ASCII"));

        String str2 = new String(arr);
        System.out.println("new string = " + str2);
    } catch (Exception e) {
        System.out.print(e.toString());
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    Vector vector = new Vector();
    vector.addElement(new Integer(5));
    vector.addElement(new Float(-14.14f));

    System.out.println(vector);//  w  w  w  .  ja v  a2 s. co m

    String s = new String("String to be inserted");
    vector.insertElementAt(s, 1);
    System.out.println(vector);

    vector.removeElementAt(2);
    System.out.println(vector);
}

From source file:Main.java

public static void main(String[] args) throws NoSuchAlgorithmException {
    int numBytes = (new Integer("1111")).intValue();

    SecureRandom srand = new SecureRandom(new byte[] { 1, 2, 3, 4 });

    byte[] bytes = new byte[numBytes];
    srand.nextBytes(bytes);/*from w  ww. j  a  v a 2 s. c  om*/
    System.out.println(new String(bytes));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] ary = new byte[128];
    DatagramPacket pack = new DatagramPacket(ary, 128);

    // read/*w  ww .  j  av  a  2s  .c om*/
    DatagramSocket sock = new DatagramSocket(1111);
    sock.receive(pack);
    String word = new String(pack.getData());
    System.out.println("From: " + pack.getAddress() + " Port: " + pack.getPort());
    System.out.println(word);
    sock.close();
    // write
    sock = new DatagramSocket();
    pack.setAddress(InetAddress.getByName(args[1]));
    pack.setData(args[2].getBytes());
    pack.setPort(1111);
    sock.send(pack);
    sock.close();

}