JAVA课件总结(3)

2025-11-14

try{ File f=new File(\ File of=new File(\ of.createNewFile();

FileInputStream in=new FileInputStream(f);

FileOutputStream out=new FileOutputStream(of); while((b=in.read(tom,0,25))!=-1)

{ String s=new String (tom,0,b); System.out.print(s);

out.write(s.getBytes()); }

in.close(); }

catch(IOException e)

{ System.out.println(\ } }

//字符型输入输出流的使用 public class ReaderTest { public static void main(String args[ ]) { File file=new File(\ String content[]={\你好:\近来工作忙吗?\常联系\祝好\ try{ FileWriter outOne=new FileWriter(file); BufferedWriter outTwo= new BufferedWriter(outOne); for(int k=0;k

}

//使用RandomAccessFile字节节点流 public class RandomAccesssTest {

public static void main(String args[]) { new CommFrame(); } }

class InputArea extends Panel implements ActionListener { File f=null;

RandomAccessFile out;

Box baseBox ,boxV1,boxV2; TextField name,email,phone; Button button; InputArea(File f)

{ //setBackground(Color.cyan); this.f=f;

name=new TextField(12); email=new TextField(12); phone=new TextField(12); button=new Button(\录入\

button.addActionListener(this); boxV1=Box.createVerticalBox(); boxV1.add(new Label(\输入姓名\

boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\输入email\ boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\输入电话\

boxV1.add(Box.createVerticalStrut(8)); boxV1.add(new Label(\单击录入\ boxV2=Box.createVerticalBox(); boxV2.add(name);

boxV2.add(Box.createVerticalStrut(8)); boxV2.add(email);

boxV2.add(Box.createVerticalStrut(8)); boxV2.add(phone);

boxV2.add(Box.createVerticalStrut(8)); boxV2.add(button);

baseBox=Box.createHorizontalBox(); baseBox.add(boxV1);

baseBox.add(Box.createHorizontalStrut(10)); baseBox.add(boxV2); add(baseBox); }

public void actionPerformed(ActionEvent e) { try{

RandomAccessFile out=new RandomAccessFile(f,\ if(f.exists())

{ long length=f.length();

out.seek(length); }

out.writeUTF(\姓名:\ out.writeUTF(\ out.writeUTF(\电话:\ out.close(); }

catch(IOException ee){} } }

class CommFrame extends JFrame implements ActionListener { File file=null; JMenuBar bar; JMenu fileMenu;

JMenuItem 录入,显示; JTextArea show;

InputArea inputMessage;

CardLayout card=null; //卡片式布局. JPanel pCenter; CommFrame() { super(\ Container c=this.getContentPane(); file=new File(\通讯录.txt\ 录入=new JMenuItem(\录入\ 显示=new JMenuItem(\显示\ bar=new JMenuBar();

fileMenu=new JMenu(\操作\ fileMenu.add(录入); fileMenu.add(显示); bar.add(fileMenu);

this.setJMenuBar(bar);

录入.addActionListener(this); 显示.addActionListener(this);

inputMessage=new InputArea(file); show=new JTextArea(12,20); card=new CardLayout(); pCenter=new JPanel(); pCenter.setLayout(card);

pCenter.add(\录入\ pCenter.add(\显示\

add(pCenter,BorderLayout.CENTER);

addWindowListener(new WindowAdapter()

{ public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true);

setBounds(100,50,420,380); validate(); }

public void actionPerformed(ActionEvent e) { if(e.getSource()==录入)

{ card.show(pCenter,\录入\ }

else if(e.getSource()==显示) { int number=1;

show.setText(null);

card.show(pCenter,\显示\

try{ RandomAccessFile in=new RandomAccessFile(file,\ String 姓名=null;

while((姓名=in.readUTF())!=null)

{ show.append(\姓名);

show.append(in.readUTF()); //读取email. show.append(in.readUTF()); //读取phone

show.append(\ number++; }

in.close(); }

catch(Exception ee){} } } }

5、实验

(1)将保存在本地机当前文件夹中的a.txt文本文件的内容在屏幕上显示出来,然后将其

另存为b.txt 文件。

分别利用字节型输入输出流和字符型输入输出流来完成。

(2)保存对象信息到文件,并将文件中的对象信息显示出来。

import java.io.*;

public class ObjectTest implements Serializable{ //序列化接口

int bh=1; int nl=21; String xm; //构造方法

ObjectTest(int bh,String xm,int nl){ }

//保存到文件中的方法 void save(String fname){

try{

FileOutputStream fout = new FileOutputStream(fname);//输出文件流 ObjectOutputStream out = new ObjectOutputStream(fout);//输出对象流 out.writeObject(this); //写入对象 out.close(); this.bh = bh; this.xm = xm; this.nl = nl;

}

}

}catch (FileNotFoundException fe){} catch (IOException ioe){}

//显示文件中对象信息的方法

void display(String fname){ }

public static void main(String arg[]){ }

String fname = \ObjectTest.obj\

ObjectTest oriObj = new ObjectTest(1,\张驰\oriObj.save(fname); oriObj.display(fname); try {

FileInputStream fin = new FileInputStream(fname);//输入文件流 ObjectInputStream in = new ObjectInputStream(fin);//输入对象流 ObjectTest oriObj = (ObjectTest)in.readObject(); //读取对象 System.out.println(\类名: \System.out.println(\ \in.close();

oriObj.getClass().getInterfaces()[0]);

}catch (FileNotFoundException fe){} catch (IOException ioe){}

catch (ClassNotFoundException ioe) {}

(2)理解打开和另存为对话框的使用,并自己编程实现,(参照实例)

第十章 网络编程

1、URL:URL规范了WWW资源网络地址的表示方法。一个URL包含两部分内容:协议名和资源名。://:/#

URL工作原理:URL对象创建后,即可通过它访问指定的WWW资源。这时需调用URL类的OpenStream()方法与指定的URL建立连接并返回一个InputStream类的对象,这样访问WWW资源的操作就转变为I/O操作,然后即可用字节流的方式读取WWW资源数据。 2、InetAddress类:netAddress类又称互连网地址类,该类的对象是封装计算机名称和IP地址的实例。

3、套接字编程:计算机网络常见应用模式:客户端/服务器(Client/Server,C/S)模式;浏览器/服务器(Brower/Server,B/S)模式。在C/S模式下,客户向服务器发出服务请求,服务器接收到请求后,提供相应的服务:客户端部分、服务器部分、“请求/响应”的工作模式 Socket:由一个地址(标识Internet上的计算机)和一个端口号(标识正在计算机上运行的进程) 来标识的网络通讯端点,利用这个端口在程序间进行数据传输。

作用:套接字是工作在是网络协议传输层的接口,利用套接字将网络作为流对象,就象对文件操作一样对这个流进行操作。

分类:流套接字:提供面向连接的、可靠的数据传输服务;数据报套接字:提供无连接的数据传输服务

4、基于TCP/IP的Socket通信模式:每一个基于TCP/IP的程序都赋予了一个端口号(0~65535),通过不同的端口号,区别服务器上运行的每一个应用程序和所提供的服务。注:习惯上将低于1024的端口号保留给系统服务使用


JAVA课件总结(3).doc 将本文的Word文档下载到电脑 下载失败或者文档不完整,请联系客服人员解决!

下一篇:建筑给水排水工程期末复习 - 图文

相关阅读
本类排行
× 游客快捷下载通道(下载后可以自由复制和排版)

下载本文档需要支付 7

支付方式:

开通VIP包月会员 特价:29元/月

注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:xuecool-com QQ:370150219