說實話,其實我並不是很喜歡Java這門語言,儘管它很強大,有很多現成的API可以調用
但我總感覺它把簡單的事情弄得太過複雜,甚至有時候會讓人迷失
弄不清到底是為了寫出東西,還是為了語言本身
我學習的第一門程式設計語言是Python,雖然學的不深
但是它的簡單優雅至今令人難忘(呃,其實也就兩年前的事……)
我接觸的第二門語言是C,它給我的感覺是一種純粹,一種高效的靈活
而不是類似java,寫一堆含糊的代碼來實現一個小小的功能
坦白講,如果一個人在學習自己不感興趣的東西,那會很累
支撐我的是,我對移動開發有著異常的嚮往,對Android也相當喜歡,無奈開發Android的主要語言便是Java
雖然已經學了半年,但是我卻到現在都不能解釋神馬企業級開發到底有哪些東西
我想要的,無非就是把編程當作生活中的一種調情,如果想要某個功能,就去實現,用儘可能簡單的方式
甚至我的猜想是,等到日後發展,編程這門技術如同每個人使用office一般的時候
那麼作為程式員的你們是該有多艱辛?
要知道,一個人將自己內心的利益蓋過真正的想法,這是相當可怕的……
因此,每個人都應該用自己喜歡的,覺得高效的方式,做自己最想要做的
好了,說了那麼多,我的目的其實只有一個
難道我會告訴你,我根本就搞不懂,也不想搞懂java那些複雜的垃圾文法嗎?
我只用那些最簡單最好用的東西……
java以前的那些io寫法我就懶得記錄了,主要是System類對IO的支援
如果你覺得我寫的代碼不夠深沉,那你就噴吧,可噴完之後你還能幹些什麼呢?
現在,步入正題……
這一節我們來講Scanner類和PrintWriter類的用法
Scanner類
執行個體1:從鍵盤讀取 複製代碼 代碼如下:import java.util.Scanner;
public class Demo {
public static void main(String[] args ) {
Scanner input = new Scanner(System.in);
System.out.println("請輸出一個整數:");
int i = input.nextInt();
System.out.println("你輸入的整數是:" + i);
}
}
以上示範的只是讀取一個整數,當然還有讀取浮點數和其他資料類型的方法,比較簡單,查看API即可
執行個體2:從字串讀取
複製代碼 代碼如下:import java.util.Scanner;
public class Demo {
public static void main(String[] args ) {
//這裡的\r\n是分行符號,Linux下其實只用\n即可
Scanner input = new Scanner("hello\r\nworld\r\n");
//迴圈讀取,hasNext()方法和集合架構裡面的一樣使
while(input.hasNext()) {
//每次讀取一行,別的讀取方法見API,比較簡單
String s = input.nextLine();
System.out.println(s);
}
}
}
執行個體3:從檔案讀取
複製代碼 代碼如下:import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Demo {
public static void main(String[] args ) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
File f = new File(path);
Scanner input = null;
try {
//從檔案構造Scanner對象,有可能產生異常
input = new Scanner(f);
while(input.hasNext()) {
String s = input.nextLine();
System.out.println(s);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
input.close();
}
}
}
這裡要注意的是,從檔案建立Scanner對象得先要有File對象,當然你可以使用匿名對象來建立
此外,還需捕捉異常和關閉檔案流
PrintWriter類
執行個體4:向檔案寫入內容
複製代碼 代碼如下:import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
public class Demo {
public static void main(String[] args) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
//建立檔案對象
File file = new File(path);
PrintWriter p = null;
try {
//此處建構函式還可以傳其他對象,具體參考API文檔
p = new PrintWriter(file);
//向檔案寫入一行,此外還有print()和printf()方法
p.println("如果有一天我回到從前");
p.println("回到最原始的我");
p.println("你是否會覺得我不錯");
//重新整理流
p.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
p.close();
}
}
}
與PrintWriter類似的還有一個PrintStream類,此處以PrintWriter舉例是因為文字檔具有人為可讀性
而二進位檔案(位元組模式)則需要使用專門的程式來讀取
可能有人會問:FileOutputStream、 FileWriter都能寫檔案,那麼為何還需要PrintWriter和PrintStream類
如果細看API文檔,可以知道前者單純的字元寫入流和位元組寫入流操作的方式大多用數組進行
對檔案的細化處理非常不方便,而PrintWriter和PrintStream則很好的解決了這一問題,提供print()等方法
並且,PrintWriter和PrintStream對於不存在檔案對象的情況下會直接建立,如果已有檔案對象
它們則會把原有檔案給覆蓋掉,卻沒有增加方法
解決這問題也很簡單,再看API文檔
PrintWriter有一個構造方法PrintWriter(Writer out),也就是能夠傳入Writer對象
PrintStream有一個構造方法PrintStream(OutputStream out),也就是能傳入OutputStream對象
因此,我們這樣寫就可以了
new PrintWriter(new FileWriter(file,true))
new PrintStream(new FileOutputStream(file,true))
既能增加資料,也能更高效的處理檔案,見如下代碼示範
執行個體5:實現PrintWriter的資料追加功能
複製代碼 代碼如下:import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class Demo {
public static void main(String[] args) {
String path = File.separator + "home" + File.separator + "siu" +
File.separator + "work" + File.separator + "demo.txt";
//建立檔案對象
File file = new File(path);
PrintWriter p = null;
try {
//利用FileWriter方式構建PrintWriter對象,實現追加
p = new PrintWriter(new FileWriter(file,true));
p.println("尼瑪 這一句就是追加的 看到沒");
p.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
//我們來小心翼翼的關閉流,好吧^_^
p.close();
}
}
}
看,這樣就能實現追加效果了,最後一行便是
System類對IO的支援
執行個體6:System類中的寫入
複製代碼 代碼如下:import java.io.IOException;
import java.io.OutputStream;
public class Demo {
public static void main(String[] args) {
//別忘了,OutputStream是所有位元組寫入流的父類
OutputStream out = System.out;
try {
//寫入資料,只能是數組,所以用getBytes()方法
out.write("Hello,bitch!\r\n".getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
注意,此處正好印證了System.out的覆寫行為
如果想學好io,整個io體系中的多態需要瞭解清楚才能駕輕就熟
執行個體7:System類中的讀取
複製代碼 代碼如下:import java.io.IOException;
import java.io.InputStream;
public class Demo {
public static void main(String[] args) {
//別忘了InputStream是所有位元組輸入資料流的父類
InputStream in = System.in;
System.out.print("請輸入文字: ");
byte[] buf = new byte[1024];
int len = 0;
try {
//將輸入的資料保證到數組中,len記錄輸入的長度
len = in.read(buf);
} catch (IOException e) {
e.printStackTrace();
}
//用字串的方式列印數組中的資料
System.out.println("你的輸入是: " + new String(buf,0,len));
}
}
看,這樣就能從鍵盤擷取內容並且列印了
需要注意的是,這裡的數組大小是1024位元組
一旦輸入的資料超過1024位元組,那麼超過的內容將被截取掉,所以此程式有局限性
並且,一個中文佔兩個位元組,輸入中文有時候會被意外截取掉
相信我,每個程式都是俺親自編寫編譯的~!!!
執行個體8:利用BufferedReader實現對鍵盤的讀取
複製代碼 代碼如下:import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Demo {
public static void main(String[] args) {
BufferedReader b = new BufferedReader(new InputStreamReader(System.in));
System.out.print("請輸入文本:");
try {
String str = b.readLine();
System.out.println("你輸入的是:" + str);
} catch (IOException e) {
e.printStackTrace();
}
//迴圈讀取方式
/*
while(true) {
System.out.print("請輸入文本:");
String str = null;
try {
str = b.readLine();
} catch (IOException e) {
e.printStackTrace();
}
//如果輸入over就結束迴圈
if("over".equals(str)) {
break;
}
System.out.println("你輸入的是:" + str);
}
*/
try {
//關閉流,不耐煩的就直接拋
b.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
這樣做相對於上面一個方法的好處是:不用關心數組大小的問題
BufferedReader有一個最重要的方法就是readLine(),每次讀取一行