Example usage for java.io FileInputStream close

List of usage examples for java.io FileInputStream close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this file input stream and releases any system resources associated with the stream.

Usage

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("C:/test.bin");
    FileInputStream inFile = null;
    try {/* ww  w  .j a  va 2 s.c om*/
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    final int PRIMECOUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT);
    long[] primes = new long[PRIMECOUNT];
    try {
        while (inChannel.read(buf) != -1) {
            ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes);
            for (long prime : primes) {
                System.out.printf("%10d", prime);
            }
            buf.clear();
        }
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
    }
}

From source file:PersistentEcho.java

public static void main(String[] args) {
    String argString = "";
    boolean notProperty = true;

    // Are there arguments? 
    // If so retrieve them.
    if (args.length > 0) {
        for (String arg : args) {
            argString += arg + " ";
        }//from   w  w  w.j a v  a2  s .c o  m
        argString = argString.trim();
    }
    // No arguments, is there
    // an environment variable?
    // If so, //retrieve it.
    else if ((argString = System.getenv("PERSISTENTECHO")) != null) {
    }
    // No environment variable
    // either. Retrieve property value.
    else {
        notProperty = false;
        // Set argString to null.
        // If it's still null after
        // we exit the try block,
        // we've failed to retrieve
        // the property value.
        argString = null;
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream("PersistentEcho.txt");
            Properties inProperties = new Properties();
            inProperties.load(fileInputStream);
            argString = inProperties.getProperty("argString");
        } catch (IOException e) {
            System.err.println("Can't read property file.");
            System.exit(1);
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                }
                ;
            }
        }
    }
    if (argString == null) {
        System.err.println("Couldn't find argString property");
        System.exit(1);
    }

    // Somehow, we got the
    // value. Echo it already!
    System.out.println(argString);

    // If we didn't retrieve the
    // value from the property,
    // save it //in the property.
    if (notProperty) {
        Properties outProperties = new Properties();
        outProperties.setProperty("argString", argString);
        FileOutputStream fileOutputStream = null;
        try {
            fileOutputStream = new FileOutputStream("PersistentEcho.txt");
            outProperties.store(fileOutputStream, "PersistentEcho properties");
        } catch (IOException e) {
        } finally {
            if (fileOutputStream != null) {
                try {
                    fileOutputStream.close();
                } catch (IOException e) {
                }
                ;
            }
        }
    }
}

From source file:com.alexoree.jenkins.Main.java

public static void main(String[] args) throws Exception {
    // create Options object
    Options options = new Options();

    options.addOption("t", false, "throttle the downloads, waits 5 seconds in between each d/l");

    // automatically generate the help statement
    HelpFormatter formatter = new HelpFormatter();
    formatter.printHelp("jenkins-sync", options);

    CommandLineParser parser = new DefaultParser();
    CommandLine cmd = parser.parse(options, args);
    boolean throttle = cmd.hasOption("t");

    String plugins = "https://updates.jenkins-ci.org/latest/";
    List<String> ps = new ArrayList<String>();
    Document doc = Jsoup.connect(plugins).get();
    for (Element file : doc.select("td a")) {
        //System.out.println(file.attr("href"));
        if (file.attr("href").endsWith(".hpi") || file.attr("href").endsWith(".war")) {
            ps.add(file.attr("href"));
        }/* w ww.j  a v a2s  .c  o  m*/
    }

    File root = new File(".");
    //https://updates.jenkins-ci.org/latest/AdaptivePlugin.hpi
    new File("./latest").mkdirs();

    //output zip file
    String zipFile = "jenkinsSync.zip";
    // create byte buffer
    byte[] buffer = new byte[1024];
    FileOutputStream fos = new FileOutputStream(zipFile);
    ZipOutputStream zos = new ZipOutputStream(fos);

    //download the plugins
    for (int i = 0; i < ps.size(); i++) {
        System.out.println("[" + i + "/" + ps.size() + "] downloading " + plugins + ps.get(i));
        String outputFile = download(root.getAbsolutePath() + "/latest/" + ps.get(i), plugins + ps.get(i));

        FileInputStream fis = new FileInputStream(outputFile);
        // begin writing a new ZIP entry, positions the stream to the start of the entry data
        zos.putNextEntry(new ZipEntry(outputFile.replace(root.getAbsolutePath(), "")
                .replace("updates.jenkins-ci.org/", "").replace("https:/", "")));
        int length;
        while ((length = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }
        zos.closeEntry();
        fis.close();
        if (throttle)
            Thread.sleep(WAIT);
        new File(root.getAbsolutePath() + "/latest/" + ps.get(i)).deleteOnExit();
    }

    //download the json metadata
    plugins = "https://updates.jenkins-ci.org/";
    ps = new ArrayList<String>();
    doc = Jsoup.connect(plugins).get();
    for (Element file : doc.select("td a")) {
        //System.out.println(file.attr("href"));
        if (file.attr("href").endsWith(".json")) {
            ps.add(file.attr("href"));
        }
    }
    for (int i = 0; i < ps.size(); i++) {
        download(root.getAbsolutePath() + "/" + ps.get(i), plugins + ps.get(i));

        FileInputStream fis = new FileInputStream(root.getAbsolutePath() + "/" + ps.get(i));
        // begin writing a new ZIP entry, positions the stream to the start of the entry data
        zos.putNextEntry(new ZipEntry(plugins + ps.get(i)));
        int length;
        while ((length = fis.read(buffer)) > 0) {
            zos.write(buffer, 0, length);
        }
        zos.closeEntry();
        fis.close();
        new File(root.getAbsolutePath() + "/" + ps.get(i)).deleteOnExit();
        if (throttle)
            Thread.sleep(WAIT);
    }

    // close the ZipOutputStream
    zos.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String zipname = "data.zip";
    FileInputStream fis = new FileInputStream(zipname);
    ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    ZipEntry entry;/*ww w . j  av  a2s. com*/

    while ((entry = zis.getNextEntry()) != null) {
        System.out.println("Unzipping: " + entry.getName());

        int size;
        byte[] buffer = new byte[2048];

        FileOutputStream fos = new FileOutputStream(entry.getName());
        BufferedOutputStream bos = new BufferedOutputStream(fos, buffer.length);

        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
            bos.write(buffer, 0, size);
        }
        bos.flush();
        bos.close();
    }
    zis.close();
    fis.close();
}

From source file:Main.java

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

    st.executeUpdate("create table images (Id int, b BLOB);");

    File file = new File("myimage.gif");
    FileInputStream fis = new FileInputStream(file);
    PreparedStatement ps = conn.prepareStatement("insert into images values (?,?)");
    ps.setString(1, "10");
    ps.setBinaryStream(2, fis);//from   w  w w.  j a  v  a  2 s . c om
    ps.executeUpdate();

    ResultSet rset = st.executeQuery("select b from images");
    InputStream stream = rset.getBinaryStream(1);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    int a1 = stream.read();
    while (a1 >= 0) {
        output.write((char) a1);
        a1 = stream.read();
    }
    Image myImage = Toolkit.getDefaultToolkit().createImage(output.toByteArray());
    output.close();

    ps.close();

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

From source file:io.nuclei.box.db.DbContext.java

public static void main(String[] args) throws IOException {
    FileInputStream input = new FileInputStream(args[0]);
    byte[] buf = new byte[input.available()];
    input.read(buf);/*from w w w .j ava2 s.c  om*/
    input.close();
    JSONObject model = new JSONObject(new String(buf, "UTF-8"));
    DbContext.newContext(model, new File(args[1]), null, null).render();
}

From source file:controller.tempClass.java

/**
 * @desc Image manipulation - Conversion
 *
 * @filename ImageManipulation.java//w  w  w .  j  a  v a2s  .c  o m
 * @author Jeevanandam M. (jeeva@myjeeva.com)
 * @copyright myjeeva.com
 */
public static void main(String[] args) {

    File file = new File("E:\\Photograph\\Temporary\\mshien.jpg");

    try {
        // Reading a Image file from file system
        FileInputStream imageInFile = new FileInputStream(file);
        byte imageData[] = new byte[(int) file.length()];
        imageInFile.read(imageData);

        // Converting Image byte array into Base64 String
        System.out.println(encodeImage(imageData));
        String imageDataString = encodeImage(imageData);

        // Converting a Base64 String into Image byte array
        byte[] imageByteArray = decodeImage(imageDataString);

        // Write a image byte array into file system
        FileOutputStream imageOutFile = new FileOutputStream("C:\\Users\\doanvanthien\\Desktop\\LOGO.png");

        imageOutFile.write(imageByteArray);

        imageInFile.close();
        imageOutFile.close();

        System.out.println("Image Successfully Manipulated!");
    } catch (FileNotFoundException e) {
        System.out.println("Image not found" + e);
    } catch (IOException ioe) {
        System.out.println("Exception while reading the Image " + ioe);
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    File aFile = new File("file.dat");
    FileInputStream inFile = null;

    try {//from  w  w  w .ja v  a 2s.com
        inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
        System.exit(0);
    }
    FileChannel inChannel = inFile.getChannel();
    final int COUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * COUNT);
    long[] data = new long[COUNT];
    try {
        while (inChannel.read(buf) != -1) {
            ((ByteBuffer) (buf.flip())).asLongBuffer().get(data);
            System.out.println();
            for (long prime : data)
                System.out.printf("%10d", prime);
            buf.clear();
        }
        System.out.println("\nEOF reached.");
        inFile.close();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
}

From source file:InsertTextFileToOracle.java

public static void main(String[] args) throws Exception {
    String id = "001";
    String fileName = "fileName.txt";

    FileInputStream fis = null;
    PreparedStatement pstmt = null;
    Connection conn = null;/*w ww.  j a  va 2 s. c  om*/
    try {
        conn = getConnection();
        conn.setAutoCommit(false);
        File file = new File(fileName);
        fis = new FileInputStream(file);
        pstmt = conn.prepareStatement("insert into DataFiles(id, fileName, fileBody) values (?, ?, ?)");
        pstmt.setString(1, id);
        pstmt.setString(2, fileName);
        pstmt.setAsciiStream(3, fis, (int) file.length());
        pstmt.executeUpdate();
        conn.commit();
    } catch (Exception e) {
        System.err.println("Error: " + e.getMessage());
        e.printStackTrace();
    } finally {
        pstmt.close();
        fis.close();
        conn.close();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File aFile = new File("main.java");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer lengthBuf = ByteBuffer.allocate(8);
    while (true) {
        if (inChannel.read(lengthBuf) == -1) {
            break;
        }/*from   ww w . ja  va  2  s.  co m*/
        lengthBuf.flip();
        int strLength = (int) lengthBuf.getDouble();
        ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
        if (inChannel.read(buf) == -1) {
            break;
        }
        buf.flip();
        byte[] strChars = new byte[2 * strLength];
        buf.get(strChars);
        System.out.println(strLength);
        System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
        System.out.println(buf.getLong());
        lengthBuf.clear();
    }
    inFile.close();
}