- The FileInputStream and FileOutputStream classes are used to create the input and output stream objects for disk files, using their constructors to specify the file path and file name.
- When you create an FileInputStream instance object, the specified file should be present and readable. When you create an FileOutputStream instance object, the original content in this file is overwritten if the specified file already exists.
- Two ways to create a FileInputStream object on the same disk file: (1) FileInputStream inone = new FileInputStream ("Hello.test"); (2) File F = new file ("Hello.test"); FileInputStream intwo = new FileInputStream (f);
- When you create an FileOutputStream instance object, you can specify a file name that does not already exist, but you cannot specify a file that has been opened by another program.
Think: To write the contents of a file to B file, in the program code, is the output class object, or the input class object to connect a file and complete the operation of a file? (remember: the concept of input and output here is relative to this application, not relative to the file.) such as) so we should create an input class to read the contents of a file, and then create an output class to output the content to the B file. Programming Example: Write a string character to a file with the FileOutputStream class and read the written content with FileInputStream. //filestream.java import Java.io.*; public class filestream{ public static void main ( String[] args) throws Exception { FileOutputStream out = new FileOutputStream (" Hello.txt "); out.write (" www.sina.com.cn ". GetBytes ()); //converts a string into a byte array and writes to the stream out.close (); byte[] buf = new byte[1024]; file F = new file ("Hello.txt"); fileinputstream in = new FileInputStream (f); int len = In.read (BUF); //reads content into byte array System.out.println (new string (Buf,0,len)); //string constructor converts a byte array to a string, note: buf should be a characterArray (char[]), byte array (byte[]), integer array (int[]) means to take the Len elements from the array starting with subscript 0 to generate a string object, print in.close () ; }} reader class and writer class
- Reader and writer are abstract base classes for all character stream classes that simplify the input-output programming of strings, which is used to read and write text data.
- The difference between a binary file and a text file. Strictly speaking, each file in the file system is a binary file. Various text characters are made up of one or several bytes, where the data for each byte cannot be arbitrary. If each byte in a file or data in each contiguous number of bytes can be represented as a character, we can call this file a text file. A visible text file is just a special case of a binary file. To distinguish it from text files, people refer to files other than text files as binary files. Conceptually we can simply assume that if a file is dedicated to storing text characters without any data other than characters, it is called a text file, except that the file is a binary file.
The reader and writer classes and their subclasses (FileReader and FileWriter classes, etc.) are primarily used to read text-formatted content, while the InputStream and OutputStream classes and their subclasses primarily read the contents of binary formats. Programming Example: Write a string character to a file with the FileWriter class and read the written content with FileReader. //filestream2.java import Java.io.*; public class filestream2{ public static void main ( String[] args) throws Excetpion { FileWriter out = new FileWriter ("Hello2.txt"); & nbsp; out.write ("www.sina.com.cn"); //here you can write the string directly without converting it to a byte array Out.close (); char[] buf = new char[1024]; //character array FileReader in = new FileReader ("Hello2.txt"); int len = In.read ( BUF); //at this time the Read method can read one character or several characters, and Len represents the number of characters actually read. System.out.println (new String (buf,0,1024)); //string constructor converts a character array to a string. in.close (); }}
Use of FileInputStream and FileOutputStream classes, reader classes, and writer classes