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 {

    ObjectOutputStream out = new ObjectOutputStream(
            new BufferedOutputStream(new FileOutputStream("file.data")));
    out.write("java2s.com".getBytes(), 1, 2);
    out.close();/*from  w w w .  jav  a  2s. c om*/

    ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(new FileInputStream("file.data")));

    byte[] byteArray = new byte[10];
    in.read(byteArray);
    System.out.println(Arrays.toString(byteArray));
    in.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    double[] dbuf = { 12.34, 34.45, 67.78, 88.88, 66.66, 77.33 };

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

    for (double d : dbuf) {
        dos.writeDouble(d);//from  w ww. j a va  2 s  . 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:Main.java

public static void main(String[] args) throws Exception {
    Connection con = DriverManager.getConnection("jdbc:h2:mem:");
    Statement s = con.createStatement();
    s.execute("CREATE TABLE Table1 (Column1 CLOB)");

    InputStream is = new FileInputStream("data.txt");
    Reader rdr = new InputStreamReader(is, StandardCharsets.ISO_8859_1);
    PreparedStatement ps = con.prepareStatement("INSERT INTO Table1 (Column1) VALUES (?)");
    ps.setCharacterStream(1, rdr);/*from  ww  w .  j a va2  s .c  o  m*/
    ps.executeUpdate();

    ResultSet rs = s.executeQuery("SELECT Column1 FROM Table1");
    int rowNumber = 0;
    while (rs.next()) {
        String str = rs.getString("Column1");
        System.out.println(String.format("Row %d: CLOB is %d character(s) long.", ++rowNumber, str.length()));
    }
    rs.close();
    con.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    DataOutputStream out2 = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("Data.txt")));
    out2.writeDouble(3.14159);//from  w  w w. ja  v a  2  s.co  m
    out2.writeUTF("Square root of 2");
    out2.close();

    DataInputStream in5 = new DataInputStream(new BufferedInputStream(new FileInputStream("Data.txt")));

    System.out.println(in5.readDouble());
    System.out.println(in5.readUTF());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Connection conn = getConnection();
    Statement st = conn.createStatement();

    st.executeUpdate("create table survey (Id int, b CLOB);");

    PreparedStatement pstmt = conn.prepareStatement("INSERT INTO survey VALUES(1,?)");

    File file = new File("c:/Java_Dev/data.txt");
    FileInputStream fis = new FileInputStream(file);
    pstmt.setAsciiStream(1, fis, (int) file.length());
    pstmt.execute();/* w w  w. jav  a2s .  com*/

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

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String cacert = "mytest.cer";
    String lfcert = "lf_signed.cer";
    String lfstore = "lfkeystore";
    char[] lfstorepass = "wshr.ut".toCharArray();
    char[] lfkeypass = "wshr.ut".toCharArray();
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    FileInputStream in1 = new FileInputStream(cacert);
    java.security.cert.Certificate cac = cf.generateCertificate(in1);
    in1.close();// ww w.ja va 2  s .  co  m
    FileInputStream in2 = new FileInputStream(lfcert);
    java.security.cert.Certificate lfc = cf.generateCertificate(in2);
    in2.close();
    java.security.cert.Certificate[] cchain = { lfc, cac };
    FileInputStream in3 = new FileInputStream(lfstore);
    KeyStore ks = KeyStore.getInstance("JKS");
    ks.load(in3, lfstorepass);
    PrivateKey prk = (PrivateKey) ks.getKey("lf", lfkeypass);
    ks.setKeyEntry("lf_signed", prk, lfstorepass, cchain);
    FileOutputStream out4 = new FileOutputStream("lfnewstore");
    ks.store(out4, "newpass".toCharArray());
    out4.close();
}

From source file:WordCount.java

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

    // Map File from filename to byte buffer
    FileInputStream input = new FileInputStream(filename);
    FileChannel channel = input.getChannel();
    int fileLength = (int) channel.size();
    MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, fileLength);

    // Convert to character buffer
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    CharBuffer charBuffer = decoder.decode(buffer);

    // Create line pattern
    Pattern linePattern = Pattern.compile(".*$", Pattern.MULTILINE);

    // Create word pattern
    Pattern wordBreakPattern = Pattern.compile("[\\p{Punct}\\s}]");

    // Match line pattern to buffer
    Matcher lineMatcher = linePattern.matcher(charBuffer);

    Map map = new TreeMap();
    Integer ONE = new Integer(1);

    // For each line
    while (lineMatcher.find()) {
        // Get line
        CharSequence line = lineMatcher.group();

        // Get array of words on line
        String words[] = wordBreakPattern.split(line);

        // For each word
        for (int i = 0, n = words.length; i < n; i++) {
            if (words[i].length() > 0) {
                Integer frequency = (Integer) map.get(words[i]);
                if (frequency == null) {
                    frequency = ONE;//  w w  w .j  a  v a 2s . com
                } else {
                    int value = frequency.intValue();
                    frequency = new Integer(value + 1);
                }
                map.put(words[i], frequency);
            }
        }
    }
    System.out.println(map);
}

From source file:SVGDOC2JPEG.java

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

    JPEGTranscoder transcoder = new JPEGTranscoder();

    transcoder.addTranscodingHint(JPEGTranscoder.KEY_XML_PARSER_CLASSNAME,
            "org.apache.crimson.parser.XMLReaderImpl");
    transcoder.addTranscodingHint(JPEGTranscoder.KEY_QUALITY, new Float(1.0));

    TranscoderInput input = new TranscoderInput(new FileInputStream("rectangles.svg"));
    OutputStream ostream = new FileOutputStream("out.jpg");
    TranscoderOutput output = new TranscoderOutput(ostream);

    transcoder.transcode(input, output);
    ostream.close();/*  w  w w .j av a2 s.c  o m*/
    System.exit(0);
}

From source file:Main.java

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

    byte b = 127;

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

    // write something in the file
    oout.writeByte(b);/*from   ww  w.j a va 2  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 unshared object
    System.out.println(ois.readUnsignedByte());
    ois.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String url = "jdbc:mysql://localhost:3306/";
    String dbName = "javatutorial";
    String userName = "root";
    String password = "root";

    Class.forName("com.mysql.jdbc.Driver");
    Connection con = DriverManager.getConnection(url + dbName, userName, password);
    File imgfile = new File("images.jpg");
    FileInputStream fin = new FileInputStream(imgfile);
    PreparedStatement pre = con.prepareStatement("insert into Image values(?,?,?)");
    pre.setInt(1, 5);/*from  ww  w.j  a v  a  2 s. c o  m*/
    pre.setString(2, "A");
    pre.setBinaryStream(3, fin, (int) imgfile.length());
    pre.executeUpdate();
    pre.close();
    con.close();
}