<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(504) 評論(1)  編輯  收藏

    評論

    # re: 海運項目:ExporterBL類 2009-09-27 12:14 向軍

    砌磚  回復  更多評論   


    只有注冊用戶登錄后才能發表評論。


    網站導航:
     
    主站蜘蛛池模板: 国产乱子伦精品免费无码专区| 99在线视频免费观看| 免费人成网站永久| 在线看片免费人成视频久网下载| 秋霞人成在线观看免费视频| 日韩国产免费一区二区三区| 妞干网手机免费视频| 亚洲一区二区精品视频| 亚洲国语精品自产拍在线观看| 亚洲a级成人片在线观看| 成人精品国产亚洲欧洲| 最好免费观看高清在线| 中文字幕无码视频手机免费看| 四虎影视在线永久免费观看| 亚洲精品无码MV在线观看| 亚洲午夜精品在线| 特黄特色大片免费| 日韩av无码久久精品免费| 免费看www视频| 精品国产亚洲一区二区三区| 亚洲另类视频在线观看| 免费精品国自产拍在线播放| 伊人久久免费视频| 国产大片91精品免费观看男同| 亚洲AV无码久久寂寞少妇| 亚洲国产成人久久精品软件| 成人爽a毛片免费| 好吊妞视频免费视频| 亚洲国产三级在线观看| 亚洲愉拍一区二区三区| 国产免费av一区二区三区| 亚洲日韩激情无码一区| 香蕉大伊亚洲人在线观看| 巨胸喷奶水www永久免费| 日韩吃奶摸下AA片免费观看| 中文字幕精品无码亚洲字| 亚洲一区二区三区在线网站| 在线免费观看伊人三级电影| 妞干网免费视频观看| 亚洲A∨无码一区二区三区| 美女尿口扒开图片免费|