Discussion Locked

ByteArrayInputStream of java

0 helpful
2 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

Can anyone tell me how can I use the inputStream( ByteArrayInputStream). This class defines two constructors in the API

1>ByteArrayInputStream(byte[])
2>ByteArrayInputStream(byte[],int offset,int size)

Now tell me how will I connect my file from which I will be reading data?

Best regards,
MMM

2 replies

by snoopdoug

Try this:

import java.io.*;

public class myIOTest {

public static final int EOF = -1;
public static final int BUF_SIZE = 128;
public static final String FILENAME = "myFile";

public static void main(String [] args) {

File inFile;
FileInputStream myStream;
int c;
byte [] buf = new byte[BUF_SIZE];

try {
inFile = new File(FILENAME);
myStream = new FileInputStream(inFile);

/* The last read my notreturn BUF_SIZE chars,
but numChars chars. This check is omitted.
*/
while((c = myStream.read(buf)) != EOF) {
// do something with the bytes
}
}
catch(IOException ioe) {
System.out.println("Error opening file " + FILENAME);
}
}
}

This discussion is closed to new replies.