Example usage for java.io BufferedReader read

List of usage examples for java.io BufferedReader read

Introduction

In this page you can find the example usage for java.io BufferedReader read.

Prototype

public int read() throws IOException 

Source Link

Document

Reads a single character.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("c:/test.txt");

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    int value = 0;
    while ((value = br.read()) != -1) {
        char c = (char) value;
        System.out.println(c);//from  w  w w.j  a  v  a 2s . co m
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("c:/test.txt");

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    System.out.println((char) br.read());
    System.out.println((char) br.read());

    br.mark(26);//from  www.  ja  v  a2 s. c o m
    System.out.println("mark() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

    br.reset();
    System.out.println("reset() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    InputStream is = new FileInputStream("c:/test.txt");

    InputStreamReader isr = new InputStreamReader(is);

    BufferedReader br = new BufferedReader(isr);

    br.close();/*  ww  w  .j av a 2  s . co  m*/

    // creates error
    br.read();

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    Document document = new Document(PageSize.A4);
    PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("2.pdf"));
    document.open();//from  w  ww .j  a v a2  s  .c  o m
    PdfContentByte cb = writer.getDirectContent();
    StringBuffer sb = new StringBuffer(1024);
    BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
    int c;
    while ((c = reader.read()) > -1) {
        sb.append((char) c);
    }
    reader.close();
    ColumnText ct = new ColumnText(cb);
    ct.setSimpleColumn(new Phrase(sb.toString()), 36, 36, PageSize.A4.width() - 36, PageSize.A4.height() - 36,
            18, Element.ALIGN_JUSTIFIED);
    int status = ColumnText.START_COLUMN;
    while (ColumnText.hasMoreText(status)) {
        status = ct.go();
        ct.setYLine(PageSize.A4.height() - 36);
        document.newPage();
    }
    document.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String s = "from java2s.com";

    StringReader sr = new StringReader(s);

    BufferedReader br = new BufferedReader(sr);

    // reads and prints BufferedReader
    System.out.println((char) br.read());
    System.out.println((char) br.read());

    // mark invoked at this position
    br.mark(0);/*  w  ww . ja  v  a 2s .  co  m*/
    System.out.println("mark() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

    // reset() repositioned the stream to the mark
    br.reset();
    System.out.println("reset() invoked");
    System.out.println((char) br.read());
    System.out.println((char) br.read());

}

From source file:Main.java

public static void main(String[] args) throws Exception {

    String s = "from java2s.com";

    // create and assign a new string reader
    StringReader sr = new StringReader(s);

    // create new buffered reader
    BufferedReader br = new BufferedReader(sr);

    // reads and prints BufferedReader
    int value = 0;
    while ((value = br.read()) != -1) {
        // skips a character
        br.skip(1);//from  ww  w .  ja va  2  s  .  c om
        System.out.print((char) value);
    }

}

From source file:BRRead.java

public static void main(String args[]) throws IOException {
    char c;//w  ww. j  a  va 2s.co  m
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter characters, 'q' to quit.");
    do {
        c = (char) br.read();
        System.out.println(c);
    } while (c != 'q');
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedReader br = new BufferedReader(new FileReader("in.txt"));
    BufferedWriter bw = new BufferedWriter(new FileWriter("out.txt"));
    int i;//from ww w  .j ava2  s.  c om
    do {
        i = br.read();
        if (i != -1) {
            if (Character.isLowerCase((char) i))
                bw.write(Character.toUpperCase((char) i));
            else if (Character.isUpperCase((char) i))
                bw.write(Character.toLowerCase((char) i));
            else
                bw.write((char) i);
        }
    } while (i != -1);
    br.close();
    bw.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");

    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;/*ww w .j a  v  a2  s.  co  m*/
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();

    System.out.println("Checksum: " + csum.getChecksum().getValue());

    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();

}

From source file:ZipCompress.java

public static void main(String[] args) throws IOException {
    FileOutputStream f = new FileOutputStream("test.zip");
    CheckedOutputStream csum = new CheckedOutputStream(f, new Adler32());
    ZipOutputStream zos = new ZipOutputStream(csum);
    BufferedOutputStream out = new BufferedOutputStream(zos);
    zos.setComment("A test of Java Zipping");
    // No corresponding getComment(), though.
    for (int i = 0; i < args.length; i++) {
        System.out.println("Writing file " + args[i]);
        BufferedReader in = new BufferedReader(new FileReader(args[i]));
        zos.putNextEntry(new ZipEntry(args[i]));
        int c;//from  w  w  w  . jav a 2 s. co m
        while ((c = in.read()) != -1)
            out.write(c);
        in.close();
    }
    out.close();
    // Checksum valid only after the file has been closed!
    System.out.println("Checksum: " + csum.getChecksum().getValue());
    // Now extract the files:
    System.out.println("Reading file");
    FileInputStream fi = new FileInputStream("test.zip");
    CheckedInputStream csumi = new CheckedInputStream(fi, new Adler32());
    ZipInputStream in2 = new ZipInputStream(csumi);
    BufferedInputStream bis = new BufferedInputStream(in2);
    ZipEntry ze;
    while ((ze = in2.getNextEntry()) != null) {
        System.out.println("Reading file " + ze);
        int x;
        while ((x = bis.read()) != -1)
            System.out.write(x);
    }
    System.out.println("Checksum: " + csumi.getChecksum().getValue());
    bis.close();
    // Alternative way to open and read zip files:
    ZipFile zf = new ZipFile("test.zip");
    Enumeration e = zf.entries();
    while (e.hasMoreElements()) {
        ZipEntry ze2 = (ZipEntry) e.nextElement();
        System.out.println("File: " + ze2);
        // ... and extract the data as before
    }
}