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

/**
 * @param fileName/*w  w  w. j  a  va 2  s  . c  o m*/
 */
public static String readFileByLines(String fileName) {
    String res = "";
    try {
        FileInputStream fin = new FileInputStream(fileName);
        int length = fin.available();
        byte[] buffer = new byte[length + 1024];
        fin.read(buffer);
        res = EncodingUtils.getString(buffer, "UTF-8");
        fin.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return res;
}

From source file:Main.java

private static void shareAct(Activity act, String fileName, String text) {

    Uri uri = null;/*from   w  w w.ja  v a2 s. c om*/

    try {
        FileInputStream input = act.openFileInput(fileName);
        Bitmap bitmap = BitmapFactory.decodeStream(input);
        uri = Uri.parse(MediaStore.Images.Media.insertImage(act.getContentResolver(), bitmap, null, null));
        input.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    Intent shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setType("image/jpeg");
    act.startActivity(Intent.createChooser(shareIntent, act.getTitle()));
}

From source file:com.redsqirl.workflow.utils.FileStream.java

public static byte[] decryptFile(File in) throws Exception {
    //Decrypt the file data:
    byte[] encData;

    //Read in the file:
    FileInputStream inStream = new FileInputStream(in);
    encData = new byte[(int) in.length()];
    inStream.read(encData);//from   w  w  w. j a v  a  2s .  c o  m
    inStream.close();

    byte[] decData = decrypt(encData);

    //Figure out how much padding to remove
    int padCount = (int) decData[decData.length - 1];

    //Naive check, will fail if plaintext file actually contained
    //this at the end
    //For robust check, check that padCount bytes at the end have same value
    if (padCount >= 1 && padCount <= 8) {
        decData = Arrays.copyOfRange(decData, 0, decData.length - padCount);
    }

    //Write the decrypted data to a new file:
    return decData;
}

From source file:id.co.nlp.MachineTranslation.Utils.Util.java

public static Object deserializing(String pathfile)
        throws FileNotFoundException, IOException, ClassNotFoundException {
    FileInputStream fileIn = new FileInputStream(pathfile);
    ObjectInputStream in = new ObjectInputStream(fileIn);
    Object object = in.readObject();
    in.close();/*from   w  ww. jav  a  2  s.c  o  m*/
    fileIn.close();
    return object;
}

From source file:com.ikon.util.FileUtils.java

/**
 * Copy File to OutputStream//www  .j  av  a  2  s .c o m
 */
public static void copy(File input, OutputStream output) throws IOException {
    FileInputStream fis = new FileInputStream(input);
    IOUtils.copy(fis, output);
    fis.close();
}

From source file:Main.java

public static <T> T unmarshall(Class<T> clazz, String filename, Unmarshaller.Listener listener)
        throws JAXBException, IOException {
    FileInputStream is = null;
    T object;//from  w  w  w. j a v  a 2  s  .co  m
    try {
        is = new FileInputStream(filename);
        object = unmarshall(clazz, is, listener);
    } finally {
        if (is != null) {
            is.close();
        }
    }
    return object;
}

From source file:com.knockturnmc.api.util.ConfigurationUtils.java

/**
 * Loads a mapped {@link Properties} file and applies the mapping provided by the {@link NamedProperties}.
 * If the desired file was not found in the datafolder, a default file will be copied from the classpath.
 *
 * @param classLoader the classloader to use for the default file
 * @param filename    the filename//  w w w  . j  a va 2  s  .co  m
 * @param datafolder  the datafolder
 * @param mapping     the mapped file
 * @param <T>         the type of the mapped file
 * @return the loaded configuration mapping
 */
public static <T extends NamedProperties> T loadConfiguration(ClassLoader classLoader, String filename,
        File datafolder, Class<? extends T> mapping) {
    try {
        File file = getConfigFile(classLoader, filename, datafolder);
        Constructor<? extends T> constructor = mapping.getDeclaredConstructor();
        constructor.setAccessible(true);
        T properties = constructor.newInstance();
        FileInputStream stream = new FileInputStream(file);
        properties.load(stream);
        stream.close();

        OutputStream fos = new FileOutputStream(file);

        properties.store(fos, "Configuration for " + filename);
        fos.close();
        return properties;
    } catch (Exception e) {
        logger.warn("Failed to load configuration", e);
        throw new RuntimeException(e);
    }
}

From source file:Main.java

/**
 * This method deserializes an object from an input stream.
 *
 * @param file The input file//from  ww  w .j  a  v a2 s. c om
 * @return The deserialized object
 * @exception IOException IOError
 */
public static Object deserializeObject(File file) throws IOException, ClassNotFoundException {
    FileInputStream fis = new FileInputStream(file);
    Object object = null;
    try {
        ObjectInputStream ois = new ObjectInputStream(new BufferedInputStream(fis));
        object = ois.readObject();
    } finally {
        fis.close();
    }
    return object;
}

From source file:Main.java

@Deprecated
public static String getStringFromFile(String filePath) {
    File fl = new File(filePath);
    FileInputStream fin = null;
    String ret = null;//from  ww w  .  j  a v  a  2 s  . com
    try {
        fin = new FileInputStream(fl);
        ret = convertStreamToString(fin);
        //Make sure you close all streams.
        fin.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return ret;
}

From source file:Compress.java

public static void gzipFile(String from, String to) throws IOException {
    FileInputStream in = new FileInputStream(from);
    GZIPOutputStream out = new GZIPOutputStream(new FileOutputStream(to));
    byte[] buffer = new byte[4096];
    int bytesRead;
    while ((bytesRead = in.read(buffer)) != -1)
        out.write(buffer, 0, bytesRead);
    in.close();
    out.close();//from   w ww . ja  v  a 2  s  .  co m
}