Java世界

          學習筆記

          導航

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          留言簿(11)

          收藏夾

          隨筆檔案

          文章檔案

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          積分與排名

          天籟村

          新華網

          雅虎

          最新評論

          海運項目: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 閱讀(510) 評論(1)  編輯  收藏

          評論

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

          砌磚  回復  更多評論   


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


          網站導航:
           
          主站蜘蛛池模板: 海口市| 郯城县| 广宗县| 奎屯市| 称多县| 慈溪市| 全州县| 马边| 吕梁市| 泉州市| 乐安县| 阿坝县| 南宫市| 天津市| 金湖县| 招远市| 邳州市| 广西| 旬阳县| 确山县| 杨浦区| 镇宁| 无棣县| 万安县| 会宁县| 崇阳县| 山东| 渑池县| 太原市| 宜章县| 阿克陶县| 万载县| 台中县| 土默特左旗| 奎屯市| 页游| 扬中市| 蚌埠市| 肥城市| 栖霞市| 姜堰市|