List of usage examples for java.lang String String
public String(StringBuilder builder)
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); System.out.println(text);//from w ww . j a va 2 s .c o m }
From source file:Main.java
public static void main(String args[]) { final Map<String, String> map = new WeakHashMap<String, String>(); map.put(new String("A"), "B"); Runnable runner = new Runnable() { public void run() { while (map.containsKey("A")) { try { Thread.sleep(500); } catch (InterruptedException ignored) { }// w ww. ja v a 2 s.com System.gc(); } } }; Thread t = new Thread(runner); t.start(); try { t.join(); } catch (InterruptedException ignored) { } }
From source file:ACDemo.java
public static void main(String args[]) { System.out.println("a = " + new String(a)); System.out.println("b = " + new String(b)); System.arraycopy(a, 0, b, 0, a.length); System.out.println("a = " + new String(a)); System.out.println("b = " + new String(b)); System.arraycopy(a, 0, a, 1, a.length - 1); System.arraycopy(b, 1, b, 0, b.length - 1); System.out.println("a = " + new String(a)); System.out.println("b = " + new String(b)); }
From source file:Main.java
public static void main(String args[]) { int i = 1;//from ww w. jav a 2 s.c o m float f = 1.0f; long l = 1; double d = 1.0d; char c = 'a'; boolean b = true; Object o = new String("Hello World"); /* convert int to String */ System.out.println(String.valueOf(i)); /* convert float to String */ System.out.println(String.valueOf(f)); /* convert long to String */ System.out.println(String.valueOf(l)); /* convert double to String */ System.out.println(String.valueOf(d)); /* convert char to String */ System.out.println(String.valueOf(c)); /* convert boolean to String */ System.out.println(String.valueOf(b)); /* convert Object to String */ System.out.println(String.valueOf(o)); }
From source file:Main.java
public static void main(String[] args) throws Exception { String phrase = new String("www.java2s.com\n"); File aFile = new File("test.txt"); FileOutputStream outputFile = null; outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); // Load the data into the buffer for (char ch : phrase.toCharArray()) { buf.putChar(ch);/*ww w. j a va2s . c o m*/ } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); }
From source file:Main.java
public static void main(String[] args) { Console console = System.console(); char passwordArray[] = console.readPassword("Enter your secret password: "); console.printf("Password entered was: %s%n", new String(passwordArray)); }
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("text\n"); String dirname = "C:/test"; // Directory name String filename = "byteData.txt"; File aFile = new File(dirname, filename); // Create the file output stream FileOutputStream file = null; try {// w w w. j a va 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:Main.java
public static void main(String[] args) { try {//from w ww . j av a 2 s. c om String str1 = "java2s.com"; System.out.println("string1 = " + str1); // copy the contents of the String to a byte array byte[] arr = str1.getBytes("ASCII"); String str2 = new String(arr); System.out.println("new string = " + str2); } catch (Exception e) { System.out.print(e.toString()); } }
From source file:Main.java
public static void main(String[] args) { String hello = "aaaaaaaa"; byte[] decoded = Base64.decodeBase64(hello.getBytes()); System.out.println(Arrays.toString(decoded)); String decodedString = new String(decoded); System.out.println(hello + " = " + decodedString); }
From source file:MainClass.java
public static void main(String[] args) { String phrase = new String("text \n"); String dirname = "C:/test"; String filename = "charData.txt"; File dir = new File(dirname); File aFile = new File(dir, filename); FileOutputStream outputFile = null; try {/*from w ww . j a v a2 s . co m*/ outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel outChannel = outputFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("New buffer: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); for (char ch : phrase.toCharArray()) { buf.putChar(ch); } System.out.println("Buffer after loading: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); buf.flip(); System.out.println("Buffer after flip: position = " + buf.position() + "\tLimit = " + buf.limit() + "\tcapacity = " + buf.capacity()); try { outChannel.write(buf); outputFile.close(); System.out.println("Buffer contents written to file."); } catch (IOException e) { e.printStackTrace(System.err); } }