Text files: You can open an edited file with an editor
Binaries: Files that are only readable by the program that cannot be edited with the editor.
Text I/O requires encoding and decoding.
Binary I/O does not require encoding and decoding, so it is more efficient.
i/o***************************** of text files
public class FileInput {
public static void Main (string[] args) throws FileNotFoundException {
TODO auto-generated Method Stub
File File = new file ("A.txt");//If you do not have the file, automatically create
System.out.println (File.exists ());///Determine if the file exists
System.out.println (File.length ());///Return file size
System.out.println (File.canread ());///Judging the readability of the file
System.out.println (File.isdirectory ());////file object represents a directory
System.out.println (File.isabsolute ());////file file is not an absolute path
System.out.println (File.ishidden ());///Determine if the file is hidden
System.out.println (File.getabsolutepath ());///Get the absolute path to the file
System.out.println (New Date (File.lastmodified ()))////To get the file the last time the modification was taken
Writing data to a file
Try (printwriter print = new PrintWriter (file);) {
Print.print ("Hello,i am from newyork!"); /write String
Print.println (123456); Write value
}
Reading data from a file, console output
Try (Scanner input = new Scanner (file);
){
System.out.println (Input.next ());
while (Input.hasnext ()) {
System.out.println (Input.next ());
}
}
}
}
Binary file i/o********************
Abstract class InputStream: reads the root class of binary data. Abstract class OutputStream: The root class for writing binary data
FileOutputStream is a subclass of OutputStream that writes data to a file
FileInputStream is a subclass of InputStream that reads data from a file
Try-with-resources declares and creates an input-output stream that can be automatically closed after use
Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import java.io.IOException;
public class Demo1 {
//Binary All methods are declared to throw java.io.IOException or jav.io.IOException subclasses
public static void Main (string[] args) throwsIOException{
Try (fileoutputstream output = new FileOutputStream ("C:\\users\\cortana\\desktop\\myfile1");) {
for (int i = 0; I <=100; i++) {
Output.write (i); ToSpecify a path (under Windows)The number of files written to 0-100
}
}
Try (fileoutputstream output = new FileOutputStream ("Temp.txt");) {
for (int i = +; I <=200; i++) {
Output.write (i); ToDefault PathThe number of file Temp.txt written to 100-200
}
}
Try (fileinputstream input = new FileInputStream ("C:\\users\\cortana\\desktop\\myfile1");) {
int value;
while ((Value=input.read ())!=-1) {
System.out.println (value+ ""); Read data from a file, in console output
}
System.out.println ();
}
Try (fileinputstream input = new FileInputStream ("Temp.txt");) {
int value;
while ((Value=input.read ())!=-1) {
System.out.println (value+ ""); Read data from a file, in console output
}
System.out.println ();
}
}
}
Java text file I/O, binary file I/O