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 Exception {

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeUTF("Hello World from java2s.com");
    oout.flush();// w  w  w  . j a v a 2  s  . co  m
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // check how many bytes are available
    System.out.println(ois.available());
    ois.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    short b = 32767;

    FileOutputStream out = new FileOutputStream("test.txt");
    ObjectOutputStream oout = new ObjectOutputStream(out);

    // write something in the file
    oout.writeShort(b);/*w ww.j a  va2 s  .co  m*/
    oout.flush();
    oout.close();
    // create an ObjectInputStream for the file we created before
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    // read and print the short
    System.out.println(ois.readUnsignedShort());
    ois.close();
}

From source file:Main.java

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

    double[] dbuf = { 65.56, 66.89, 67.98, 68.82, 69.55, 70.37 };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");

    DataOutputStream dos = new DataOutputStream(fos);

    for (double d : dbuf) {
        dos.writeDouble(d);//w  ww . j a v  a  2s.  c o m
    }

    dos.flush();

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

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        double c = dis.readDouble();
        System.out.print(c + " ");
    }

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    User a = new User("A", "B");
    System.out.println("logon a = " + a);
    ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("User.out"));
    o.writeObject(a);/*from ww  w .  ja  va 2s.  c o m*/
    o.close();

    Thread.sleep(1000); // Delay for 1 second

    ObjectInputStream in = new ObjectInputStream(new FileInputStream("User.out"));
    System.out.println("Recovering object at " + new Date());
    a = (User) in.readObject();
    System.out.println("logon a = " + a);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*from  w w w  .  j a va  2  s.c  o  m*/
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        FileInputStream fis = new FileInputStream("sometextfile.txt");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement createTable = connection.createStatement();
        createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)");
        String ins = "INSERT INTO source_code VALUES(?,?)";
        PreparedStatement statement = connection.prepareStatement(ins);

        statement.setString(1, "TryInputStream2");
        statement.setAsciiStream(2, fis, fis.available());

        int rowsUpdated = statement.executeUpdate();
        System.out.println("Rows affected: " + rowsUpdated);
        Statement getCode = connection.createStatement();
        ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code");
        BufferedReader reader = null;
        String input = null;

        while (theCode.next()) {
            reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream(2)));
            while ((input = reader.readLine()) != null) {
                System.out.println(input);
            }
        }
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/* w w w  . j  ava2s  .c  om*/
        String url = "jdbc:odbc:yourdatabasename";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";

        FileInputStream fis = new FileInputStream("sometextfile.txt");

        Class.forName(driver);
        Connection connection = DriverManager.getConnection(url, user, password);
        Statement createTable = connection.createStatement();
        createTable.executeUpdate("CREATE TABLE source_code (name char(20), source LONGTEXT)");
        String ins = "INSERT INTO source_code VALUES(?,?)";
        PreparedStatement statement = connection.prepareStatement(ins);

        statement.setString(1, "TryInputStream2");
        statement.setAsciiStream(2, fis, fis.available());

        int rowsUpdated = statement.executeUpdate();
        System.out.println("Rows affected: " + rowsUpdated);
        Statement getCode = connection.createStatement();
        ResultSet theCode = getCode.executeQuery("SELECT name,source FROM source_code");
        BufferedReader reader = null;
        String input = null;

        while (theCode.next()) {
            reader = new BufferedReader(new InputStreamReader(theCode.getAsciiStream("source")));
            while ((input = reader.readLine()) != null) {
                System.out.println(input);
            }
        }
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:MainClass.java

public static void main(String[] a) throws Exception {
    String elements[] = { "A", "B", "C", "D", "E" };
    Set set = new HashSet(Arrays.asList(elements));

    FileOutputStream fos = new FileOutputStream("set.ser");
    ObjectOutputStream oos = new ObjectOutputStream(fos);
    oos.writeObject(set);//  w  ww .  j ava 2  s . co  m
    oos.close();

    FileInputStream fis = new FileInputStream("set.ser");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Set anotherSet = (Set) ois.readObject();
    ois.close();
    System.out.println(anotherSet);
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream("clients.ser"));

    AccountRecordSerializable record;/*from  ww  w  .j ava 2  s .co m*/

    record = new AccountRecordSerializable(1, "firstName", "lastName", 0.1);
    output.writeObject(record);

    ObjectInputStream input = new ObjectInputStream(new FileInputStream("clients.ser"));
    record = (AccountRecordSerializable) input.readObject();

    System.out.printf("%-10d%-12s%-12s%10.2f\n", record.getAccount(), record.getFirstName(),
            record.getLastName(), record.getBalance());

    output.close();
}

From source file:Main.java

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

    float[] fbuf = { 123.345f, 234.567f, 123.123f, 123.123f, 123.123f, 123.345f };

    FileOutputStream fos = new FileOutputStream("c:\\test.txt");
    DataOutputStream dos = new DataOutputStream(fos);

    for (float f : fbuf) {
        dos.writeFloat(f);//  w  w  w  .  j av a 2s .  c o m
    }

    dos.flush();

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

    DataInputStream dis = new DataInputStream(is);

    while (dis.available() > 0) {
        float c = dis.readFloat();
        System.out.print(c + " ");
    }
}

From source file:InputOutputDemoBinaryFile.java

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

    //Write primitive values to a binary file "java2s.dat":
    DataOutputStream dos = new DataOutputStream(new FileOutputStream("java2s.dat"));
    dos.writeInt(228);/* www.  j  a v a 2  s.  c om*/
    dos.writeChar(' ');
    dos.writeUTF("Java Source and Support at www.java2s.com");
    dos.close();
    //Read primitive values from binary file "java2s.dat":
    DataInputStream dis = new DataInputStream(new FileInputStream("java2s.dat"));
    System.out.println(dis.readInt() + "|" + dis.readChar() + "|" + dis.readUTF());
}