How to convert File to Byte[] in Java?

0 votes
I am working on a file. But I want to convert it into bytes. How can I do this?
May 7, 2018 in Java by Daisy
• 8,050 points
102 views

3 answers to this question.

0 votes

You can use IOUtils.toByteArray(InputStream input).

answered May 7, 2018 by Sushmita
• 6,880 points
0 votes
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;

Path path = Paths.get("path of file");
byte[] data = Files.readAllBytes(path);
answered Jul 10, 2018 by parth295
• 4,640 points
0 votes
import java.io.File;
import java.nio.file.Files;

File file;
// ...(file is initialised)...
byte[] fileContent = Files.readAllBytes(file.toPath());
answered Aug 14, 2018 by samarth295
• 2,190 points

Related Questions In Java

0 votes
2 answers

How to convert a byte into Hexadecimal in Java?

private static final String ...READ MORE

answered Aug 29 in Java by Sirajul
• 40,990 points
66 views
+5 votes
3 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,380 points

edited Nov 6, 2018 by Omkar 11,371 views
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered Aug 20 in Java by Sirajul
• 40,990 points
213 views
0 votes
2 answers
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,880 points
629 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,880 points
54 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 parth295
• 4,640 points
63 views
0 votes
2 answers

How to read the files using Java?

You can use collections. try (Stream<Path> filePathStream=Files.walk(Paths.get("/home/you/Desktop"))) { ...READ MORE

answered Jul 10, 2018 in Java by Sushmita
• 6,880 points
72 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 27, 2018 in Java by samarth295
• 2,190 points
129 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,880 points
295 views