-1

I have stored a flatfile (.txt format) in DB as BLOB and retrieving it using JPA like below.

@Lob
@Column( name="TXT_FILE")
private byte[] txtFile;

Could you please suggest, how can I get the content of this text file from this byte[]?

Shakthi
  • 826
  • 3
  • 15
  • 33
  • Wonder what basic JPA docs would tell you? How do you read ANY field? If instead you mean HOW DO I CONVERT a BYTE[] into a String then that is nothing at all to do with JPA –  Feb 25 '19 at 07:17
  • Its not a file content that I'm trying to fetch. Its a file object stored in db. I want its content. – Shakthi Feb 25 '19 at 08:32
  • So you retrieve the field using JPA, and you have a `byte[]`, and so need to convert `byte[]` into a `java.io.File` ? Which is then nothing to do with JPA –  Feb 25 '19 at 09:09
  • https://stackoverflow.com/questions/4350084/byte-to-file-in-java –  Feb 25 '19 at 09:18
  • This sample requires server path to store the file first then only we can read the content of it right? I don't want to store the file. Without storing, I want to retrieve the file content. – Shakthi Feb 25 '19 at 12:25
  • Look, YOU stored the object in the database, and so you must know what format you used to store it, so you reverse that to convert it back. The only thing in a database is the contents of a file EVER, or a serialised form of the file. –  Feb 25 '19 at 13:20

1 Answers1

0

Well, I'm supposing that you didn't persist the whole file, but only it's content. If so, you could just read it by doing a new String(yourObject.getTxtFile(), "UTF-8")

Note that used UTF-8 as the encoding pattern. So you'll need to store data with it too.

R. Karlus
  • 2,094
  • 3
  • 24
  • 48