黑马程序员--IO【1】
生活随笔
收集整理的這篇文章主要介紹了
黑马程序员--IO【1】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
-------------------?android培訓、java培訓、期待與您交流!------------------
------------------------------------------------------------------------------------------------------------
/* Runtime對象 該類并沒有提供構造函數。 說明不可以new對象。那么會直接想到該類中的方法都是靜態的。 發現該類中還有非靜態方法。 說明該類肯定會提供了方法獲取本類對象。而且該方法是靜態的,并返回值類型是本類類型。 ? 由這個特點可以看出該類使用了單例設計模式完成。 ? ? ? ? 該方式是static Runtime getRuntime(); */ ? class ?RuntimeDemo { public static void main(String[] args) throws Exception { Runtime r = Runtime.getRuntime(); //java命令打開電腦里任意一個文件 ? //Process p = r.exec("winmine.exe");//可以不寫路徑,默認當前路徑找,找不到,再在其他盤找 //Thread.sleep(4000);//讓線程等4s,再殺掉子進程 //p.destroy();//殺掉子進程,剛一運行掃雷,就被秒殺掉了 ? Process p = r.exec("notepad.exe ?SystemDemo.java");//用記事本打開**.java文件 } }
------------------------------------------------------------------------------------------------------------?
? import java.util.*; import java.text.*;//日期格式化在這個包 class DateDemo? { public static void main(String[] args)? { Date d = new Date(); System.out.println(d);//打印的時間看不懂,希望有些格式。 ? //將模式封裝到SimpleDateformat對象中。 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss"); //調用format方法讓模式格式化指定Date對象。 String time = sdf.format(d); ? System.out.println("time="+time); long l = System.currentTimeMillis(); ? Date d1 = new Date(l); ? System.out.println("d1:"+d1); } }
------------------------------------------------------------------------------------------------------------?
import java.util.*; import java.text.*; class ?CalendarDemo { public static void main(String[] args)? { Calendar c = Calendar.getInstance(); String[] mons = {"一月","二月","三月","四月" ,"五月","六月","七月","八月" ,"九月","十月","十一月","十二月"};//查表法~! String[] weeks = { "","星期日","星期一","星期二","星期三","星期四","星期五","星期六", };//第一天是星期日,第二天是星期一....,前面加個空格解決! int index = c.get(Calendar.MONTH); int index1 = c.get(Calendar.DAY_OF_WEEK); sop(c.get(Calendar.YEAR)+"年"); //sop((c.get(Calendar.MONTH)+1)+"月");//從零開始 sop(mons[index]); sop(c.get(Calendar.DAY_OF_MONTH)+"日"); //sop("星期"+c.get(Calendar.DAY_OF_WEEK)); sop(weeks[index1]); /* Date d = new Date(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy"); String year = sdf.format(d); System.out.println(year); */ } public static void sop(Object obj) { System.out.println(obj); } }
------------------------------------------------------------------------------------------------------------?
import java.util.*; /* 兩個練習: 1,獲取任意年的二月有多少天。 思路:根據指定年設置一個時間就是? c.set(year,2,1)//某一年的3月1日。 c.add(Calendar.DAY_OF_MONTH,-1);//3月1日,往前推一天,就是2月最后一天。 2,獲取昨天的現在這個時刻。 c.add(Calenar.DAY_OF_MONTH,-1); */ class ?CalendarDemo2 { public static void main(String[] args)? { Calendar c = Calendar.getInstance(); //c.set(2012,2,23);//0代表1月------2代表3月 c.add(Calendar.DAY_OF_MONTH,-18);//對時間量的偏移! printCalendar(c); } public static void printCalendar(Calendar c) { String[] mons = {"一月","二月","三月","四月" ,"五月","六月","七月","八月" ,"九月","十月","十一月","十二月"}; String[] weeks = { "","星期日","星期一","星期二","星期三","星期四","星期五","星期六", }; int index = c.get(Calendar.MONTH); int index1 = c.get(Calendar.DAY_OF_WEEK); sop(c.get(Calendar.YEAR)+"年"); //sop((c.get(Calendar.MONTH)+1)+"月"); sop(mons[index]); sop(c.get(Calendar.DAY_OF_MONTH)+"日"); //sop("星期"+c.get(Calendar.DAY_OF_WEEK)); sop(weeks[index1]); } public static void sop(Object obj) { System.out.println(obj); } }
------------------------------------------------------------------------------------------------------------?
/* 練習。給定一個小數。 保留該小數的后兩位。 選作。可以考慮,保留時進行四舍五入。 */ import java.util.*; class ?MathDemo { public static void main(String[] args)? { /*-------必要掌握!!!! Random r = new Random();//大于等于0,小于1的偽隨機數【有算法】 for(int x=0; x<10; x++) { //int d = (int)(Math.random()*10+1);//1到10的隨機數 int d = r.nextInt(10)+1;//"1-10"----不包括 sop(d); } */ saveTwo(12.3456,2,true);//12.35 } public static void saveTwo(double d,int scale,boolean isRound) { double base = Math.pow(10,scale); double num = isRound?Math.round(d*base)/base:((int)(d*base))/base; sop("num="+num); /* double d1 = d*100; sop("d1="+d1); d1 = d1+0.5; double d2 = (int)d1; sop("d2="+d2); double d3 = d2/100; sop("d3="+d3); */ } public static void show()//常見的方法!! { double d = Math.ceil(16.34);//cell返回大于指定數據的最小整數。 double d1 = Math.floor(16.67);//floor返回小于指定數據的最大整數 long l = Math.round(12.34);//四舍五入 sop("d="+d); sop("d1="+d1); sop("l="+l); double d2 = Math.pow(2,3);//2的3次方 sop("d2="+d2);//結果:8 } public static void sop(Object obj) { System.out.println(obj); } }
------------------------------------------------------------------------------------------------------------?
/* 字符流和字節流: 字節流兩個基類: InputStream ? OutputStream 字符流兩個基類: Reader Writer 先學習一下字符流的特點。 既然IO流是用于操作數據的, 那么數據的最常見體現形式是:文件。 那么先以操作文件為主來演示。 需求:在硬盤上,創建一個文件并寫入一些文字數據。 找到一個專門用于操作文件的Writer子類對象。FileWriter。 ?后綴名是父類名。 前綴名是該流對象的功能。 */ import java.io.*; class ?FileWriterDemo { public static void main(String[] args) throws IOException { //創建一個FileWriter對象。該對象一被初始化就必須要明確被操作的文件。 //而且該文件會被創建到指定目錄下。----如果該目錄下已有同名文件,將被覆蓋。 //其實該步就是在明確數據要存放的目的地。 FileWriter fw = new FileWriter("demo.txt"); //調用write方法,將字符串寫入到流中【內存中】。 fw.write("abcde"); //刷新流對象中的緩沖中的數據。 //將數據刷到目的地中。 //fw.flush();//這時,Demo.txt中會有abcde //fw.write("haha"); //fw.flush();//繼續寫 //關閉流資源,但是關閉之前會刷新一次內部的緩沖中的數據。 //將數據刷到目的地中。 //和flush區別:flush刷新后,流可以繼續使用,close刷新后,會將流關閉。 fw.close();//最終要做的動作 } }
------------------------------------------------------------------------------------------------------------?
/* IO異常的處理方式。 */ import java.io.*; class ?FileWriterDemo2 { public static void main(String[] args)? { FileWriter fw = null; try { //這倆異常一起處理 fw = new FileWriter("demo2.txt");// fw.write("abcdefg");// } catch (IOException e) { System.out.println("catch:"+e.toString()); } finally { try { if(fw!=null)//fw為空不能調用close fw.close();// } catch (IOException e) { System.out.println(e.toString()); } } } }
------------------------------------------------------------------------------------------------------------?
/* 演示對已有文件的數據續寫。 */ import java.io.*; class ?FileWriterDemo3 { public static void main(String[] args) throws IOException//開發時不能拋,要寫FileWriterDemo2的異常處理!!!!! { //傳遞一個true參數,代表不覆蓋已有的文件。并在已有文件的末尾處進行數據續寫。 FileWriter fw = new FileWriter("demo2.txt",true); fw.write("nihao\r\nxiexie");//windos中記事本的換行是\r\n; ? ? linux中是\n; fw.close(); } }
------------------------------------------------------------------------------------------------------------?
import java.io.*; class ?FileReaderDemo { public static void main(String[] args) throws IOException { //創建一個文件讀取流對象,和指定名稱的文件相關聯。 //要保證該文件是已經存在的,如果不存在,會發生異常FileNotFoundException FileReader fr = new FileReader("demo.txt"); //調用讀取流對象的read方法。 //read():一次讀一個字符。而且會自動往下讀。 int ch = 0; while((ch=fr.read())!=-1)//判斷是不是到達末尾,-1代表結束---//fr.read()運行一次,返回一個字符 { System.out.println( } /* while(true) { int ch = fr.read(); if(ch==-1)//讀到末尾就break break; System.out.println("ch="+(char)ch);//char強轉成字符 } */ fr.close(); } }
------------------------------------------------------------------------------------------------------------? ?
? /* 第二種方式:通過字符數組進行讀取。 */ ? import java.io.*; ? class FileReaderDemo2? { public static void main(String[] args) throws IOException { FileReader fr = new FileReader("demo.txt"); ? //定義一個字符數組。用于存儲讀到的字符。 //該read(char[])返回的是讀到字符個數。 char[] buf = new char[1024];//一個字符兩字節,所以這個是2K ? int num = 0; while((num=fr.read(buf))!=-1)//讀取幾個,fr.read()返回幾 { System.out.println(new String(buf,0,num));//讀到幾個,打印幾個 } ? fr.close(); } }
?
------------------------------------------------------------------------------------------------------------??
? ? //讀取一個.java文件,并打印在控制臺上。 import java.io.*; ? class FileReaderTest? { public static void main(String[] args) throws IOException { FileReader ?fr = new FileReader("DateDemo.java"); ? char[] buf = new ?char[1024]; ? int num = 0; ? while((num=fr.read(buf))!=-1) { System.out.print(new String(buf,0,num)); } ? fr.close(); } ? }
------------------------------------------------------------------------------------------------------------???
//將C盤一個文本文件復制到D盤。 ? /* 復制的原理: 其實就是將C盤下的文件數據存儲到D盤的一個文件中。 ? 步驟: 1,在D盤創建一個文件。用于存儲C盤文件中的數據。 2,定義讀取流和C盤文件關聯。 3,通過不斷的讀寫完成數據存儲。 4,關閉資源。 */ ? import java.io.*; ? class CopyText? { public static void main(String[] args) throws IOException { //copy_1(); copy_2(); } ? ? public static void copy_2() { FileWriter fw = null; FileReader fr = null; try { fw = new FileWriter("SystemDemo_copy.txt"); fr = new FileReader("SystemDemo.java"); ? char[] buf = new char[1024];//定義緩沖區 ? int len = 0; while((len=fr.read(buf))!=-1) { fw.write(buf,0,len); } } catch (IOException e) { throw new RuntimeException("讀寫失敗"); } ? finally { if(fr!=null) try { fr.close(); } catch (IOException e) { } ? if(fw!=null) try { fw.close(); } catch (IOException e) { } } } ? //從C盤讀一個字符,就往D盤寫一個字符。 public static void copy_1()throws IOException { //創建目的地。 FileWriter fw = new FileWriter("RuntimeDemo_copy.txt"); ? //與已有文件關聯。 FileReader fr = new FileReader("RuntimeDemo.java"); ? int ch = 0; ? while((ch=fr.read())!=-1) { fw.write(ch); } fw.close(); fr.close(); ? } ? ? } ?
?
轉載于:https://www.cnblogs.com/Stone-sw/archive/2013/03/21/2974009.html
總結
以上是生活随笔為你收集整理的黑马程序员--IO【1】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MFC,QT与WinForm,WPF简介
- 下一篇: 今天正式开通51CTO技术博客