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:MainClass.java

public static void main(String args[]) throws Exception {
    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("keyfile"));
    DESKeySpec ks = new DESKeySpec((byte[]) ois.readObject());
    SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
    SecretKey key = skf.generateSecret(ks);

    Cipher c = Cipher.getInstance("DES/CFB8/NoPadding");
    c.init(Cipher.DECRYPT_MODE, key, new IvParameterSpec((byte[]) ois.readObject()));
    CipherInputStream cis = new CipherInputStream(new FileInputStream("ciphertext"), c);
    BufferedReader br = new BufferedReader(new InputStreamReader(cis));
    System.out.println(br.readLine());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    try {/*ww w  . jav a2  s  . co m*/
        String url = "jdbc:odbc:databaseName";
        String driver = "sun.jdbc.odbc.JdbcOdbcDriver";
        String user = "guest";
        String password = "guest";
        FileInputStream fis = new FileInputStream("somefile.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, "TryInputStream"); // Set first field
        statement.setAsciiStream(2, fis, fis.available()); // Stream is source

        int rowsUpdated = statement.executeUpdate();
        System.out.println("Rows affected: " + rowsUpdated);
        connection.close();
    } catch (Exception e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) {

    String s = "from java2s.com!";

    try {// ww w  . jav  a  2  s  .  c o m

        OutputStream os = new FileOutputStream("test.txt");
        OutputStreamWriter writer = new OutputStreamWriter(os, Charset.defaultCharset());

        FileInputStream in = new FileInputStream("test.txt");

        writer.write(s, 0, 5);

        writer.flush();

        for (int i = 0; i < 5; i++) {
            System.out.print((char) in.read());
        }
        writer.close();
        in.close();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    File originFile = new File("c:\\file1.txt");
    File destinationFile = new File("c:\\file1.txt");
    if (!originFile.exists() || destinationFile.exists()) {
        return;//from w  w w.j av a  2 s .c  o m
    }
    try {
        byte[] readData = new byte[1024];
        FileInputStream fis = new FileInputStream(originFile);
        FileOutputStream fos = new FileOutputStream(destinationFile);
        int i = fis.read(readData);

        while (i != -1) {
            fos.write(readData, 0, i);
            i = fis.read(readData);
        }
        fis.close();
        fos.close();
    } catch (IOException e) {
        System.out.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xif = XMLInputFactory.newInstance();
    XMLEventReader xmlr = xif.createXMLEventReader((new FileInputStream(new File("./file.xml"))));

    boolean inline = false;
    StringBuffer sb = new StringBuffer();
    while (xmlr.hasNext()) {
        XMLEvent event = xmlr.nextEvent();

        if (event.isStartElement()) {
            StartElement element = (StartElement) event;
            if ("data".equals(element.getName().toString().trim())) {
                inline = true;/*from w  w w.  ja  v  a  2  s.c o m*/
            }
        }

        if (inline) {
            sb.append(xmlr.peek());
        }

        if (event.isEndElement()) {
            EndElement element = (EndElement) event;
            if ("data".equals(element.getName().toString().trim())) {
                inline = false;
                System.out.println(sb.toString());
                sb.setLength(0);
            }
        }
    }
}

From source file:CompressIt.java

public static void main(String[] args) {
    String filename = args[0];// w w  w. java2s  . c o  m
    try {
        File file = new File(filename);
        int length = (int) file.length();
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(length);
        GZIPOutputStream gos = new GZIPOutputStream(baos);
        byte[] buffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = bis.read(buffer)) != -1) {
            gos.write(buffer, 0, bytesRead);
        }
        bis.close();
        gos.close();
        System.out.println("Input Length: " + length);
        System.out.println("Output Length: " + baos.size());
    } catch (FileNotFoundException e) {
        System.err.println("Invalid Filename");
    } catch (IOException e) {
        System.err.println("I/O Exception");
    }
}

From source file:MainClass.java

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

    if (args.length != 3) {
        System.out.println("Usage: java TeeCopier infile outfile1 outfile2");
        return;// w  ww .  j  av  a  2 s . com
    }

    FileInputStream fin = new FileInputStream(args[0]);
    FileOutputStream fout1 = new FileOutputStream(args[1]);
    FileOutputStream fout2 = new FileOutputStream(args[2]);
    MyOutputStream tout = new MyOutputStream(fout1, fout2);
    copy(fin, tout);
    fin.close();
    tout.close();
}

From source file:Main.java

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

    XMLInputFactory factory = XMLInputFactory.newInstance();
    System.out.println("FACTORY: " + factory);

    XMLEventReader r = factory.createXMLEventReader(filename, new FileInputStream(filename));

    while (r.hasNext()) {
        XMLEvent e = r.nextEvent();
        System.out.println(e.toString());
    }//from w  w w.  j  a  v  a 2s .  c om
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    Certificate[] certpath = new Certificate[args.length];
    CertificateFactory cf = CertificateFactory.getInstance("X.509");

    for (int i = 0; i < args.length; i++) {
        FileInputStream in = new FileInputStream(args[i]);
        certpath[i] = cf.generateCertificate(in);
    }/* w  ww .ja  v  a  2s. co m*/
    for (int i = 0; i < certpath.length - 1; i++) {
        Principal issuer = ((X509Certificate) certpath[i]).getIssuerDN();
        Principal subject = ((X509Certificate) certpath[i + 1]).getSubjectDN();
        if (!issuer.equals(subject)) {
            System.out.println("in " + i + " issuer is " + issuer);
            System.out.println("But in " + (i + 1));
            System.out.println("subject is " + subject);
            break;
        }
    }
}

From source file:Main.java

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

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

    oout.writeUTF("Hello World from java2s.com");
    oout.flush();/*from   w  ww .  ja  v  a  2s  . com*/
    oout.close();

    ObjectInputStream ois = new ObjectInputStream(new FileInputStream("test.txt"));

    for (int i = 0; i < ois.available();) {
        System.out.print((char) ois.read());
    }
    ois.close();
}