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 {
    Connection conn = getConnection();
    Statement stmt = conn.createStatement();

    stmt.executeUpdate("create table survey (id int, name BINARY );");

    String sql = "INSERT INTO survey (name) VALUES(?)";
    PreparedStatement pstmt = conn.prepareStatement(sql);

    // prepare small binary stream
    File smallFile = new File("yourFileName.txt");
    int smallFileLength = (int) smallFile.length();
    InputStream smallStream = (InputStream) new FileInputStream(smallFile);

    pstmt.setBinaryStream(2, smallStream, smallFileLength);

    // insert the data
    pstmt.executeUpdate();//from  w ww  . jav  a2s.  c  om

    ResultSet rs = stmt.executeQuery("SELECT * FROM survey");
    while (rs.next()) {
        System.out.print(rs.getString(1));
    }

    rs.close();
    stmt.close();
    conn.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    CheckedOutputStream checksum = new CheckedOutputStream(new FileOutputStream("data.zip"), new Adler32());
    ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(checksum));

    int size = 0;
    byte[] buffer = new byte[1024];

    File dir = new File(".");
    String[] files = dir.list();/* ww w. jav a  2s . c  om*/

    for (int i = 0; i < files.length; i++) {
        System.out.println("Compressing: " + files[i]);
        FileInputStream fis = new FileInputStream(files[i]);
        ZipEntry zipEntry = new ZipEntry(files[i]);
        zos.putNextEntry(zipEntry);

        while ((size = fis.read(buffer, 0, buffer.length)) > 0) {
            zos.write(buffer, 0, size);
        }
        zos.closeEntry();
        fis.close();
    }
    zos.close();
    System.out.println("Checksum   : " + checksum.getChecksum().getValue());
}

From source file:CursorApproachEventObject.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = XMLInputFactory.newInstance();
    xmlif.setEventAllocator(new XMLEventAllocatorImpl());
    allocator = xmlif.getEventAllocator();

    XMLStreamReader xmlr = xmlif.createXMLStreamReader(filename, new FileInputStream(filename));

    int eventType = xmlr.getEventType();

    while (xmlr.hasNext()) {
        eventType = xmlr.next();/*from w ww.j  a v  a2s.c o m*/
        if ((eventType == XMLStreamConstants.START_ELEMENT) && xmlr.getLocalName().equals("Book")) {
            StartElement event = getXMLEvent(xmlr).asStartElement();
            System.out.println("EVENT: " + event.toString());
        }
    }
}

From source file:MainClass.java

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

    String inName = args[0];//from   w  w  w .j a  v  a  2  s  . c om
    String outName;
    if (inName.endsWith(".pack.gz")) {
        outName = inName.substring(0, inName.length() - 8);
    } else if (inName.endsWith(".pack")) {
        outName = inName.substring(0, inName.length() - 5);
    } else {
        outName = inName + ".unpacked";
    }

    JarOutputStream out = null;
    InputStream in = null;

    Pack200.Unpacker unpacker = Pack200.newUnpacker();
    out = new JarOutputStream(new FileOutputStream(outName));
    in = new FileInputStream(inName);
    if (inName.endsWith(".gz"))
        in = new GZIPInputStream(in);
    unpacker.unpack(in, out);
    out.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document document = builder/*from  ww  w. j a  v  a  2 s  . c o  m*/
            .parse(new InputSource(new InputStreamReader(new FileInputStream("inputFile.xml"))));

    Transformer xformer = TransformerFactory.newInstance().newTransformer();
    xformer.setOutputProperty(OutputKeys.METHOD, "xml");
    xformer.setOutputProperty(OutputKeys.INDENT, "yes");
    xformer.setOutputProperty("http://xml.apache.org/xslt;indent-amount", "4");
    xformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    Source source = new DOMSource(document);
    Result result = new StreamResult(new File("result.xml"));
    xformer.transform(source, result);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Class.forName("oracle.jdbc.driver.OracleDriver");
    Connection conn = DriverManager.getConnection(url, username, password);
    conn.setAutoCommit(false);/*from  ww w .  j  a  v  a 2s. co m*/

    String sql = "INSERT INTO pictures (name, description, image) VALUES (?, ?, ?)";
    PreparedStatement stmt = conn.prepareStatement(sql);
    stmt.setString(1, "java.gif");
    stmt.setString(2, "Java Official Logo");

    File image = new File("D:\\a.gif");
    FileInputStream fis = new FileInputStream(image);
    stmt.setBinaryStream(3, fis, (int) image.length());
    stmt.execute();

    conn.commit();
    fis.close();
    conn.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    KeyGenerator keyGenerator = KeyGenerator.getInstance("Blowfish");

    keyGenerator.init(128);/*from  w w w . j a v  a 2  s. c  o m*/
    Key secretKey = keyGenerator.generateKey();

    Cipher cipherOut = Cipher.getInstance("Blowfish/CFB/NoPadding");

    cipherOut.init(Cipher.ENCRYPT_MODE, secretKey);
    BASE64Encoder encoder = new BASE64Encoder();
    byte iv[] = cipherOut.getIV();
    if (iv != null) {
        System.out.println("Initialization Vector of the Cipher:\n" + encoder.encode(iv));
    }
    FileInputStream fin = new FileInputStream("inputFile.txt");
    FileOutputStream fout = new FileOutputStream("outputFile.txt");
    CipherOutputStream cout = new CipherOutputStream(fout, cipherOut);
    int input = 0;
    while ((input = fin.read()) != -1) {
        cout.write(input);
    }

    fin.close();
    cout.close();
}

From source file:SerialDemo.java

static public void main(String[] args) {
    try {//from  w  w w  .j  ava2  s.c  o  m
        { // Save a SerialDemo object with a value of 5.
            FileOutputStream f = new FileOutputStream("/tmp/testing");
            ObjectOutputStream s = new ObjectOutputStream(f);
            SerialDemo d = new SerialDemo(5);

            s.writeObject(d);
            s.flush();
        }
        { // Now restore it and look at the value.
            FileInputStream f = new FileInputStream("/tmp/testing");
            ObjectInputStream s = new ObjectInputStream(f);
            SerialDemo d = (SerialDemo) s.readObject();

            System.out.println("SerialDemo.getVal() is: " + d.getVal());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:config.MainClass.java

/**
 * @param args the command line arguments
 *//* ww  w  . j  a va 2  s  .c  om*/
public static void main(String[] args) {
    // TODO code application logic here
    File file = new File("D:\\iti images\\Dish Party_6-11-2014\\IMG_5376.jpg");
    byte[] bFile = new byte[(int) file.length()];

    try {
        FileInputStream fileInputStream = new FileInputStream(file);
        //convert file into array of bytes
        fileInputStream.read(bFile);
        fileInputStream.close();
    } catch (Exception e) {
        System.out.println("Error in reading the image");
    }
    UserData data = new UserData();
    data.setUserName("hasby allah");
    data.setBirthday(new Date());
    data.setPhone("65656");
    data.setFullName("7asby allah w n3m el wakel");
    data.setAddress("haram");
    data.setPassword("password");
    data.setImage(bFile);
    AbstractApplicationContext fact = new ClassPathXmlApplicationContext("SpringConfig.xml");
    DaoService serviceInterface = fact.getBean(DaoService.class);
    serviceInterface.save(data);
}