Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import java.io.IOException;
public class Demo4 {
public static void Main (string[] args) {
GetFile ();
}
public static void GetFile () {
1. Locate the target file
File File = new file ("D:\\a.txt");
2. Establishing a Channel
FileInputStream inputstream = null;
try {
InputStream = new FileInputStream (file);
Byte[] B = new byte[1024];
try {
int count = Inputstream.read (b);
System.out.println (New String (B,0,count));
} catch (IOException e) {
System.out.println ("Out of error");
throw new RuntimeException (e);
}
} catch (FileNotFoundException e) {
SYSTEM.OUT.PRINTLN ("File does not exist");
Prompt user for error to modify
Let the code behind it be customized to run
System.exit (0); Not very good, generally is not free to launch virtual machines.
throw new RuntimeException (e); Creating a run-time exception
}finally {
try {
Inputstream.close ();
} catch (IOException e) {
System.out.println ("Shutdown failed");
throw new RuntimeException (e);
}
}
}
}
Exception Handling for IO streams