In the past two days, I have learned how to read files in Java. First of all, I have practiced the simplest TXT file. In addition, I gradually learned how Io streams operate files in Java.
After practicing for a while, I began to read images. The idea was as follows: images are also a type of file. I can use the fileinputstream class to operate images. I can use this method for testing (note that reading an image file to a byte [] is acceptable, when writing data to an image file from this byte [], you should call the write (byte [], offset, length) method, otherwise, the stream cannot be written into another image file ).
The problem occurs in the next operation.
Because I started to read files, first for the TXT test, first for the byte stream, and second for the ghost stream. When I got to the image, I also subconsciously followed this method, as a result, I searched and viewed the API on the Internet many times and found that I was most likely in the midst of a big headache I designed for myself: pictures, can I use the volume stream to read the program?
(Add a section:
The byte stream is an official encapsulation of byte streams. It is simplified to make it easier for developers to read text. However, the storage of images can be understood as byte streams. Therefore, reading images with the seek stream is a kind of behavior that you dig yourself and then jump in and find a way to jump out. Therefore, this mode is not good for reading images with the ghost stream. Sorry for the trouble!
)
First, four types of file operations are provided in Java: inputstream outputstream reader writer. The first two types are byte stream operations, the last two operations are performed on the upstream stream.
So what is a byte stream and a byte stream?
All files are physically stored in the computer, which is actually a binary 01 code. That is to say, byte streams and byte streams only distinguish files logically. What are the differences between the two streams? The encoding method is different.
At this point, we will not continue to analyze the encoding details, and our knowledge is not that deep.
Continue back to the original image question. Can I use the ghost stream to read the image? I failed the test, and I have always been expecting this method to appear.
My friend told me that I could use a binary file to read images. I was reading images in binary format. How does it sound so awkward?
I started searching on the Internet and finally saw an article on csdn. It helped me a lot, depending on my needs and my cognition, reading an image from a binary file is actually reading an image with throttling.
So far, I think the problem of reading images with binary files has been solved. In fact, the problem is to use the class provided in Java to operate byte streams to manipulate image files.
I am very sorry that I have not attached the code and run it. Now Add the following:
Import Java. io. *; import javax. swing. *; public class start {public static void main (string ARGs []) throws exception {// source file, which must exist. The path can be file Sf = new file ("h: /javapro/files/source.jpg "); // target file. To write data to the file, the specified file may not exist. The program generates file df = new file (" h: /javapro/files/dest.jpg "); New readwritegra (SF, DF); New usegra (DF) ;}} class readwritegra {fileinputstream in = NULL; fileoutputstream out = NULL; public readwritegra (File sour Cefile, file destfile) throws exception {byte [] Buf = new byte [1024]; int Len = 0; In = new fileinputstream (sourcefile); out = new fileoutputstream (destfile, true); While (LEN = in. read (BUF ))! =-1) {out. write (BUF, 0, Len);} Out. close () ;}} class usegra extends jframe {public usegra (File picfile) throws exception {This. setvisible (true); this. setresizable (false); this. setlayout (null); this. setbounds (600,200,400,370); this. setdefaclocloseoperation (dispose_on_close); jpanel p1 = (jpanel) This. getcontentpane (); p1.setopaque (false); p1.setlayout (null); inputstream is = new fileinputstream (picfile); bytearr Ayoutputstream baos = new bytearrayoutputstream (); int B = 0; while (B = is. Read ())! =-1) {baos. write (B);} imageicon image = new imageicon (baos. tobytearray (); jlabel background = new jlabel (image); this. getlayeredpane (). add (background, new INTEGER (integer. min_value); background. setbounds (0, 0, image. geticonwidth (), image. geticonheight (); jbutton bt = new jbutton ("test_button"); p1.add (BT); BT. setbounds (,); Validate ();}}
Description: The upper left part is the file directory behind the program running. After the program is passed, dest.jpg is generated. The lower left part is the background of a jframe filled with the generated dest.jpg, and the lower right part is the running of the program.
Compiling environment: editplus