Java IO stream (character stream)
File Open
Read file
Close File
//Open File//Read File contents//Close FileImportJava.io.*; Public classindex{ Public Static voidMain (string[] args)throwsexception{//Open File//character Stream mode open//Character streams are read by one character at a timeFileReader WJ =NewFileReader ("D:/java/kj/javanew/src/index.java"); //byte stream mode open//byte stream is read at a time//FileInputStream WJ = new FileInputStream ("D:/java/kj/javanew/src/index.java"); //Read File contents intAa//This must be defined as shaping, and Java specifies that the return value of the Read () method in Io is shapedAA = Wj.read ();//read one characterString content = ""; while(Aa!= (-1)) {//The ASCII code is a number starting at 0, and only nothing will return-1Content + = (Char) AA; AA= Wj.read ();//Continue reading one character } //Output File ContentsSystem.out.println ("File content is:"); System.out.print (content); //Close FileWj.close (); }}
Java IO stream (character stream) file open, read file, close file