【精品】输出流和文件流【精品】输出流和文件流
南昌大学计算机科学技术系学生上机实践报告
一、 实验目的
练习使用JAVA语言编程,学习如何使用JAVA语言的事件处理和文件读取功能创建和使用输出流
和文件流。
二、实验内容与要求
编写一个JAVA Application程序,包括一个“打开”按钮和一个TextArea,用户点击按钮时将弹出FileDialog,请用户选择一个文本文件,然后关闭FileDialog,程序把这个文本文件的内容显示在TextArea中。
三、实验平台
P4 computer ,Winxp操作系统 ,jcreat...
【精品】输出流和文件流
南昌大学计算机科学技术系学生上机实践
一、 实验目的
练习使用JAVA语言编程,学习如何使用JAVA语言的事件处理和文件读取功能创建和使用输出流
和文件流。
二、实验
与要求
编写一个JAVA Application程序,包括一个“打开”按钮和一个TextArea,用户点击按钮时将弹出FileDialog,请用户选择一个文本文件,然后关闭FileDialog,程序把这个文本文件的内容显示在TextArea中。
三、实验平台
P4 computer ,Winxp操作系统 ,jcreator 3.5
四、 源程序如下
package WindowsApplication6;
import System.Collections.Generic.*; import System.Data.*;
import System.Drawing.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import java.io.*;
/**
* Summary description for Form1.
*/
public class Form1 extends System.Windows.Forms.Form
{
private TextBox textBox1;
private Button button1;
private OpenFileDialog openFileDialog1;
/**
* Required designer variable.
*/
private System.ComponentModel.IContainer components;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
#region Windows Form Designer generated code
/**
* Clean up any resources being used.
*/
protected void Dispose(boolean disposing)
{
if (disposing)
{
第 1 页 共 4 页
南昌大学计算机科学技术系学生上机实践报告
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}
/**
* Required method for Designer support - do not modify
* the contents of this method with the code editor.
*/
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.set_Location(new System.Drawing.Point(12, 12));
this.textBox1.set_Multiline(true);
this.textBox1.set_Name("textBox1");
this.textBox1.set_Size(new System.Drawing.Size(199, 189));
this.textBox1.set_TabIndex(0);
//
// button1
//
this.button1.set_Location(new System.Drawing.Point(238, 12));
this.button1.set_Name("button1");
this.button1.set_Size(new System.Drawing.Size(75, 23));
this.button1.set_TabIndex(1);
this.button1.set_Text("打开");
this.button1.set_UseVisualStyleBackColor(true);
this.button1.add_Click(new System.EventHandler(this.button1_Click));
//
// openFileDialog1
//
this.openFileDialog1.set_FileName("openFileDialog1");
//
// Form1
//
this.set_AutoScaleDimensions(new System.Drawing.SizeF(6F, 12F));
this.set_AutoScaleMode(System.Windows.Forms.AutoScaleMode.Font);
this.set_ClientSize(new System.Drawing.Size(338, 258));
this.get_Controls().Add(this.button1);
this.get_Controls().Add(this.textBox1);
this.set_Name("Form1");
this.set_Text("卢强 02081156");
this.ResumeLayout(false);
第 2 页 共 4 页
南昌大学计算机科学技术系学生上机实践报告
this.PerformLayout();
}
#endregion
private void button1_Click(Object sender, System.EventArgs e)
{
try
{ string x="";
openFileDialog1.ShowDialog();
String name=openFileDialog1.get_FileName();
FileInputStream f=new FileInputStream(name);
DataInputStream d=new DataInputStream(f);
while(d.available()!=0)
{
x=x+d.readLine()+"\n";
}
textBox1.set_Text(x);
}
catch (Exception ee)
{
}
}
}
五、 实验结果
第 3 页 共 4 页
南昌大学计算机科学技术系学生上机实践报告
六、 实验心得
第 4 页 共 4 页
本文档为【【精品】输出流和文件流】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。