Example usage for java.lang String getBytes

List of usage examples for java.lang String getBytes

Introduction

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

Prototype

public byte[] getBytes() 

Source Link

Document

Encodes this String into a sequence of bytes using the platform's default charset, storing the result into a new byte array.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramChannel server = DatagramChannel.open();
    server.bind(null);/* w  ww .  j a va2  s  . co m*/
    NetworkInterface interf = NetworkInterface.getByName(MULTICAST_INTERFACE_NAME);
    server.setOption(StandardSocketOptions.IP_MULTICAST_IF, interf);

    String msg = "Hello!";
    ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
    InetSocketAddress group = new InetSocketAddress(MULTICAST_IP, MULTICAST_PORT);

    server.send(buffer, group);
    System.out.println("Sent the   multicast  message: " + msg);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String strFilePath = "C://demo.txt";
    FileOutputStream fos = new FileOutputStream(strFilePath);
    String strContent = "Write File using Java FileOutputStream example !";
    fos.write(strContent.getBytes());
    fos.close();/*from  w ww .  java2s . co m*/
}

From source file:PushbackInputStreamDemo.java

public static void main(String args[]) throws IOException {
    String s = "if (a == 4) a = 0;\n";
    byte buf[] = s.getBytes();
    ByteArrayInputStream in = new ByteArrayInputStream(buf);
    PushbackInputStream f = new PushbackInputStream(in);
    int c;//from w w  w.j a v  a  2 s. c  om

    while ((c = f.read()) != -1) {
        switch (c) {
        case '=':
            if ((c = f.read()) == '=')
                System.out.print(".eq.");
            else {
                System.out.print("<-");
                f.unread(c);
            }
            break;
        default:
            System.out.print((char) c);
            break;
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    String str = "java2s.com";
    ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
    System.setIn(bais);/*from  w w  w .ja v a2 s. c  o  m*/

    Scanner scanner = new Scanner(System.in);
    String input = scanner.next();
    System.out.println(input);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramChannel client = null;
    client = DatagramChannel.open();

    client.bind(null);/* w  w w.j a  va 2s .com*/

    String msg = "Hello";
    ByteBuffer buffer = ByteBuffer.wrap(msg.getBytes());
    InetSocketAddress serverAddress = new InetSocketAddress("localhost", 8989);

    client.send(buffer, serverAddress);
    buffer.clear();
    client.receive(buffer);
    buffer.flip();
    int limits = buffer.limit();
    byte bytes[] = new byte[limits];
    buffer.get(bytes, 0, limits);
    String response = new String(bytes);
    System.out.println("Server  responded: " + response);
    client.close();
}

From source file:J2METimeServer.java

public static void main(String[] args) {

    try {// w w w  . j av  a 2s. c om
        Calendar cal = Calendar.getInstance();
        DatagramConnection receiver = (DatagramConnection) Connector.open("datagram://");
        byte[] buffer = new byte[256];
        Datagram dgram = receiver.newDatagram(buffer, buffer.length);
        for (;;) {
            dgram.setLength(buffer.length);
            receiver.receive(dgram);
            cal.setTime(new Date());
            String time = cal.toString();
            byte[] dataBytes = time.getBytes();
            System.arraycopy(dataBytes, 0, buffer, 0, dataBytes.length);
            dgram.setLength(dataBytes.length);
            receiver.send(dgram);
        }
    } catch (IOException ex) {
        System.out.println("IOException: " + ex);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);/*w ww  .j ava2 s . c  o m*/
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getBytes("name").length + "   ");
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);/*w w  w.  j  a  v  a  2s.c o m*/
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.println(rs.getByte("id"));
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);/*from  w  w w  .j a  va2  s .c o  m*/
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getByte(1));
    }
    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();
    stmt.executeUpdate("create table survey (id int, name BINARY );");
    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);
    String myData = "some string data ...";
    byte[] binaryData = myData.getBytes();
    pstmt.setBytes(1, binaryData);/*  w  ww .  j av a 2 s  .  c o m*/
    pstmt.executeUpdate();

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getBytes(2).length + "   ");
    }
    rs.close();
    stmt.close();
    conn.close();
}