Java Programming io simple review and learning

Source: Internet
Author: User

Java Programming IO operation is essential, long time not to play Io, review, write a few small programs, record, easy to access and learn.

1. Give an array of integers, write them to a file, read them from the file, and print in reverse order of integer size.

 Packagecom.io.test;ImportJava.io.BufferedReader;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileReader;ImportJava.io.FileWriter;Importjava.io.IOException;ImportOrg.apache.jasper.tagplugins.jstl.core.ForEach; Public classSortandprint {//gives an array of integers, writes them to a file, reads them from a file, and prints in reverse order of integer size.          Public Static voidMain (string[] args)throwsIOException {int[] arr={3,4,6,78,90,1}; //Create a new fileFile file=NewFile ("D:\\sort.txt"); FileWriter FW=NewFileWriter (file); String String="";  for(inti = 0; i < arr.length; i++) {            intnum =Arr[i]; String+=num+ ",";                } System.out.println (String); Try{fw.write (string);        Fw.flush (); } Catch(IOException e) {e.printstacktrace (); }finally{            Try{fw.close (); } Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); }        }        //read from FileFileReader fr=Newfilereader (file); BufferedReader BFR=NewBufferedReader (FR); StringBuilder SB=NewStringBuilder (); intCH = 0; String Str=NULL;  while((Str=bfr.readline ())! =NULL) {sb.append (str);         } fr.close ();        Bfr.close (); String[] Split= Sb.tostring (). Split (","); int[] INTs =Stringtoint (split); //to sort        int[] Sort2 =sort (ints);  for(inti = sort2.length-1; i>0; i--) {System.out.println (sort2[i]); }    }    //converting a string array to an array of integers     Public Static int[] Stringtoint (string[] strs) {intnum=strs.length; int[] arrs=New int[num];  for(inti = 0; i < strs.length; i++) {Arrs[i]=Integer.parseint (Strs[i]); }        returnArrs; }    //Bubble Sort     Public Static int[] Sort (int[] arr) {        intnum=arr.length; intTemp=0;  for(inti = num-1; i>0; i--) {             for(intj=0;j<i;j++){                //Bubbling Method                if(arr[j]>Arr[i]) {                    //Swap Positiontemp=Arr[j]; ARR[J]=Arr[i]; Arr[i]=temp; }            }        }        returnarr; }        }

2. Traverse all the files in a folder.

 Packagecom.io.test;ImportJava.io.File; Public classFilebianli {//traverse all files in a folder     Public Static voidMain (string[] args) {//get the Directory folder to traverseFile file=NewFile ("e:\\");    Showallfile (file); }         Public Static voidshowallfile (file file) {//get all child filesfile[] Files =File.listfiles (); //determine file type        if(!file.isdirectory ()) {System.out.println ("File:" +File.getabsolutepath ()); }Else{            if(files==NULL|| Files.length==0) {System.out.println ("Empty directory:" +File.getabsolutepath ()); }Else{                //Traverse FilesSystem.out.println ("Directory:" +File.getabsolutepath ());  for(File f:files) {showallfile (f); }            }        }            }}

Java Programming io simple review and learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.