当前位置:首页 > 实验15
实验十五、输入输出流实验
一、实验目的
⒈了解文件的概念和文件对象的创建方法
⒉了解FileInputStream和FileOutoutStream的基本概念 ⒊学会创建文件输入输出流
⒋掌握使用文件输入输出流读写文件的方法
二、实验内容
⒈编写程序读取文本文件内容
编辑TestFileDialog.java,设保存在D:\\myjava目录下。 import java.io.*; import java.awt.*;
import java.awt.event.*; import javax.swing.*;
import javax.imageio.IIOException; public class TestFileDialog {
public static void main(String args[ ]) {
new FileFrame(); } }
class FileFrame extends Frame implements ActionListener {
TextArea ta;
Button open,quit; FileDialog fd; FileFrame() {
super(\获取显示文本文件\ ta=new TextArea(10,45);
open=new Button(\打开\ quit=new Button(\关闭\
open.addActionListener(this); quit.addActionListener(this); setLayout(new FlowLayout()); add(ta); add(open); add(quit); show(); }
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand()==\打开\
fd=new FileDialog(this,\打开文件\ fd.setDirectory(\ fd.show(); try {
File myfile=new File(fd.getDirectory(),fd.getFile()); System.out.print(myfile);
BufferedReader inB=new BufferedReader(myfile); String fileCountent=\
While((fileCountent==inB.readLine())!=null) {
str=str+fileCountent+\ ta.append(fileCountent+\ } }
catch(IIOException ie)
{System.out.println(ie.toString()); } }
if (e.getActionCommand()==\关闭\ {dispose();
System.exit(0); } } }
(1) 编译并运行。
(2) 选择a.txt查看应用程序。
运行结果:
⒉ 读取图像文件
编辑ReadPic.java。设保存在D:myjava目录下。设该目录下已经存在一个run.gif文件(可以在此目录下放置一个本机已有的图像文件名字为run.gif。) import java.io.*;
class ReadPic {
//*此程序将当前目录下的run.gif文件复制到copyrun.gif中 ReadPic() { try{
File f=new File(\
File f1=new File(\
FileInputStream inFile=new FileInputStream(f);
BufferedInputStream inB=new BufferedInputStream(inFile); FileOutputStream outFile=new FileOutputStream(f1);
BufferedOutputStream putB=new BufferedOutputStream(outFile);
byte b[ ]=new byte[ (int) f.length()]; while(inB. read(b)!=-1) {
putB.write(b); }
putB.flush(); inB.close(); putB.close(); }
catch(Exception e) {e.printStackTrace();} }
public static void main(String args[ ]) {
new ReadPic(); }
(1) }编译并运行ReadPic..class。
(2) 打开copyrun.gif与run.gif进行对比。 ⒊将内容写入文件
编辑UseStream1.java,设保存在D:\\myjava目录下。 import java.io.*; class UseStream1 {
UseStream1(String path) {
try{
File f=new File(path, \
//向文件test1.txt中写入数据
FileWriter putFile=new FileWriter(f);
BufferedWriter OutB=new BufferedWriter(putFile); String s=\你们好,这是一个测试写入数据的文件。\ OutB.write(s);
//插入一行
OutB.newLine();
OutB.write(\这是利用FileWrite与 BuffereWrite的例题。\ //需要调用flush()方法 OutB.flush(); //写入完毕要关闭流 OutB.close();
//从text.txt中读取数据
FileReader inFile=new FileReader(f);
BufferedReader inB=new BufferedReader(inFile);
/*inB中含有能够直接读取一行数据的方法raesLine()供我们使用,当然返回值null时,意味着读取结束*/
String fileContent=\
while((fileContent=inB.readLine())!=null) {
str=str+fileContent+\ }
System.out.println(str); inB.close(); }
catch(Exception e){e.printStackTrace();} }
public static void main(String args[ ]) {
new UseStream1(\ }
}编译并运行UseStream1.class。
实验结果:
三、实验心得与体会
在编程中引入文件的使用可以方便数据的导出使用,通过这次实验,我了解了文件的概念和文件对象的创建方法,了解了FileInputStream和FileOutoutStream的基本概念,学会了创建文件输入输出流,掌握了使用文件输入输出流读写文件的方法。在文件的使用时要注意文件的路径。
共分享92篇相关文档