List of usage examples for java.lang String String
public String(byte bytes[], int offset, int length)
From source file:Main.java
public static void main(String[] atgs) throws Exception { DataOutputStream out = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("java.txt"))); out.writeUTF(""); out.writeBytes("a"); out.writeChars("aaa"); out.close();/*from ww w.j av a2s. c o m*/ DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("java.txt"))); System.out.println(in.readUTF()); byte[] buf = new byte[1024]; int len = in.read(buf); System.out.println(new String(buf, 0, len)); in.close(); }
From source file:Main.java
public static void main(String[] args) throws Exception { int mcPort = 12345; String mcIPStr = "230.1.1.1"; MulticastSocket mcSocket = null; InetAddress mcIPAddress = null; mcIPAddress = InetAddress.getByName(mcIPStr); mcSocket = new MulticastSocket(mcPort); System.out.println("Multicast Receiver running at:" + mcSocket.getLocalSocketAddress()); mcSocket.joinGroup(mcIPAddress);/* w ww. ja v a 2 s . co m*/ DatagramPacket packet = new DatagramPacket(new byte[1024], 1024); System.out.println("Waiting for a multicast message..."); mcSocket.receive(packet); String msg = new String(packet.getData(), packet.getOffset(), packet.getLength()); System.out.println("[Multicast Receiver] Received:" + msg); mcSocket.leaveGroup(mcIPAddress); mcSocket.close(); }
From source file:Main.java
public static void main(String args[]) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { System.out.println(qName); }/*from w ww . j av a 2 s .c o m*/ public void characters(char ch[], int start, int length) throws SAXException { System.out.println(new String(ch, start, length)); } }; saxParser.parse(args[0], handler); }
From source file:UDPReceive.java
public static void main(String args[]) { try {/*from ww w . j a va 2 s .co m*/ int port = 90; // Create a socket to listen on the port. DatagramSocket dsocket = new DatagramSocket(port); // Create a buffer to read datagrams into. If a // packet is larger than this buffer, the // excess will simply be discarded! byte[] buffer = new byte[2048]; // Create a packet to receive data into the buffer DatagramPacket packet = new DatagramPacket(buffer, buffer.length); // Now loop forever, waiting to receive packets and printing them. while (true) { // Wait to receive a datagram dsocket.receive(packet); // Convert the contents to a string, and display them String msg = new String(buffer, 0, packet.getLength()); System.out.println(packet.getAddress().getHostName() + ": " + msg); // Reset the length of the packet before reusing it. packet.setLength(buffer.length); } } catch (Exception e) { System.err.println(e); } }
From source file:Main.java
public static void main(String[] args) throws Exception { SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); parser.parse(new File("test.xml"), new DefaultHandler() { @Override//w w w . jav a 2 s . co m public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { if (qName.equals("passenger")) { System.out.println("id = " + atts.getValue(0)); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { } @Override public void characters(char[] ch, int start, int length) throws SAXException { String text = new String(ch, start, length); if (!text.trim().isEmpty()) { System.out.println("name " + text); } } }); }
From source file:ChatClient.java
public static void main(String[] args) throws Exception { DatagramSocket s = new DatagramSocket(); byte[] buf = new byte[1000]; DatagramPacket dp = new DatagramPacket(buf, buf.length); InetAddress hostAddress = InetAddress.getByName("localhost"); while (true) {// ww w .java2 s . c om BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String outMessage = stdin.readLine(); if (outMessage.equals("bye")) break; String outString = "Client say: " + outMessage; buf = outString.getBytes(); DatagramPacket out = new DatagramPacket(buf, buf.length, hostAddress, 9999); s.send(out); s.receive(dp); String rcvd = "rcvd from " + dp.getAddress() + ", " + dp.getPort() + ": " + new String(dp.getData(), 0, dp.getLength()); System.out.println(rcvd); } }
From source file:MainClass.java
public static void main(String args[]) { try {/*from w w w . j a v a 2 s. c o m*/ SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean name = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("NAME")) { name = true; } } public void characters(char ch[], int start, int length) throws SAXException { if (name) { System.out.println("Name: " + new String(ch, start, length)); name = false; } } }; saxParser.parse(new InputSource(new StringReader(xmlString)), handler); } catch (Exception e) { e.printStackTrace(); } }
From source file:NameLister.java
public static void main(String args[]) { if (args.length != 1) { System.err.println("Usage: java NameLister xmlfile.xml"); System.exit(-1);//from ww w . ja v a 2 s.c o m } try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() { boolean name = false; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (qName.equalsIgnoreCase("NAME")) { name = true; } } public void characters(char ch[], int start, int length) throws SAXException { if (name) { System.out.println("Name: " + new String(ch, start, length)); name = false; } } }; saxParser.parse(args[0], handler); } catch (Exception e) { e.printStackTrace(); } }
From source file:ChatClient.java
public static void main(String[] args) throws Exception { int PORT = 4000; byte[] buf = new byte[1000]; DatagramPacket dgp = new DatagramPacket(buf, buf.length); DatagramSocket sk;/*w ww.j av a 2 s.c om*/ sk = new DatagramSocket(PORT); System.out.println("Server started"); while (true) { sk.receive(dgp); String rcvd = new String(dgp.getData(), 0, dgp.getLength()) + ", from address: " + dgp.getAddress() + ", port: " + dgp.getPort(); System.out.println(rcvd); BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in)); String outMessage = stdin.readLine(); buf = ("Server say: " + outMessage).getBytes(); DatagramPacket out = new DatagramPacket(buf, buf.length, dgp.getAddress(), dgp.getPort()); sk.send(out); } }
From source file:test1.ApacheHttpRestClient2.java
public final static void main(String[] args) { HttpClient httpClient = new DefaultHttpClient(); try {/*from w w w . j a v a 2 s. c o m*/ // this ona api call returns results in a JSON format HttpGet httpGetRequest = new HttpGet("https://api.ona.io/api/v1/users"); // Execute HTTP request HttpResponse httpResponse = httpClient.execute(httpGetRequest); System.out.println("------------------HTTP RESPONSE----------------------"); System.out.println(httpResponse.getStatusLine()); System.out.println("------------------HTTP RESPONSE----------------------"); // Get hold of the response entity HttpEntity entity = httpResponse.getEntity(); // If the response does not enclose an entity, there is no need // to bother about connection release byte[] buffer = new byte[1024]; if (entity != null) { InputStream inputStream = entity.getContent(); try { int bytesRead = 0; BufferedInputStream bis = new BufferedInputStream(inputStream); while ((bytesRead = bis.read(buffer)) != -1) { String chunk = new String(buffer, 0, bytesRead); FileWriter file = new FileWriter("C:\\Users\\fred\\Desktop\\webicons\\output.txt"); //file.write(chunk.toJSONString()); file.write(chunk.toCharArray()); file.flush(); file.close(); System.out.print(chunk); System.out.println(chunk); } } catch (IOException ioException) { // In case of an IOException the connection will be released // back to the connection manager automatically ioException.printStackTrace(); } catch (RuntimeException runtimeException) { // In case of an unexpected exception you may want to abort // the HTTP request in order to shut down the underlying // connection immediately. httpGetRequest.abort(); runtimeException.printStackTrace(); } // try { // FileWriter file = new FileWriter("C:\\Users\\fred\\Desktop\\webicons\\output.json"); // file.write(bis.toJSONString()); // file.flush(); // file.close(); // // System.out.print(bis); // } catch (Exception e) { // } finally { // Closing the input stream will trigger connection release try { inputStream.close(); } catch (Exception ignore) { } } } } catch (ClientProtocolException e) { // thrown by httpClient.execute(httpGetRequest) e.printStackTrace(); } catch (IOException e) { // thrown by entity.getContent(); e.printStackTrace(); } finally { // When HttpClient instance is no longer needed, // shut down the connection manager to ensure // immediate deallocation of all system resources httpClient.getConnectionManager().shutdown(); } }