【基礎篇】java中輸入輸出的總括——字元流

來源:互聯網
上載者:User

不想把別人的東西佔為己有,但是想方便日後參考還是摘錄了。

煩死了,看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);
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.