Example usage for java.io FileInputStream read

List of usage examples for java.io FileInputStream read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of data from this input stream.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");
    FileInputStream fis1 = new FileInputStream(file1);
    FileInputStream fis2 = new FileInputStream(file2);
    int i1 = fis1.read();
    int i2 = fis2.read();
    while (i1 != -1) {
        if (i1 != i2) {
            areFilesIdentical = false;//w  ww . j  a  va  2 s  .c o m
            break;
        }
        i1 = fis1.read();
        i2 = fis2.read();
    }
    fis1.close();
    fis2.close();
    System.out.println(areFilesIdentical);
}

From source file:Main.java

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

    byte b = 66;/*from   w  w w  . j a  v  a2s. c o m*/
    int i = 0;
    FileOutputStream fos = new FileOutputStream(new File("C://test1.txt"));

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:Main.java

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

    byte[] b = { 65, 66, 67, 68, 69 };
    int i = 0;/*from  w w w.  j a  v  a 2 s  . c  om*/

    FileOutputStream fos = new FileOutputStream("C://test.txt");

    // writes byte to the output stream
    fos.write(b, 2, 3);

    // flushes the content to the underlying stream
    fos.flush();

    // create new file input stream
    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;
        System.out.print(c);
    }
}

From source file:Main.java

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

    byte b = 66;//from w  w w . j av  a  2 s  .  c  o  m
    int i = 0;
    FileOutputStream fos = new FileOutputStream(FileDescriptor.out);

    fos.write(b);

    fos.flush();

    FileInputStream fis = new FileInputStream("C://test.txt");

    // read till the end of the file
    while ((i = fis.read()) != -1) {
        // convert integer to character
        char c = (char) i;

        System.out.print(c);
    }
    fos.close();
    fis.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileInputStream fis = new FileInputStream("/home/username/data.txt");

    int i = 0;//from w  w  w  .  j  a  v a  2 s . c  om

    int count = 0;

    while ((i = fis.read()) != -1) {
        if (i != -1) {
            System.out.printf("%02X ", i);
            count++;
        }

        if (count == 16) {
            System.out.println("");
            count = 0;
        }
    }
    fis.close();
}

From source file:CopyBytes.java

public static void main(String[] args) throws IOException {
    File inputFile = new File("input.txt");
    File outputFile = new File("output.txt");

    FileInputStream in = new FileInputStream(inputFile);
    FileOutputStream out = new FileOutputStream(outputFile);
    int c;/*from w  w w.  j a v a 2 s .c o  m*/

    while ((c = in.read()) != -1)
        out.write(c);

    in.close();
    out.close();
}

From source file:Main.java

public static void main(String[] args) {
    File file = new File(args[0]);
    if (!file.exists()) {
        System.out.println(args[0] + " does not exist.");
        return;/*from www  .  j  av a2 s  .  c  om*/
    }
    if (!(file.isFile() && file.canRead())) {
        System.out.println(file.getName() + " cannot be read from.");
        return;
    }
    try {
        FileInputStream fis = new FileInputStream(file);
        char current;
        while (fis.available() > 0) {
            current = (char) fis.read();
            System.out.print(current);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");
    if (!file1.exists() || !file2.exists()) {
        System.out.println("One or both files do not exist");
        System.out.println(false);
    }/*from   w  w w.  jav a 2 s  .  co m*/
    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
        System.out.println("lengths not equal");
        System.out.println(false);
    }
    try {
        FileInputStream fis1 = new FileInputStream(file1);
        FileInputStream fis2 = new FileInputStream(file2);
        int i1 = fis1.read();
        int i2 = fis2.read();
        while (i1 != -1) {
            if (i1 != i2) {
                areFilesIdentical = false;
                break;
            }
            i1 = fis1.read();
            i2 = fis2.read();
        }
        fis1.close();
        fis2.close();
    } catch (IOException e) {
        System.out.println("IO exception");
        areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("C://test.txt");

    // skip bytes from file input stream
    fis.skip(4);//from w  w  w .  j av a2 s.c  o  m

    // read bytes from this stream
    int i = fis.read();

    // converts integer to character
    char c = (char) i;
    System.out.print("Character read: " + c);
}

From source file:Main.java

public static void main(String[] args) {
    boolean areFilesIdentical = true;
    File file1 = new File("c:\\file1.txt");
    File file2 = new File("c:\\file2.txt");

    if (!file1.exists() || !file2.exists()) {
        System.out.println("One or both files do not exist");
        System.out.println(false);
    }//from w  ww  .j  a  v  a  2s .com

    System.out.println("length:" + file1.length());
    if (file1.length() != file2.length()) {
        System.out.println("lengths not equal");
        System.out.println(false);
    }

    try {
        FileInputStream fis1 = new FileInputStream(file1);
        FileInputStream fis2 = new FileInputStream(file2);
        int i1 = fis1.read();
        int i2 = fis2.read();
        while (i1 != -1) {
            if (i1 != i2) {
                areFilesIdentical = false;
                break;
            }
            i1 = fis1.read();
            i2 = fis2.read();
        }
        fis1.close();
        fis2.close();
    } catch (IOException e) {
        System.out.println("IO exception");
        areFilesIdentical = false;
    }
    System.out.println(areFilesIdentical);
}