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);
}
}
第五节 字节流的高级应用
管道流
管道用来把一个程序、线程和代码块的输出连接到另一个程序、线程和代码块的输入。java.io中提供了类PipedInputStream和PipedOutputStream作为管道的输入/输出流
管道输入流作为一个通信管道的接收端,管道输出流则作为发送端。管道流必须是输入输出并用,即在使用管道前,两者必须进行连接
管道输入/输出流可以用两种方式进行连接:
– 在构造方法中进行连接
• PipedInputStream(PipedOutputStream pos);
• PipedOutputStream(PipedInputStream pis);
– 通过各自的connect()方法连接
• 在类PipedInputStream中,connect(PipedOutputStream pos);
• 在类PipedOutputStream中,connect(PipedInputStream pis);
例 8.8 管道流。
本例例管道流的使用方法。设输入管道in与输出管道out已连接,Send线程向输出管道out发送数据,Receive线程从输入管道in中接收数据。程序如下:
import java.io.*;
public class Pipedstream
{
public static void main (String args[])
{
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
try
{
in.connect(out);
}
catch(IOException ioe) { }
Send s1 = new Send(out,1);
Send s2 = new Send(out,2);
Receive r1 = new Receive(in);
Receive r2 = new Receive(in);
s1.start();
s2.start();
r1.start();
r2.start();
}
}
class Send extends Thread //发送线程
{
PipedOutputStream out;
static int count=0; //记录线程个数
int k=0;
public Send(PipedOutputStream out,int k)
{
this.out= out;
this.k= k;
this.count++; //线程个数加1
}
public void run( )
{
System.out.print("\r\nSend"+this.k+": "+this.getName()+" ");
int i=k;
try
{
while (i<10)
{
out.write(i);
i+=2;
sleep(1);
}
if (Send.count==1) //只剩一个线程时
{
out.close(); //关闭输入管道流
System.out.println(" out closed!");
}
else
this.count--; //线程个数减1
}
catch(InterruptedException e) { }
catch(IOException e) { }
}
}
class Receive extends Thread //接收线程
{
PipedInputStream in;
public Receive(PipedInputStream in)
{
this.in = in;
}
public void run( )
{
System.out.print("\r\nReceive: "+this.getName()+" ");
try
{
int i = in.read();
while (i!=-1) //输入流未结束时
{
System.out.print(i+" ");
i = in.read();
sleep(1);
}
in.close(); //关闭输入管道流
}
catch(InterruptedException e) { }
catch(IOException e)
{
System.out.println(e);
}
}
}
程序运行结果如下:
Send1: Thread-0
Send2: Thread-1
Receive: Thread-2 1
Receive: Thread-3 2 3 4 5 7 out closed!
6 8 9 java.io.IOException: Pipe closed!
数据流
DataInputStream和DataOutputStream
在提供了字节流的读写手段的同时,
以统一的通用的形式向输入流中写入boolean,int,long,double等基本数据类型,并可以在次把基本数据类型的值读取回来。
提供了字符串读写的手段。
分别实现了DataInput和DataOutput接口
声明类:
Public class DataInputStream extends filterInputStream implements DataInput
例 8.9 数据流。
本例演示数据流的使用方法。
程序如下:
import java.io.*;
public class Datastream
{
public static void main(String arg[])
{
String fname = "student1.dat";
new Student1("Wang").save(fname);
new Student1("Li").save(fname);
Student1.display(fname);
}
}
class Student1
{
static int count=0;
int number=1;
String name;
Student1(String n1)
{
this.count++; //编号自动加1
this.number = this.count;
this.name = n1;
}
Student1()
{
this("");
}
void save(String fname)
分享到:
相关推荐
Java中输入输出的总括
流一般分为输入流(Input Stream)和输出流(Output Stream)两类,但这种划分并不是绝对的。比如一个文件,当向其中写数据时,它就是一个输出流;当从其中读取数据时,它就是一个输入流。当然,键盘只是一个输人流...
NULL 博文链接:https://juliancg65.iteye.com/blog/572174
java io 操作的一个总结,对于初学java的朋友是十分有帮助的
是html中的CSS的总括,包含了html中所有样式的特效,是网页开发人员在网页开发中非常便捷,舒畅。
图书总括登记与个别登记表.pdf
这些词汇一共分为十章,是把所有关于java的英语单词 总括在一起,方便大家记忆
保险基础知识总括.pptx
管理咨询总括汇报.ppt
品管圈知识总括.pptx
会计基础知识点总括.pdf
资产质量分析总括内容.pptx
运维工作总括性结论及总结性报告.pdf运维工作总括性结论及总结性报告.pdf运维工作总括性结论及总结性报告.pdf运维工作总括性结论及总结性报告.pdf运维工作总括性结论及总结性报告.pdf运维工作总括性结论及总结性报告...
运维工作总括性结论及总结性报告.docx运维工作总括性结论及总结性报告.docx运维工作总括性结论及总结性报告.docx运维工作总括性结论及总结性报告.docx运维工作总括性结论及总结性报告.docx运维工作总括性结论及总结...
Java 语言基本上属于一个完全面向对象的语言,并且语言的设计仍然以简捷为重点,因此有其它语言基础的朋友学习Java语言会感觉比较简单,对于这个语言学习的难点则是面向对象的相关概念,包括:包、类、对象、实例、...
2011政治理论逻辑关联总括图:最权威的资料,最高效的复习!!
运维年度工作总括性结论及总结性报告.pdf运维年度工作总括性结论及总结性报告.pdf运维年度工作总括性结论及总结性报告.pdf运维年度工作总括性结论及总结性报告.pdf运维年度工作总括性结论及总结性报告.pdf运维年度...
保险基础知识--总括.pptx