FileWriter can only accept arguments in the form of strings, which means that the content can only be stored in a text file. Relative to the text of this article
Save more space in binary format file storage
The InputStream is used to read data from the input stream by byte. where the int read () method reads a byte, this byte
Returns a value from 0 to 255 as an integer . Why read a byte without directly returning a value of type byte?
because the highest bit of the byte type is the sign bit, the largest positive integer it can represent is 127.
InputStream is just an abstract class and cannot be instantiated. Filelnputstream is a subclass of InputStream that is used to
Read by byte in the file.
Public Static void throws = "D:/test.txt"new// Create a file object according to the file path // If the file cannot be found, Throws a FileNotFoundException exception new// closes the file input stream and throws a IOException exception if it fails to shut down properly
The Write (int b) method in OutputStream is used to write out data by byte. FileOutputStream is used to set the number of bytes by byte
The file was written. For example, read the content from a file in bytes and write another new file, the file
System functions.
File Fileln =NewFile ("source. TXT ");//Open source FileFile fileout =NewFile ("Target.txt");//Open write files, that is, target file Filelnputstream streamln =NewFilelnputstream (Fileln);//build an input stream from a source fileFileOutputStream streamout =NewFileOutputStream (fileout);//build an output stream from a target fileintC;//reads the data in bytes from the source file, and if the content is not finished reading, continue while((c = Streamln.read ())! = 1{streamout. write (c);//write to destination file}streamln.close. ;//close the input streamStreamout.close ();//turn off the output stream
Determines whether the file already exists and generates the file if it does not exist.
New File (Dicdir + datadic); if (! datafile.exists ()) {// write to file if the file does not exist }
Use the File.mkdirs () method to create a multilevel directory. For example, when a directory does not exist, it is created.
New File (imgpath); if (! tempdir.exists ()) {Tempdir.mkdirs ();}
Java Read file: binary file