Example usage for java.io FileInputStream FileInputStream

List of usage examples for java.io FileInputStream FileInputStream

Introduction

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

Prototype

public FileInputStream(FileDescriptor fdObj) 

Source Link

Document

Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.

Usage

From source file:Main.java

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

    InputStream is = new FileInputStream("c:\\test.txt");

    DataInputStream dis = new DataInputStream(is);

    int count = is.available();

    byte[] bs = new byte[count];

    dis.read(bs, 4, 3);//  w w w  .  j a va  2  s .co  m

    for (byte b : bs) {
        char c = (char) b;

        if (b == 0) {
            c = '0';
        }
        System.out.print(c);
    }
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    // new input stream reader is created
    FileInputStream fis = new FileInputStream("C:/test.txt");
    InputStreamReader isr = new InputStreamReader(fis);

    // the name of the character encoding returned
    String s = isr.getEncoding();
    System.out.print("Character Encoding: " + s);
    isr.close();/*from   w  ww. j a v a 2  s .  com*/
}

From source file:FileInputOutputExample.java

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

    InputStream is = new FileInputStream("in.txt");
    OutputStream os = new FileOutputStream("out.txt");
    int c;//from  ww w.  j a v  a2  s.c  o m
    while ((c = is.read()) != -1) {
        System.out.print((char) c);
        os.write(c);
    }
    is.close();
    os.close();

}

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   w ww  .j  a va2  s  .  co 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:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
    while (ib.hasRemaining())
        ib.get();// w w w. j  a v  a 2s. c om
    fc.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    byte[] buffer = new byte[1024];
    BufferedInputStream bufferedInput = new BufferedInputStream(new FileInputStream("filename.txt"));

    int bytesRead = 0;
    while ((bytesRead = bufferedInput.read(buffer)) != -1) {
        String chunk = new String(buffer, 0, bytesRead);
        System.out.print(chunk);//from www.  j a va 2s . c  o m
    }
    bufferedInput.close();
}

From source file:Main.java

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

    InputStream inStream = new FileInputStream("c:/test.txt");

    BufferedInputStream bis = new BufferedInputStream(inStream);

    // read until a single byte is available
    while (bis.available() > 0) {
        // get the number of bytes available
        Integer nBytes = bis.available();
        System.out.println("Available bytes = " + nBytes);

        // read next available character
        char ch = (char) bis.read();

        // print the read character.
        System.out.println("The character read = " + ch);
    }/*from ww  w  . j a  va 2  s .c om*/

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = new File("C:/ReadFile.txt");
    FileInputStream fin = new FileInputStream(file);
    BufferedInputStream bin = new BufferedInputStream(fin);

    byte[] contents = new byte[1024];
    int bytesRead = 0;
    String strFileContents;// ww w  .ja  v a  2s. c o  m
    while ((bytesRead = bin.read(contents)) != -1) {
        strFileContents = new String(contents, 0, bytesRead);
        System.out.print(strFileContents);
    }
    bin.close();
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocate(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();/*from   ww  w . j  a  va2  s.  c  o  m*/
        outc.write(bb);
        bb.clear();
    }
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocateDirect(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();//from   w  w  w.j a  va  2 s  .  c  o m
        outc.write(bb);
        bb.clear();
    }
}