Computing.Net > Forums > Programming > reading in a file in java

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

reading in a file in java

Reply to Message Icon

Name: tony
Date: July 21, 2003 at 06:34:49 Pacific
OS: xp pro
CPU/Ram: 256
Comment:

Hi,
is it possible in Java to read in a file such as a jpeg and store the binary data of the file within an int array (assuming it would just be lots of 1's and 0's).
help would be appreciated :)



Sponsored Link
Ads by Google

Response Number 1
Name: Giuseppe
Date: July 22, 2003 at 08:33:23 Pacific
Reply:


Yeah, you can do it.

Have a look at classes in java.io: InputStream and DataInputStream. They offer 'read' methods which reads data from disk and put it into byte arrays.

You can use these classes in a Java standalone program. You can't gereally use them in a untrusted applet.

Giuseppe


0

Response Number 2
Name: tony
Date: July 23, 2003 at 14:32:58 Pacific
Reply:

thanks very much, i'll try that then :)


0

Response Number 3
Name: tony
Date: July 24, 2003 at 07:41:59 Pacific
Reply:

i have tried using the datainput stream but have run into a little trouble,

The code i have returns an unsigned byte value where as i would prefer a sequence of 10101 etc. the code i have so far is below. Is there anyway to just get java to return a sequence of the binary values and store it within an array? or do i need to convert each number into an 8 digit binary number?


import java.io.*;

class Fileatemmpt
{
public static void main(String args[])
{
if (args.length == 1)
{
try
{
FileInputStream fstream = new FileInputStream(args[0]);
DataInputStream in = new DataInputStream(fstream);

while (in.available() !=0)
{
System.out.println (in.readUnsignedByte());
}
in.close();
}
catch (Exception e)
{
System.err.println("File input error");
}
}
else System.out.println("Invalid parameters");
}

}



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: reading in a file in java

move a file from network in java? www.computing.net/answers/programming/move-a-file-from-network-in-java/4144.html

Reading from a text file in C++ www.computing.net/answers/programming/reading-from-a-text-file-in-c/8983.html

open , read a file in binary format www.computing.net/answers/programming/open-read-a-file-in-binary-format/6944.html