List of usage examples for java.lang String String
public String(StringBuilder builder)
From source file:Main.java
public static void main(String[] args) { try {// w w w . j a v a 2 s. c o m String s = "Hello world from java2s.com"; RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw"); // write something in the file raf.writeUTF(s); // set the file pointer at 0 position raf.seek(0); // create an array equal to the length of raf byte[] arr = new byte[(int) raf.length()]; // read the file raf.readFully(arr); // create a new string based on arr String s2 = new String(arr); System.out.println(s2); raf.close(); } catch (IOException ex) { ex.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { // main method // Encode a String into bytes String inputString = "this is a test"; byte[] input = inputString.getBytes("UTF-8"); // Compress the bytes byte[] output1 = new byte[input.length]; Deflater compresser = new Deflater(); compresser.setInput(input);/*from www . j ava 2 s .c om*/ compresser.finish(); int compressedDataLength = compresser.deflate(output1); compresser.end(); String str = new String(Base64.getEncoder().encode(output1)); System.out.println("Deflated String:" + str); byte[] output2 = Base64.getDecoder().decode(str); // Decompress the bytes Inflater decompresser = new Inflater(); decompresser.setInput(output2); byte[] result = str.getBytes(); int resultLength = decompresser.inflate(result); decompresser.end(); // Decode the bytes into a String String outputString = new String(result, 0, resultLength, "UTF-8"); System.out.println("Deflated String:" + outputString); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile = null; String line1 = "line\n"; String line2 = "asdf1234\n"; // read / write permissions randomAccessFile = new RandomAccessFile("yourFile.dat", "rw"); randomAccessFile.writeBytes(line1);//from w w w. j a va 2 s . c om randomAccessFile.writeBytes(line2); // Place the file pointer at the end of the first line randomAccessFile.seek(line1.length()); byte[] buffer = new byte[line2.length()]; randomAccessFile.read(buffer); System.out.println(new String(buffer)); randomAccessFile.close(); }
From source file:Main.java
License:asdf
public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFile = null; String line1 = "java2s.com\n"; String line2 = "asdf1234\n"; // read / write permissions randomAccessFile = new RandomAccessFile("yourFile.dat", "rw"); randomAccessFile.writeBytes(line1);// w ww . ja v a 2 s .com randomAccessFile.writeBytes(line2); // Place the file pointer at the end of the first line randomAccessFile.seek(line1.length()); byte[] buffer = new byte[line2.length()]; randomAccessFile.read(buffer); System.out.println(new String(buffer)); randomAccessFile.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { final List<String> list = new ArrayList<String>(); ParserDelegator parserDelegator = new ParserDelegator(); ParserCallback parserCallback = new ParserCallback() { public void handleText(final char[] data, final int pos) { list.add(new String(data)); }// ww w. java2 s .c o m public void handleStartTag(Tag tag, MutableAttributeSet attribute, int pos) { } public void handleEndTag(Tag t, final int pos) { } public void handleSimpleTag(Tag t, MutableAttributeSet a, final int pos) { } public void handleComment(final char[] data, final int pos) { } public void handleError(final java.lang.String errMsg, final int pos) { } }; parserDelegator.parse(new FileReader("a.html"), parserCallback, true); System.out.println(list); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] keyBytes = new byte[] { 0x01, 0x23, 0x45, 0x67, (byte) 0x89, (byte) 0xab, (byte) 0xcd, (byte) 0xef }; SecretKeySpec key = new SecretKeySpec(keyBytes, "DES"); Cipher cipher = Cipher.getInstance("DES/ECB/PKCS7Padding", "BC"); System.out.println("input : " + new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println("cipher: " + new String(cipherText) + " bytes: " + ctLength); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println("plain : " + new String(plainText) + " bytes: " + ptLength); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "java2s.com".getBytes(); byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); System.out.println(new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println(new String(cipherText)); System.out.println(ctLength); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println(new String(plainText)); System.out.println(ptLength); }
From source file:MainClass.java
public static void main(String[] args) throws Exception { Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); byte[] input = "www.java2s.com".getBytes(); byte[] keyBytes = new byte[] { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17 }; SecretKeySpec key = new SecretKeySpec(keyBytes, "AES"); Cipher cipher = Cipher.getInstance("AES/ECB/PKCS7Padding", "BC"); System.out.println(new String(input)); // encryption pass cipher.init(Cipher.ENCRYPT_MODE, key); byte[] cipherText = new byte[cipher.getOutputSize(input.length)]; int ctLength = cipher.update(input, 0, input.length, cipherText, 0); ctLength += cipher.doFinal(cipherText, ctLength); System.out.println(new String(cipherText)); System.out.println(ctLength); // decryption pass cipher.init(Cipher.DECRYPT_MODE, key); byte[] plainText = new byte[cipher.getOutputSize(ctLength)]; int ptLength = cipher.update(cipherText, 0, ctLength, plainText, 0); ptLength += cipher.doFinal(plainText, ptLength); System.out.println(new String(plainText)); System.out.println(ptLength); }
From source file:MainClass.java
public static void main(String args[]) throws Exception { KeyGenerator kg = KeyGenerator.getInstance("DES"); Cipher c = Cipher.getInstance("DES/CBC/PKCS5Padding"); Key key = kg.generateKey();//from w w w .j a v a 2 s.c o m c.init(Cipher.ENCRYPT_MODE, key); byte input[] = "Stand and unfold yourself".getBytes(); byte encrypted[] = c.doFinal(input); byte iv[] = c.getIV(); IvParameterSpec dps = new IvParameterSpec(iv); c.init(Cipher.DECRYPT_MODE, key, dps); byte output[] = c.doFinal(encrypted); System.out.println(new String(output)); }
From source file:Main.java
public static void main(String args[]) throws IOException { String host = args[0];//from w w w.ja v a 2s .c o m byte message[] = new byte[256]; InetAddress address = InetAddress.getByName(host); System.out.println("Checking at: " + address); DatagramPacket packet = new DatagramPacket(message, message.length, address, DAYTIME_PORT); DatagramSocket socket = new DatagramSocket(); socket.send(packet); packet = new DatagramPacket(message, message.length); socket.receive(packet); String time = new String(packet.getData()); System.out.println(time); socket.close(); }