File to byte in Java

0 votes
How do I convert a java.io.File to a byte[]?
Dec 28, 2020 in Java by anonymous
• 10,520 points
515 views

2 answers to this question.

0 votes

It depends on what best means for you. Productivity wise, don't reinvent the wheel and use Apache Commons. 

In Java, we can use Files.readAllBytes(path) to convert a File object into a byte[].

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

  String filePath = "/path/to/file";

  // file to byte[], Path
  byte[] bytes = Files.readAllBytes(Paths.get(filePath));

  // file to byte[], File -> Path
  File file = new File(filePath);
  byte[] bytes = Files.readAllBytes(file.toPath());
answered Dec 28, 2020 by Gitika
• 65,910 points
0 votes
Example 1: Convert File to byte[]
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;

public class FileByte {

    public static void main(String[] args) {

        String path = System.getProperty("user.dir") + "\\src\\test.txt";

        try {
            byte[] encoded = Files.readAllBytes(Paths.get(path));
            System.out.println(Arrays.toString(encoded));
        } catch (IOException e) {

        }
    }
}
v

[84, 104, 105, 115, 32, 105, 115, 32, 97, 13, 10, 84, 101, 115, 116, 32, 102, 105, 108, 101, 46]
In the above program, we store the path to the file in the variable path.


Then, inside the try block, we read all the bytes from the given path using readAllBytes() method.

answered Dec 28, 2020 by Reshma

Related Questions In Java

+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,304 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
2,097 views
0 votes
3 answers

How to check whether a file exists or not in Java?

Using nio we can check whether file ...READ MORE

answered Aug 14, 2018 in Java by Sushmita
• 6,910 points
3,529 views
0 votes
1 answer

Convert String type to byte[] in Java

Try using String.getBytes(). It returns a byte[] ...READ MORE

answered May 8, 2018 in Java by Rishabh
• 3,620 points
577 views
0 votes
2 answers

How can I get the filenames of all files in a folder which may or may not contain duplicates

List<String> results = new ArrayList<String>(); File[] files = ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,910 points
1,634 views
0 votes
2 answers

Store String inside a File using Java

We can use Apache Commons IO. It ...READ MORE

answered Jul 20, 2018 in Java by Sushmita
• 6,910 points
1,056 views
0 votes
2 answers

How to read a text file in Java?

You can use Scanner class to read ...READ MORE

answered Aug 9, 2018 in Java by Parth
• 4,630 points
754 views
0 votes
3 answers

How to convert File to Byte[] in Java?

import java.io.File; import java.nio.file.Files; File file; // ...(file is initialised)... byte[] ...READ MORE

answered Aug 15, 2018 in Java by samarth295
• 2,220 points
1,929 views
0 votes
1 answer

How to append text to an existing file in Java?

Java 7+ If you just need to do ...READ MORE

answered Dec 30, 2020 in Java by Gitika
• 65,910 points

edited Jul 6, 2023 by Khan Sarfaraz 948 views
+16 votes
25 answers

How can I convert String to JSON object in Java?

Hi @Daisy You can use Google gson  for more ...READ MORE

answered Feb 7, 2019 in Java by Suresh
• 720 points
250,397 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP