1

I can do this but this question is more to know about what is the best way to achieve this.

I can do following to get the file in buffered reader

BufferedReader bufferedReader = new BufferedReader(new   InputStreamReader(new FileInputStream(file), "UTF-8"));

What is the best way to proceed after this?

1
  • The requirement itself does not quite make sense. Look: UTF-8 is an encoding. I.E.: A parameter to convert characters to bytes (and vice-versa). Every conversion must have its proper encoding. So if you read a file (which is always a sequence of bytes) with a specific encoding, it's because it should be converted to chars. OK. Then, if it must be converted back again to bytes, the same encoding must be used, so... it becomes a pure do+undo. You can read the file as bytes and forget about the encoding. Commented Nov 12, 2016 at 20:11

1 Answer 1

2

The best approach is often the simplest one:

Path path = Paths.get("path/to/file");
byte[] data = Files.readAllBytes(path);

PS: If you have a File object instead of string representation of the path, then just convert the File to Path:

Path path = file.toPath()
Sign up to request clarification or add additional context in comments.

2 Comments

First requirement is to read the file in UTF format and then process further
UTF is not a format, but an encoding scheme. If your file is already UTF-8 encoded, you just have to read it. Nothing more

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.