Java File to String fileToString(File file)

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

Description

file To String

License

Open Source License

Declaration

public static String fileToString(File file) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Main {
    public static String fileToString(File file) {
        StringBuilder sb = new StringBuilder();

        try (BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file))) {

            for (int b; (b = inputStream.read()) != -1;) {
                String s = Integer.toHexString(b).toUpperCase();
                if (s.length() == 1) {
                    sb.append('0');
                }/*from  w  w w.ja  v a2s  .c  o m*/
                sb.append(s).append(' ');
            }

            return sb.toString().replace(" ", "");

        } catch (IOException e) {
            Logger logger = Logger.getAnonymousLogger();
            logger.log(Level.SEVERE, "Error while reading file.", e);
            return null;
        }
    }
}

Related

  1. fileToString(File file)
  2. fileToString(File file)
  3. fileToString(File file)
  4. fileToString(File file)
  5. fileToString(File file)
  6. fileToString(File file)
  7. fileToString(File file)
  8. fileToString(File file, int max)
  9. fileToString(File file, String charsetName)