不想把別人的東西佔為己有,但是想方便日後參考還是摘錄了。
煩死了,看Java編程思想三或者四,感覺老外寫書跟我們看書的習慣都不一樣的,總感覺老外寫的東西就像是在寫手冊,全面但是煩瑣。
【原則】不要告訴我曆史,告訴我怎麼做就行了。
【事實】輸出輸入類,就是TMD的簡單,為什麼非要弄成手冊,讓我這個菜鳥看不懂
【鳴謝】中國IT實驗室的總結篇
——————————————————————————————————————————————————————————
字元流Reader類和Writer類
前面說過,在JDK1.1之前,java.io包中的流只有普通的位元組流(以byte為基本處理單位的流),這種流對於以16位的Unicode碼錶示的字元流處理很不方便。從JDK1.1開始, java.io包中加入了專門用於字元流處理的類,它們是以Reader和Writer為基礎派生的一系列類
同類InputStream和OutputStream一樣,Reader和Writer也是抽象類別,只提供了一系列用於字元流處理的介面。它們的方法與類InputStream和OutputStream類似,只不過其中的參數換成字元或字元數組
Reader類
• void close()
• void mark(int readAheadLimit)
• boolean markSupported() :
• int read()
• int read(char[] cbuf)
• int read(char[] cbuf, int off, int len)
• boolean ready()
• void reset()
• long skip(long n)
Writer類
• void close()
• void flush()
• void write(char[] cbuf)
• void write(char[] cbuf, int off, int len)
• void write(int c)
• void write(String str)
• void write(String str, int off, int len)
例 8.7 檔案編輯器。
本例實現檔案編輯器中的開啟、儲存檔案功能。程式如下:
import java.awt.*;
import java.awt.event.*;
import java.io.*;
public class EditFile1 extends WindowAdapter
implements ActionListener,TextListener
{
Frame f;
TextArea ta1;
Panel p1;
TextField tf1;
Button b1,b2,b3;
FileDialog fd;
File file1 = null;
public static void main(String args[])
{
(new EditFile1()).display();
}
public void display()
{
f = new Frame("EditFile");
f.setSize(680,400);
f.setLocation(200,140);
f.setBackground(Color.lightGray);
f.addWindowListener(this);
tf1 = new TextField();
tf1.setEnabled(false);
tf1.setFont(new Font("Dialog",0,20)); //設定文本行的初始字型
f.add(tf1,"North");
ta1 = new TextArea();
ta1.setFont(new Font("Dialog",0,20)); //設定文本區的初始字型
f.add(ta1);
ta1.addTextListener(this); //註冊文本區的事件監聽程式
p1 = new Panel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
b1 = new Button("Open");
b2 = new Button("Save");
b3 = new Button("Save As");
p1.add(b1);
p1.add(b2);
p1.add(b3);
b2.setEnabled(false);
b3.setEnabled(false);
b1.addActionListener(this); //註冊按鈕的事件監聽程式
b2.addActionListener(this);
b3.addActionListener(this);
f.add(p1,"South");
f.setVisible(true);
}
public void textValueChanged(TextEvent e)
{ //實現TextListener介面中的方法,對文本區操作時觸發
b2.setEnabled(true);
b3.setEnabled(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource()==b1) //單擊[開啟]按鈕時
{
fd = new FileDialog(f,"Open",FileDialog.LOAD);
fd.setVisible(true); //建立並顯示開啟檔案對話方塊
if ((fd.getDirectory()!=null) && (fd.getFile()!=null))
{
tf1.setText(fd.getDirectory()+fd.getFile());
try //以緩衝區方式讀取檔案內容
{
file1 = new File(fd.getDirectory(),fd.getFile());
FileReader fr = new FileReader(file1);
BufferedReader br = new BufferedReader(fr);
String aline;
while ((aline=br.readLine()) != null)//按行讀取文本
ta1.append(aline+"/r/n");
fr.close();
br.close();
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
}
if ((e.getSource()==b2) || (e.getSource()==b3))
{ //單擊[儲存]按鈕時
if ((e.getSource()==b3) ||(e.getSource()==b2)&&(file1==null))
{ //單擊[SaveAs]按鈕時,或單擊[Save]按鈕且檔案對象為空白時
fd = new FileDialog(f,"Save",FileDialog.SAVE);
if (file1==null)
fd.setFile("Edit1.txt");
else
fd.setFile(file1.getName());
fd.setVisible(true); //建立並顯示儲存檔案對話方塊
if ((fd.getDirectory()!=null) && (fd.getFile()!=null))
{
tf1.setText(fd.getDirectory()+fd.getFile());
file1 = new File(fd.getDirectory(),fd.getFile());
save(file1);
}
}
else
save(file1);
}
}
public void save(File file1)
{
try //將文本區內容寫入字元輸出資料流
{
FileWriter fw = new FileWriter(file1);
fw.write(ta1.getText());
fw.close();
b2.setEnabled(false);
b3.setEnabled(false);
}
catch (IOException ioe)
{
System.out.println(ioe);
}
}
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
}