Monday 7 May 2012

Java String getBytes(Charset charset) Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
String class getBytes(Charset charset) method example

String class getBytes(Charset charset) method example. 
This example shows you how to use getBytes(Charset charset) method.This method Encodes this String into a sequence of bytes using the given charset, storing the result into a new byte array. 


Here is the Code:-
 /**
 * @(#) GetBytes1string.java
 *Program that Convert this String into bytes according to the specified character encoding, storing the result into a new byte array.
 */
public class GetBytes1string
{
    public static void main(String[] args)
    {
    try
    {
         String str = "Welcome to RoseIndia.";
            // Copy the contents of the String to a byte array using the ASCII encoding.
            byte[] arr = str.getBytes("ASCII");
            // Create a new String using the contents of the byte array.
            String newStr = new String(arr);
            // Display the contents of the byte array.
            System.out.println("The new String equals \"" +newStr + "\"");
      }
    catch (Exception ex)
    {
    }
    }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...