Import Java.io.File;
Import java.io.FileNotFoundException;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.FileInputStream;
public class Inputstreamdemo {
public static void Main (string[] args) {
File.separator represents "/", the Java virtual opportunity to convert the path delimiter according to the operating system
String info = "D:" +file.separator+ "text.txt";//Path
InputStream is = null;
try {
is = new FileInputStream (info);
Byte[] B = new byte[1024];
int temp = 0;
While ((Temp=is.read (b))!=-1) {
The following can also be written: New String (b); However, because B is 1024, if the file size is not enough 1024 will be filled with spaces
So usually we use the following method:
System.out.println (New String (b,0,temp));
}
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
is.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
public class Outputstreamdemo {
public static void Main (string[] args) {
String path = "D:" +file.separator+ "File" +file.separator+ "Aaa.txt";
File F = new file (path);
OutputStream OS = null;
String info = "This is Sunny today";
try {
F.createnewfile ();
BYTE stream
OS = new FileOutputStream (path,true);//If True means that the original content will not be overwritten, append to the text, and overwrite the original content if it does not add true
Os.write (Info.getbytes ());
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}finally{
try {
Is.close ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
}
Usage of input byte stream (FileInputStream) and output byte stream (FileInputStream)