Java世界

学习笔记

常用链接

统计

积分与排名

天籁村

新华网

雅虎

最新评论

海运项目:ExporterBL类

package com.sinojava.haiyun;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.*;
import java.net.*;
//客户端类
public class ExporterBL {
    //定义相应的私有变量
    private Frame frame;
    private TextArea display;
    private Panel panel;
    private ScrollPane sp;
    private Button button,buttonsave;
    private FileDialog dialog,dialogsave;
    private String str1,str2;
    private String strContext;
    private String line;
    private String result="";
    private int count = 0;
    ArrayList list = new ArrayList();
    private ObjectOutputStream output;
    private ObjectInputStream input;
    private Socket soc;
    private String chatServer;
    //输出信息
    private String message = "";
    private String a[];
    
    public ExporterBL(String host) {
        //创建客户端GUI界面
        chatServer = host;
        frame = new Frame("出口预配集装箱货物信息导入");
        frame.setLayout(new BorderLayout());
        panel = new Panel();
        panel.setLayout(new FlowLayout());
        button = new Button("打开");
        buttonsave = new Button("保存");
        panel.add(button);
        panel.add(buttonsave);
        sp = new ScrollPane();
        display = new TextArea();
        sp.add(display);
        dialog = new FileDialog(frame,"打开",FileDialog.LOAD);
        dialogsave = new FileDialog(frame,"保存",FileDialog.SAVE);
        frame.add(sp,BorderLayout.CENTER);
        frame.add(panel,BorderLayout.SOUTH);
        frame.setSize(500,500);
        frame.setLocation(200, 200);
        frame.setVisible(true);
        frame.pack();
        //退出程序
        frame.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialog.show();
                //获取文件的路径和文件名
                str1 = dialog.getDirectory()+dialog.getFile();
                //发送字符数据给服务器端
                sendData(readFile(str1));
            }
        });
        //对文本进行保存
        buttonsave.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                dialogsave.show();
                str2 = dialogsave.getDirectory()+dialogsave.getFile();
                String strContext = display.getText();
                writeFile(str2,strContext);
            }
        });    
    }
    
    //连接服务器处理信息
       public void runClient() {
          try {
              // 设置JTextArea不能被更改
              display.setEditable(false);
             // 第一步:创建一个Socket,连接服务器
             connectToServer();
             // 第二步:获取接受数据
             getStreams();
             // 第三步:连接处理
             processConnection();
             // 第四步:关闭连接
             closeConnection();
          }
          catch (IOException e) {
             e.printStackTrace();
          }
       }
       
       //获取接受数据
       private void getStreams() throws IOException {
          output = new ObjectOutputStream(soc.getOutputStream());
          output.flush();      
          input = new ObjectInputStream(soc.getInputStream());
          display.append("\n获得I/O流\n");
       }
       // 创建一个Socket,连接服务器
       private void connectToServer() throws IOException {      
          display.setText("连接中\n");
          //InetAddress类采用工厂设计模式,有三个静态工厂方法,如getByName。
          soc = new Socket(InetAddress.getByName(chatServer),5000);
          display.append("连接到:"+soc.getInetAddress().getHostName());
       }
       // 连接处理
       private void processConnection() throws IOException {
          do {
             try {
                //接收服务器端传回来的字符数据
                message = (String)input.readObject();
                //显示到TextArea上
                display.append("\n"+message);
                display.setCaretPosition(display.getText().length());
             }
             catch(ClassNotFoundException e) {
                display.append("\n没有对象接受");
             }
          } while(true);
       }  
       // 关闭连接
       private void closeConnection() throws IOException {
          display.append("\n关闭连接");
          output.close();
          input.close();
          soc.close();
       }
       // 发送信息到服务器
       private void sendData(String message) {
          try {
             output.writeObject(message);
             output.flush();
          }
          catch (IOException e) {
             display.append("\n错误的对象");
          }
       }   
    
    //读取文件内容
    public String readFile(String s) {
        try {
            FileReader fr = new FileReader(s);
            BufferedReader br = new BufferedReader(fr);
            while((line=br.readLine())!=null) {
                    result+=line+"\n";
            }                  
            fr.close();
            br.close();
        }catch(IOException e) {
            System.out.println("Error:"+e.getMessage());
        }
        return result;
    }
    //写入文件内容
    public void writeFile(String s,String ss) {
        try {
            PrintWriter out = new PrintWriter(new FileWriter(s),true);            
            out.println(ss+"\n");
            out.flush();
            out.close();    
        }catch(IOException e) {
            System.out.println("Error:"+e.getMessage());
        }
    }
    //main方法
    public static void main(String[] args) {
        ExporterBL ebl = new ExporterBL("127.0.0.1");
        //运行客户端
        ebl.runClient();
    }
}

posted on 2007-11-16 14:04 Rabbit 阅读(498) 评论(1)  编辑  收藏

评论

# re: 海运项目:ExporterBL类 2009-09-27 12:14 向军

砌砖  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航: