Example usage for java.io IOException printStackTrace

List of usage examples for java.io IOException printStackTrace

Introduction

In this page you can find the example usage for java.io IOException printStackTrace.

Prototype

public void printStackTrace() 

Source Link

Document

Prints this throwable and its backtrace to the standard error stream.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from  ww w  .  jav  a2 s .c  o  m

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("Hello World from java2s.com");

        // set the file pointer at 0 position
        raf.seek(0);

        // print the byte
        System.out.println(raf.readUnsignedByte());

        // set the file pointer at 7 position
        raf.seek(7);

        System.out.println(raf.readUnsignedByte());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String serverName = args[0];//from  ww  w. j av  a 2  s .c  om
    int port = Integer.parseInt(args[1]);

    try {
        System.out.println("Connecting to " + serverName + " on port " + port);
        Socket client = new Socket(serverName, port);

        System.out.println("Just connected to " + client.getRemoteSocketAddress());

        OutputStream outToServer = client.getOutputStream();
        DataOutputStream out = new DataOutputStream(outToServer);
        out.writeUTF("Hello from " + client.getLocalSocketAddress());

        InputStream inFromServer = client.getInputStream();
        DataInputStream in = new DataInputStream(inFromServer);
        System.out.println("Server says " + in.readUTF());

        client.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    // create a new PushBack reader based on our string reader
    PushbackReader pr = new PushbackReader(sr, 20);

    try {/* w  ww.ja  v a 2  s  . c om*/
        // read the first five chars
        for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.println(c);

            // skip a character every time
            pr.skip(1);
        }

        pr.close();

    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {

    PipedWriter writer = new PipedWriter();
    PipedReader reader = new PipedReader();

    try {//from w  w  w  . j  a va  2s.c  o  m
        // connect the reader and the writer
        writer.connect(reader);

        writer.write(70);
        writer.write(71);

        // flush the writer
        writer.flush();

        // print what we wrote
        for (int i = 0; i < 2; i++) {
            System.out.println((char) reader.read());
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from w  w  w.jav  a 2 s  .c  om
        double d = 1234.5678;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeDouble(d);

        raf.seek(0);

        System.out.println(raf.readDouble());

        raf.seek(0);

        raf.writeDouble(123.4567);

        raf.seek(0);

        System.out.println(raf.readDouble());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from w w  w.j  a  va  2s  .c  o m
        short s = 15;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeShort(s);

        raf.seek(0);

        System.out.println(raf.readShort());

        raf.seek(0);

        raf.writeShort(20);

        raf.seek(0);

        System.out.println(raf.readShort());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//w ww. j  a  v  a2  s. c  o  m

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("java2s.com Hello World");

        // set the file pointer at 0 position
        raf.seek(0);

        // read the first byte and print it
        System.out.println(raf.read());

        // set the file pointer at 4rth position
        raf.seek(4);

        // read the first byte and print it
        System.out.println(raf.read());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {// w w w  . j a v  a2  s. c o m
        float f = 1234.5678f;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeFloat(f);

        raf.seek(0);

        System.out.println(raf.readFloat());

        raf.seek(0);

        raf.writeFloat(123.4567f);

        raf.seek(0);

        System.out.println(raf.readFloat());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("test.txt");

    try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.READ)) {
        int fileSize = (int) afc.size();
        ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize);

        Future<Integer> result = afc.read(dataBuffer, 0);
        int readBytes = result.get();

        System.out.format("%s bytes read   from  %s%n", readBytes, path);
        System.out.format("Read data is:%n");

        byte[] byteData = dataBuffer.array();
        Charset cs = Charset.forName("UTF-8");
        String data = new String(byteData, cs);

        System.out.println(data);
    } catch (IOException ex) {
        ex.printStackTrace();
    }/*from   w  w  w  .  jav  a 2  s  .c om*/
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  w w.j a  v  a 2s. com*/
        long f = 123456789909876L;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeLong(f);

        raf.seek(0);

        System.out.println(raf.readLong());

        raf.seek(0);

        raf.writeLong(20000000000l);

        raf.seek(0);

        System.out.println(raf.readLong());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}