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(byte bytes[], int offset, int length) 

Source Link

Document

Constructs a new String by decoding the specified subarray of bytes using the platform's default charset.

Usage

From source file:Main.java

public static void main(String args[]) {
    byte ascii[] = { 65, 66, 67, 68, 69, 70 };

    String s2 = new String(ascii, 2, 3);
    System.out.println(s2);// w w w  . jav  a2  s .co  m
}

From source file:Main.java

public static void main(String[] argv) {
    int[] bytes = new int[] { 65, 66, 67, 68 };
    System.out.println(new String(bytes, 1, 2));

}

From source file:Main.java

public static void main(String args[]) {

    char charArray[] = { 'j', 'a', 'v', 'a', '2', 's', '.', 'c', 'o', 'm' };
    String s4 = new String(charArray, 6, 3);

    System.out.println(s4);//www.j ava  2 s .  c  om
}

From source file:MainClass.java

public static void main(String[] arg) {
    char[] textArray = { 'T', 'o', ' ', 'b', 'e', ' ', 'o', 'r', ' ', 'n', 'o', 't', ' ', 't', 'o', ' ', 'b',
            'e' };

    String text = new String(textArray, 9, 3);

    System.out.println(text);/*from   w w  w . j  a v  a 2 s.c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    DatagramSocket ds = new DatagramSocket(3000);
    byte[] buf = new byte[1024];
    DatagramPacket dp = new DatagramPacket(buf, 1024);
    ds.receive(dp);/* w  w  w  . j  a  v a 2  s  .c om*/
    String strRecv = new String(dp.getData(), 0, dp.getLength()) + " from " + dp.getAddress().getHostAddress()
            + ":" + dp.getPort();
    System.out.println(strRecv);
    ds.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Socket s = new Socket(args[0], 13);
    InputStream is = s.getInputStream();
    while (true) {
        byte b[] = new byte[100];
        int i = is.read(b);
        if (i == -1)
            break;
        System.out.print(new String(b, 0, i));
    }//w  ww  . java 2s  . c o m
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] buffer = new byte[1024];
    BufferedInputStream bufferedInput = new BufferedInputStream(new FileInputStream("filename.txt"));

    int bytesRead = 0;
    while ((bytesRead = bufferedInput.read(buffer)) != -1) {
        String chunk = new String(buffer, 0, bytesRead);
        System.out.print(chunk);//from ww  w.  j  a  v a  2 s  .  com
    }
    bufferedInput.close();
}

From source file:WriteServer.java

public static void main(String args[]) throws Exception {
    int clientPort = 999;

    int buffer_size = 1024;

    byte buffer[] = new byte[buffer_size];
    DatagramSocket ds = new DatagramSocket(clientPort);
    while (true) {
        DatagramPacket p = new DatagramPacket(buffer, buffer.length);
        ds.receive(p);/*from w w  w  .  j  av a2 s  . c om*/
        System.out.println(new String(p.getData(), 0, p.getLength()));
    }
}

From source file:Main.java

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

    byte[] contents = new byte[1024];
    int bytesRead = 0;
    String strFileContents;//from w  ww. ja  v a 2 s. co m
    while ((bytesRead = bin.read(contents)) != -1) {
        strFileContents = new String(contents, 0, bytesRead);
        System.out.print(strFileContents);
    }
    bin.close();
}

From source file:Test.java

public static void main(String[] args) throws Exception {

    final URL test = Test.class.getResource("/");
    System.out.println(test);//  ww w  .j  av  a 2s .  c o  m

    final URL url = Test.class.getResource("/resources/test.txt");
    System.out.println(url);

    if (url == null) {
        System.out.println("URL is null");
    } else {
        final File file = new File(url.toURI());
        final FileReader reader = new FileReader(file);
        final char[] buff = new char[20];
        final int l = reader.read(buff);
        System.out.println(new String(buff, 0, l));
        reader.close();
    }

}