File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Get file and getBytes() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Get file and getBytes()" Watch "Get file and getBytes()" New topic

Get file and getBytes()

Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178

I want to read the test.txt file as byte, not int. But, it cannot cast int c to byte [] d. Or, I do not need to use int c to read the whole file?
Pls advise.
Ali Gohar
Ranch Hand

Joined: Mar 18, 2004
Posts: 572
May be following code help you
byte[] b;
File f = new File("c:/abc.txt");
int length = (int)f.length();
b = new byte[length];
FileInputStream fin = new FileInputStream(f);
DataInputStream din = new DataInputStream(fin);;
}catch(Exception ex){}
Andrew Parker
Ranch Hand

Joined: Nov 12, 2001
Posts: 178
I tried
The FileInputStream:

The EncryptClass constructor:

The FileOutputStream:

My questions are
1. how to make to be a byte array?
2. can I write byte [] b directly to os?
Thanks for your great help.
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Just don't try do it in one line of code:
EncryptClass o = new EncryptClass(;
You need to use two lines:;
EncryptClass o = new EncryptClass(bytes));

[Jess in Action][AskingGoodQuestions]
I agree. Here's the link:
subject: Get file and getBytes()
jQuery in Action, 3rd edition