How can i read bytes from a very heavy file? then store store them in a String e.g. .pdf .zip .xlsx files

1w3j

im using the java.nio.file.Files library and the method works only when i choose tiny files (like txt, docx, "pdf but ONLY with a tiny size") and sometimes with a some minutes of delay. But if i choose a very large file with any extension or simply with a more "complex" extension (like .exe, .pptx, .zip, .rar, etc) the program clashes¡. it could be great if you give me the name of a up to date libray that has the same functionality as FileInputStream and Files just because i think the problem is that the library can't support large sizes or maybe a brilliant sorcerer who can solve my issue. Very Thanks¡

following the method im using:

private void readBytes(){
    try{
        boolean completed=false;
        File file=null;
        JFileChooser chooser=new JFileChooser();
        if(chooser.showOpenDialog(this)==JFileChooser.APPROVE_OPTION){
            file=new File(chooser.getSelectedFile().getAbsoluteFile().getPath());
            byte[]bytes=Files.readAllBytes(file.getAbsoluteFile().toPath());
            String output="File size: "+file.length()+" bytes\n\n";
            for(int i=0;i<bytes.length;i++){
                output+=bytes[i]+"  ";
                if(i!=0){                        
                    if(i%10==0)output+="\n";
                }
                if(i==(int)file.length()-1)completed=true;
            }
            if(completed)JOptionPane.showMessageDialog(this, "The reading has completed and the file size is: "+file.length()+" bytes");
            else JOptionPane.showMessageDialog(this, "The reading has not completed","Error",0);
            jTextArea1.setText(output);
        }
    }
    catch(Exception ex){}
}
Raghu K Nair

Files.readAllBytes is not recommended for large files as it loads into memory. May be you could use MappedByteBuffer.

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class ReadFileWithMappedByteBuffer 
{
    public static void main(String[] args) throws IOException 
    {
        RandomAccessFile aFile = new RandomAccessFile
                ("test.txt", "r");
        FileChannel inChannel = aFile.getChannel();
        MappedByteBuffer buffer = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, inChannel.size());
        buffer.load();  
        for (int i = 0; i < buffer.limit(); i++)
        {
            System.out.print((char) buffer.get());
        }
        buffer.clear(); // do something with the data and clear/compact it.
        inChannel.close();
        aFile.close();
    }
}

Please refer http://howtodoinjava.com/java-7/nio/3-ways-to-read-files-using-java-nio/ for more options

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Android development: How can i store pdf file in a android app as local file and read that pdf file?

From Dev

How can I read tsv files and store them as hdf5 without running out of memory?

From Dev

I want to read particular string from a text file and store them in Excel sheets

From Dev

I want to read particular string from a text file and store them in Excel sheets

From Dev

How do you read a integer and a string seperatly from a single line in file and store them to variables?

From Dev

How can I store a very large text file into an array?

From Dev

How can I store files in an archive without compressing them?

From Dev

How can I read a set of bytes from a file and convert them to letters?

From Dev

how can i read data from csv file and store in binary tree and write multiple file in perl

From Dev

How can i store pdf file on google cloud storage?

From Dev

How can I programmatically launch a PDF file from a URI in a Windows Store App on 8.1?

From Dev

How can I read uncertain data form file and store it in struct

From Dev

Java: How do I read and store data from input file?

From Dev

How can I read a file into bytes, and find a string of hex to match?

From Dev

read phone numbers from file and store them in other file uniquely

From Dev

Unable to read from text file and store into string

From Dev

How can I use VBA to read and store the file path, creation data, modified data, and other file information of thousands of files?

From Dev

How can I store substrings generated from a String into a String array?

From Dev

read in specific values from a file and store them in a list python

From Dev

How to Read values from xml file in java webservice and store them like application variables in Webservice?

From Dev

How can I store string from object as a variable?

From Dev

How can I fetch lyrics for all my music and store them within the files?

From Dev

How to read data from file and store into matrix

From Dev

How to store words read from a file into a list

From Dev

How to read data from file and store into matrix

From Dev

How can I read the first two bytes of a .zip file to confirm that the appropriate magic number is present?

From Dev

How to count rows in text files and store them in a single file?

From Dev

How to count rows in text files and store them in a single file?

From Dev

How to read variables from stdin dynamically and store them into an array

Related Related

  1. 1

    Android development: How can i store pdf file in a android app as local file and read that pdf file?

  2. 2

    How can I read tsv files and store them as hdf5 without running out of memory?

  3. 3

    I want to read particular string from a text file and store them in Excel sheets

  4. 4

    I want to read particular string from a text file and store them in Excel sheets

  5. 5

    How do you read a integer and a string seperatly from a single line in file and store them to variables?

  6. 6

    How can I store a very large text file into an array?

  7. 7

    How can I store files in an archive without compressing them?

  8. 8

    How can I read a set of bytes from a file and convert them to letters?

  9. 9

    how can i read data from csv file and store in binary tree and write multiple file in perl

  10. 10

    How can i store pdf file on google cloud storage?

  11. 11

    How can I programmatically launch a PDF file from a URI in a Windows Store App on 8.1?

  12. 12

    How can I read uncertain data form file and store it in struct

  13. 13

    Java: How do I read and store data from input file?

  14. 14

    How can I read a file into bytes, and find a string of hex to match?

  15. 15

    read phone numbers from file and store them in other file uniquely

  16. 16

    Unable to read from text file and store into string

  17. 17

    How can I use VBA to read and store the file path, creation data, modified data, and other file information of thousands of files?

  18. 18

    How can I store substrings generated from a String into a String array?

  19. 19

    read in specific values from a file and store them in a list python

  20. 20

    How to Read values from xml file in java webservice and store them like application variables in Webservice?

  21. 21

    How can I store string from object as a variable?

  22. 22

    How can I fetch lyrics for all my music and store them within the files?

  23. 23

    How to read data from file and store into matrix

  24. 24

    How to store words read from a file into a list

  25. 25

    How to read data from file and store into matrix

  26. 26

    How can I read the first two bytes of a .zip file to confirm that the appropriate magic number is present?

  27. 27

    How to count rows in text files and store them in a single file?

  28. 28

    How to count rows in text files and store them in a single file?

  29. 29

    How to read variables from stdin dynamically and store them into an array

HotTag

Archive