Import java. io. *; import java. util. logging. level; import java. util. logging. logger; public class io1 {public static void main (String [] a) {File fl = new File ("F: \ self-compiled java program \ 123 \ src \ java \ hug \ gytg. java "); // open a file and use the file path on the disk as the parameter System. out. println ("absolute path" + fl. getAbsolutePath (); // The fl object provides many methods about gytg file information: System. out. println ("file size" + fl. length (); // length indicates the number of bytes in the file. io1 io1 = new io1 (); io1.chuanjian ();} public vo Id chuanjian () {// create the li.txt File fl2 = new File ("f:/li.txt") to drive f; if (! Fl2.exists () {// check whether the file exists. try {fl2.createNewFile (); // create this file} catch (IOException ex) {Logger. getLogger (io1.class. getName ()). log (Level. SEVERE, null, ex) ;}} else {System. out. println ("this File already exists");} File fl3 = new File ("f:/ff"); if (fl3.isDirectory () {// check whether the folder System already exists. out. println ("folder already exists");} else {fl3.mkdir (); // create a folder} File fl4 = new File ("F: \ fast press "); if (fl4.isDirectory () {File file [] = fl4.listFiles (); // fl4 calls listFiles () and returns the File list (an array) in the folder ), in an array of the file type, for (int I = 0; I <file. length; I ++) {// cyclically output the file name System. out. println ("file name:" + file [I]. getName ());}}}}
Example 2:
Import java. io. *; import java. util. logging. level; import java. util. logging. logger; public class io2 {public static void main (String [] s) throws IOException {io2 io2 = new io2 (); io2.io1 ();} public void io1 () throws IOException {// byte stream can process any data File fl1 = new File ("F:/li.txt"); // here is a File object (because it will be read later, so it must exist, otherwise it will be abnormal) File fl2 = new File ("F:/li3.txt"); // find this File. If you want to create a File, use mkdir () method FileInputStream FCM = null; try {/ /FileInputStream file input stream (from your own point of view, you are the input stream) fiis = new FileInputStream (fl1); // because the file class does not have the read/write function, therefore, you must use FileInputStream to read files (read data from the fl1 file) byte [] bt = new byte [1024]; // define an array to cache int I = 0; while (I = Fi. read (bt ))! =-1) {// here I indicates the number of bytes in the array and reads the data to bt (from the FileInputStream perspective, from the bt perspective) string st = new String (bt, 0, I); // convert the byte into a String System. out. print (st);} // FCM. close (); it cannot be put here, because if an exception is thrown, it will be skipped} catch (FileNotFoundException ex) {Logger. getLogger (io2.class. getName ()). log (Level. SEVERE, null, ex);} finally {FS. close ();}}}
Example 3:
Import java. io. *; import java. util. logging. level; import java. util. logging. logger; public class io3 {public static void main (String [] s) throws IOException {io3 io3 = new io3 (); io3.io1 (); io3.io2 ();} public void io1 () {// write data to the text File fl1 = new File ("F:/li.txt"); // if the File already exists, it will overwrite it (because the code below is to write something to this File, in the case of io2 File fl1 = new File ("F: // li.txt"); fl1 will not be replaced, because the code behind it is read) FileOutputStream ops = null; try {ops = new File OutputStream (fl1); // FileOutputStream byte output stream (output from your own perspective). The output stream writes data to fl1 (that is, F:/li.txt) string s = "yyuhuh"; // s. getBytes () converts a string to byte ops. write (s. getBytes (); // output s. Principle: Write the bytes of s as the input stream (because data must be written before writing) then, input the stream // from the s perspective. FileOutputStream is the input stream, but from the fl1 text perspective, FileOutputStream is the output stream} catch (FileNotFoundException ex) {} catch (IOException ex) {Logger. getLogger (io3.class. getName ()). log (Level. SEVERE, null, ex );} Public void io2 () throws FileNotFoundException {// copy a FileInputStream to a new FileInputStream ("E: \ wallpaper \ Picture \ 14606556_847849.jpg "); // The output stream is FileOutputStream fos = null; byte bt [] = new byte [125*1025]; // FileInputStream is the input stream from the image perspective, but from the array perspective, FileInputStream is the output stream int I = 0; try {while (I = Fi. read (bt ))! =-1) {fos = new FileOutputStream ("f: \ ff \ a.jpg"); // output to this file fos. write (bt); // because bt itself is transmitted in bytes, you do not need to use s in the io1 () method above. getBytes ()} catch (IOException ex) {Logger. getLogger (io3.class. getName ()). log (Level. SEVERE, null, ex);} finally {try {FCM. close (); fos. close ();} catch (IOException ex) {Logger. getLogger (io3.class. getName ()). log (Level. SEVERE, null, ex) ;}}} I am a student. I just wrote my understanding here. If you have any mistakes, please give me some advice, I also hope this article will be helpful to other students.