Java File to Byte Array getBytes(File file)

Here you can find the source of getBytes(File file)

Description

get Bytes

License

Open Source License

Declaration

public static byte[] getBytes(File file) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  www  . j ava2s  .  c o m*/
 * Copyright (c) 2000-2005 Liferay, LLC. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileInputStream;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Main {
    public static byte[] getBytes(File file) throws IOException {
        if (file == null || !file.exists()) {
            return null;
        }

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        FileInputStream in = new FileInputStream(file);

        int c = in.read();

        while (c != -1) {
            out.write(c);
            c = in.read();
        }

        in.close();
        out.close();

        return out.toByteArray();
    }

    public static boolean exists(String fileName) {
        File file = new File(fileName);

        return file.exists();
    }

    public static String read(String fileName) throws IOException {
        return read(new File(fileName));
    }

    public static String read(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));

        StringBuffer sb = new StringBuffer();
        String line = null;

        while ((line = br.readLine()) != null) {
            sb.append(line).append('\n');
        }

        br.close();

        return sb.toString().trim();
    }

    public static void write(File file, String s) throws IOException {
        if (file.getParent() != null) {
            mkdirs(file.getParent());
        }

        BufferedWriter bw = new BufferedWriter(new FileWriter(file));

        bw.flush();
        bw.write(s);

        bw.close();
    }

    public static void write(String fileName, String s) throws IOException {
        write(new File(fileName), s);
    }

    public static void write(String pathName, String fileName, String s) throws IOException {

        write(new File(pathName, fileName), s);
    }

    public static void mkdirs(String pathName) {
        File file = new File(pathName);
        file.mkdirs();
    }
}

Related

  1. getBytes(File f)
  2. getBytes(File file)
  3. getBytes(File file)
  4. getBytes(File file)
  5. getBytes(File file)
  6. getBytes(File file)
  7. getBytes(File file)
  8. getBytes(File file)
  9. getBytes(File file)