public void run() { try { theInputStream = new BufferedReader(new InputStreamReader(client.getInputStream())); theOutputStream = new PrintStream(client.getOutputStream()); while (true) { readin = theInputStream.readLine(); chat.ta.append(readin + "\n"); } } catch (SocketException e) { chat.ta.append("连接中断!\n"); chat.clientBtn.setEnabled(true); chat.serverBtn.setEnabled(true); chat.tfaddress.setEnabled(true); chat.tfport.setEnabled(true); try { i--; skt.close(); client.close(); } catch (IOException err) { chat.ta.append(err.toString()); } } catch (IOException e) { chat.ta.append(e.toString()); } }
private int Load_Edges(DataInputStream inStream, int num) { String line; String item1, item2, item3; int source_node; int dest_node; int value; int n_nodes, edge_cnt; // Node nodes_array[] = new Node[num]; // Wil Edge edge; n_nodes = num; Node nodes_array[]; nodes_array = new Node[n_nodes]; nodes_array = this.Node_Array(); // Wil edge_cnt = 0; try { while ((line = inStream.readLine()) != null) { StringTokenizer Data = new StringTokenizer(line, " "); item1 = Data.nextToken(); item2 = Data.nextToken(); item3 = Data.nextToken(); source_node = Integer.parseInt(item1); dest_node = Integer.parseInt(item2); value = Integer.parseInt(item3); edge = new Edge(nodes_array[source_node - 1], nodes_array[dest_node - 1], value); this.Add_Edge(edge); edge_cnt++; } // inFile.close(); } catch (IOException e) { System.err.println("Error in accessing URL: " + e.toString()); System.exit(1); } return edge_cnt; }
public void actionPerformed(ActionEvent e) { int index = list.getSelectedIndex(); if (index == -1) { return; } int returnVal = fc.showOpenDialog(fileBackupProgram.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); Path directory = file.toPath(); try { destination.setText("Destination Directory: " + directory.toRealPath()); // destField.setValue(directory.toRealPath()); } catch (IOException x) { printer.printError(x.toString()); } directoryList.getDirectory(index).setDestination(directory); startButton.setEnabled(true); } else { log.append("Open command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); }
public Server(int port, Face chat) { try { this.port = port; skt = new ServerSocket(port); this.chat = chat; } catch (IOException e) { chat.ta.append(e.toString()); } }
private int Load_Nodes(DataInputStream inStream) { // need to open file and load data int node_id; int x_cor; int y_cor; // int n_nodes, n_edges, node_cnt, arrow_status; Node n; String line; String item1, item2, item3, item4; node_id = 0; x_cor = 0; y_cor = 0; // n_nodes = 0; // n_edges = 0; // arrow_status = -1; try { if ((line = inStream.readLine()) != null) { StringTokenizer Data = new StringTokenizer(line, " "); item1 = Data.nextToken(); n_nodes = Integer.parseInt(item1); item2 = Data.nextToken(); n_edges = Integer.parseInt(item2); item3 = Data.nextToken(); arrow_status = Integer.parseInt(item3); // item4 = Data.nextToken(); // type = Integer.parseInt( item4 ); // graph = new GraphClass( n_nodes, n_edges, arrow_status ); nodes = new Node[n_nodes]; edges = new Edge[n_edges]; // ??? while ((this.Node_Cnt() < n_nodes) && ((line = inStream.readLine()) != null)) { Data = new StringTokenizer(line, " "); item1 = Data.nextToken(); item2 = Data.nextToken(); item3 = Data.nextToken(); node_id = Integer.parseInt(item1); x_cor = Integer.parseInt(item2); y_cor = Integer.parseInt(item3); n = new Node(node_id, x_cor, y_cor); this.Add_Node(n); } if (n_nodes != 0) { source_node = nodes[0]; } } } catch (IOException e) { System.err.println("error in file" + e.toString()); System.exit(1); } return this.Node_Cnt(); }
/* (non-Javadoc) * @see java.awt.print.Printable#print(java.awt.Graphics, java.awt.print.PageFormat, int) */ public int print(Graphics g, PageFormat pf, int page) throws PrinterException { int ret = PAGE_EXISTS; String line = null; try { if (fph.knownPage(page)) { in.seek(fph.getFileOffset(page)); line = in.readLine(); } else { long offset = in.getFilePointer(); line = in.readLine(); if (line == null) { ret = NO_SUCH_PAGE; } else { fph.createPage(page); fph.setFileOffset(page, offset); } } if (ret == PAGE_EXISTS) { // Seite ausgeben, Grafikkontext vorbereiten Graphics2D g2 = (Graphics2D) g; g2.scale(1.0 / RESMUL, 1.0 / RESMUL); int ypos = (int) pf.getImageableY() * RESMUL; int xpos = ((int) pf.getImageableX() + 2) * RESMUL; int yd = 12 * RESMUL; int ymax = ypos + (int) pf.getImageableHeight() * RESMUL - yd; // Seitentitel ausgeben ypos += yd; g2.setColor(Color.black); g2.setFont(new Font("Monospaced", Font.BOLD, 10 * RESMUL)); g.drawString(fbname + " Seite " + (page + 1), xpos, ypos); g.drawLine( xpos, ypos + 6 * RESMUL, xpos + (int) pf.getImageableWidth() * RESMUL, ypos + 6 * RESMUL); ypos += 2 * yd; // Zeilen ausgeben g2.setColor(new Color(0, 0, 127)); g2.setFont(new Font("Monospaced", Font.PLAIN, 10 * RESMUL)); while (line != null) { g.drawString(line, xpos, ypos); ypos += yd; if (ypos >= ymax) { break; } line = in.readLine(); } } } catch (IOException e) { throw new PrinterException(e.toString()); } return ret; }
/** Save a Drawing in a file */ protected void saveDrawing(StorageFormat storeFormat, String file) { // Need a better alert than this. if (view() == null) { return; } try { String name = storeFormat.store(file, view().drawing()); view().drawing().setTitle(name); setDrawingTitle(name); } catch (IOException e) { showStatus(e.toString()); } }
public void actionPerformed(ActionEvent e) { // Write to disk with FileOutputStream try { FileOutputStream f_out = new FileOutputStream(LOG_DIRECTORY + "\\save.data"); // Write object with ObjectOutputStream ObjectOutputStream obj_out = new ObjectOutputStream(f_out); // Write object out to disk obj_out.writeObject(directoryList); log.setCaretPosition(log.getDocument().getLength()); } catch (IOException x) { printer.printError(x.toString()); } }
public void actionPerformed(ActionEvent e) { try { FileOutputStream f_out = new FileOutputStream(LOG_DIRECTORY + "\\save.data"); // Write object with ObjectOutputStream ObjectOutputStream obj_out = new ObjectOutputStream(f_out); // Write object out to disk obj_out.writeObject(directoryList); obj_out.writeObject(ERROR_LOG_NAME); obj_out.writeObject(MOVE_LOG_NAME); obj_out.flush(); obj_out.close(); printer.printError(LOG_DIRECTORY); } catch (IOException x) { printer.printError(x.toString()); } System.exit(0); }
@Override public void windowClosing(WindowEvent e) { try { FileOutputStream f_out = new FileOutputStream(LOG_DIRECTORY + "\\save.data"); // Write object with ObjectOutputStream ObjectOutputStream obj_out = new ObjectOutputStream(f_out); // Write object out to disk obj_out.writeObject(directoryList); obj_out.writeObject(ERROR_LOG_NAME); obj_out.writeObject(MOVE_LOG_NAME); obj_out.flush(); obj_out.close(); printer.printError(LOG_DIRECTORY); } catch (IOException x) { printer.printError(x.toString()); } }
public void run() { try { // initialize the statusbar status.removeAll(); JProgressBar progress = new JProgressBar(); progress.setMinimum(0); progress.setMaximum((int) f2.length()); status.add(progress); status.revalidate(); // try to start reading Reader in = new FileReader(f2); char[] buff = new char[4096]; int nch; while ((nch = in.read(buff, 0, buff.length)) != -1) { doc2.insertString(doc2.getLength(), new String(buff, 0, nch), null); progress.setValue(progress.getValue() + nch); } // we are done... get rid of progressbar // doc2.addUndoableEditListener(undoHandler); status.removeAll(); status.revalidate(); // resetUndoManager(); } catch (IOException e) { System.err.println("TextViewer:FileLoader " + e.toString()); } catch (BadLocationException e) { System.err.println("TextViewer:FileLoader " + e.getMessage()); } /* aa if (elementTreePanel != null) { SwingUtilities.invokeLater(new Runnable() { public void run() { elementTreePanel.setEditor(getEditor()); } }); } */ }
private void saveToFile() { JFileChooser chooser = new JFileChooser(); String saveName = clazz.getFileName(); saveName = saveName.substring(0, saveName.lastIndexOf('.')) + ".java"; chooser.setSelectedFile(new File(saveName)); int returnVal = chooser.showSaveDialog(this); if (returnVal != JFileChooser.APPROVE_OPTION) { JOptionPane.showMessageDialog( this, "No file specified", "Try again...", JOptionPane.WARNING_MESSAGE); return; } try { PrintWriter pw = new PrintWriter(new FileOutputStream(chooser.getSelectedFile())); pw.println(source); pw.close(); } catch (IOException ioe) { JOptionPane.showMessageDialog( this, ioe.toString(), "Input/Output Exception", JOptionPane.ERROR_MESSAGE); System.exit(1); } }
public void actionPerformed(ActionEvent e) { int returnVal = fc.showOpenDialog(fileBackupProgram.this); if (returnVal == JFileChooser.APPROVE_OPTION) { File file = fc.getSelectedFile(); Path directory = file.toPath(); directoryList.addNewDirectory(directory, 3600000, 14400000, copy); try { listModel.addElement(directory.toRealPath()); } catch (IOException x) { printer.printError(x.toString()); } int index = list.getSelectedIndex(); if (index == -1) { list.setSelectedIndex(0); } index = list.getSelectedIndex(); Directory dir = directoryList.getDirectory(index); if (dir.copy) { copyButton.setSelected(true); } else { moveButton.setSelected(true); } destButton.setEnabled(true); checkField.setValue(dir.getInterval()); waitField.setValue(dir.getWaitInterval()); checkField.setEditable(true); waitField.setEditable(true); } else { log.append("Open command cancelled by user." + newline); } log.setCaretPosition(log.getDocument().getLength()); }
public void run() { chat.ta.append("等待连线......"); while (true) { try { client[k] = skt.accept(); if (i < 2) { ServerThread server[] = new ServerThread[10]; server[k] = new ServerThread(client[k], this.chat, i); l = server.length; server[k].start(); chat.ta.append("客户端" + client[k].getInetAddress() + "已连线\n"); theOutputStream = new PrintStream(server[k].getClient().getOutputStream()); i = server[k].getI(); k++; } else { } } catch (SocketException e) { } catch (IOException e) { chat.ta.append(e.toString()); } } }
/** Submits POST command to the server, and reads the reply. */ public boolean post( String url, String fileName, String cryptToken, String type, String path, String content, String comment) throws IOException { String sep = "89692781418184"; while (content.indexOf(sep) != -1) sep += "x"; String message = makeMimeForm(fileName, cryptToken, type, path, content, comment, sep); // for test // URL server = new URL("http", "localhost", 80, savePath); URL server = new URL(getCodeBase().getProtocol(), getCodeBase().getHost(), getCodeBase().getPort(), url); URLConnection connection = server.openConnection(); connection.setAllowUserInteraction(false); connection.setDoOutput(true); // connection.setDoInput(true); connection.setUseCaches(false); connection.setRequestProperty("Content-type", "multipart/form-data; boundary=" + sep); connection.setRequestProperty("Content-length", Integer.toString(message.length())); // System.out.println(url); String replyString = null; try { DataOutputStream out = new DataOutputStream(connection.getOutputStream()); out.writeBytes(message); out.close(); // System.out.println("Wrote " + message.length() + // " bytes to\n" + connection); try { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String reply = null; while ((reply = in.readLine()) != null) { if (reply.startsWith("ERROR ")) { replyString = reply.substring("ERROR ".length()); } } in.close(); } catch (IOException ioe) { replyString = ioe.toString(); } } catch (UnknownServiceException use) { replyString = use.getMessage(); System.out.println(message); } if (replyString != null) { // System.out.println("---- Reply " + replyString); if (replyString.startsWith("URL ")) { URL eurl = getURL(replyString.substring("URL ".length())); getAppletContext().showDocument(eurl); } else if (replyString.startsWith("java.io.FileNotFoundException")) { // debug; when run from appletviewer, the http connection // is not available so write the file content if (path.endsWith(".draw") || path.endsWith(".map")) System.out.println(content); } else showStatus(replyString); return false; } else { showStatus(url + " saved"); return true; } }
public fileBackupProgram(JFrame frame) { super(new BorderLayout()); this.frame = frame; errorDialog = new CustomDialog(frame, "Please enter a new name for the error log", this); errorDialog.pack(); moveDialog = new CustomDialog(frame, "Please enter a new name for the move log", this); moveDialog.pack(); printer = new FilePrinter(); timers = new ArrayList<>(); log = new JTextArea(5, 20); log.setMargin(new Insets(5, 5, 5, 5)); log.setEditable(false); JScrollPane logScrollPane = new JScrollPane(log); Object obj; copy = true; listModel = new DefaultListModel(); // destListModel = new DefaultListModel(); directoryList = new directoryStorage(); // Create a file chooser fc = new JFileChooser(); // Create the menu bar. menuBar = new JMenuBar(); // Build the first menu. menu = new JMenu("File"); menu.getAccessibleContext() .setAccessibleDescription("The only menu in this program that has menu items"); menuBar.add(menu); editError = new JMenuItem("Save Error Log As..."); editError .getAccessibleContext() .setAccessibleDescription("Change the name of the error log file"); editError.addActionListener(new ErrorListener()); menu.add(editError); editMove = new JMenuItem("Save Move Log As..."); editMove .getAccessibleContext() .setAccessibleDescription("Change the name of the move log file"); editMove.addActionListener(new MoveListener()); menu.add(editMove); exit = new JMenuItem("Exit"); exit.getAccessibleContext().setAccessibleDescription("Exit the Program"); exit.addActionListener(new CloseListener()); menu.add(exit); frame.setJMenuBar(menuBar); // Uncomment one of the following lines to try a different // file selection mode. The first allows just directories // to be selected (and, at least in the Java look and feel, // shown). The second allows both files and directories // to be selected. If you leave these lines commented out, // then the default mode (FILES_ONLY) will be used. // fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // fc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); openButton = new JButton(openString); openButton.setActionCommand(openString); openButton.addActionListener(new OpenListener()); destButton = new JButton(destString); destButton.setActionCommand(destString); destButton.addActionListener(new DestListener()); // Create the save button. We use the image from the JLF // Graphics Repository (but we extracted it from the jar). saveButton = new JButton(saveString); saveButton.setActionCommand(saveString); saveButton.addActionListener(new SaveListener()); URL imageURL = getClass().getResource(greenButtonIcon); ImageIcon greenSquare = new ImageIcon(imageURL); startButton = new JButton("Start", greenSquare); startButton.setSize(60, 20); startButton.setHorizontalTextPosition(AbstractButton.LEADING); startButton.setActionCommand("Start"); startButton.addActionListener(new StartListener()); imageURL = getClass().getResource(redButtonIcon); ImageIcon redSquare = new ImageIcon(imageURL); stopButton = new JButton("Stop", redSquare); stopButton.setSize(60, 20); stopButton.setHorizontalTextPosition(AbstractButton.LEADING); stopButton.setActionCommand("Stop"); stopButton.addActionListener(new StopListener()); copyButton = new JRadioButton("Copy"); copyButton.setActionCommand("Copy"); copyButton.setSelected(true); copyButton.addActionListener(new RadioListener()); moveButton = new JRadioButton("Move"); moveButton.setActionCommand("Move"); moveButton.addActionListener(new RadioListener()); ButtonGroup group = new ButtonGroup(); group.add(copyButton); group.add(moveButton); // For layout purposes, put the buttons in a separate panel JPanel optionPanel = new JPanel(); GroupLayout layout = new GroupLayout(optionPanel); optionPanel.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout.createSequentialGroup().addComponent(copyButton).addComponent(moveButton)); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(copyButton) .addComponent(moveButton))); JPanel buttonPanel = new JPanel(); // use FlowLayout layout = new GroupLayout(buttonPanel); buttonPanel.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(openButton) .addComponent(optionPanel)) .addComponent(destButton) .addComponent(startButton) .addComponent(stopButton) // .addComponent(saveButton) ); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(openButton) .addComponent(destButton) .addComponent(startButton) .addComponent(stopButton) // .addComponent(saveButton) ) .addComponent(optionPanel)); buttonPanel.add(optionPanel); /* buttonPanel.add(openButton); buttonPanel.add(destButton); buttonPanel.add(startButton); buttonPanel.add(stopButton); buttonPanel.add(saveButton); buttonPanel.add(listLabel); buttonPanel.add(copyButton); buttonPanel.add(moveButton); */ destButton.setEnabled(false); startButton.setEnabled(false); stopButton.setEnabled(false); // Add the buttons and the log to this panel. // add(logScrollPane, BorderLayout.CENTER); JLabel listLabel = new JLabel("Monitored Directory:"); listLabel.setLabelFor(list); // Create the list and put it in a scroll pane. list = new JList(listModel); list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); list.setSelectedIndex(0); list.addListSelectionListener(this); list.setVisibleRowCount(8); JScrollPane listScrollPane = new JScrollPane(list); JPanel listPane = new JPanel(); listPane.setLayout(new BorderLayout()); listPane.add(listLabel, BorderLayout.PAGE_START); listPane.add(listScrollPane, BorderLayout.CENTER); listSelectionModel = list.getSelectionModel(); listSelectionModel.addListSelectionListener(new SharedListSelectionHandler()); // monitored, destination, waitInt, check destination = new JLabel("Destination Directory: "); waitField = new JFormattedTextField(); // waitField.setValue(240); waitField.setEditable(false); waitField.addPropertyChangeListener(new FormattedTextListener()); waitInt = new JLabel("Wait Interval (in minutes)"); // waitInt.setLabelFor(waitField); checkField = new JFormattedTextField(); checkField.setSize(1, 10); // checkField.setValue(60); checkField.setEditable(false); checkField.addPropertyChangeListener(new FormattedTextListener()); check = new JLabel("Check Interval (in minutes)"); // check.setLabelFor(checkField); fireButton = new JButton(fireString); fireButton.setActionCommand(fireString); fireButton.addActionListener(new FireListener()); JPanel fieldPane = new JPanel(); // fieldPane.add(destField); layout = new GroupLayout(fieldPane); fieldPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(waitInt) .addComponent(check)) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(waitField, 60, 60, 60) .addComponent(checkField, 60, 60, 60))); layout.setVerticalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(waitInt) .addComponent(waitField)) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(check) .addComponent(checkField))); JPanel labelPane = new JPanel(); labelPane.setLayout(new BorderLayout()); labelPane.add(destination, BorderLayout.PAGE_START); labelPane.add(fieldPane, BorderLayout.CENTER); layout = new GroupLayout(labelPane); labelPane.setLayout(layout); layout.setAutoCreateGaps(true); layout.setAutoCreateContainerGaps(true); layout.setHorizontalGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(destination) .addComponent(fieldPane))); layout.setVerticalGroup( layout.createSequentialGroup().addComponent(destination).addComponent(fieldPane)); // labelPane.add(destination); // labelPane.add(fieldPane); try { // Read from disk using FileInputStream FileInputStream f_in = new FileInputStream(LOG_DIRECTORY + "\\save.data"); // Read object using ObjectInputStream ObjectInputStream obj_in = new ObjectInputStream(f_in); // Read an object directoryList = (directoryStorage) obj_in.readObject(); ERROR_LOG_NAME = (String) obj_in.readObject(); MOVE_LOG_NAME = (String) obj_in.readObject(); if (ERROR_LOG_NAME instanceof String) { printer.changeErrorLogName(ERROR_LOG_NAME); } if (MOVE_LOG_NAME instanceof String) { printer.changeMoveLogName(MOVE_LOG_NAME); } if (directoryList instanceof directoryStorage) { System.out.println("found object"); // directoryList = (directoryStorage) obj; Iterator<Directory> directories = directoryList.getDirectories(); Directory d; while (directories.hasNext()) { d = directories.next(); try { listModel.addElement(d.getDirectory().toRealPath()); } catch (IOException x) { printer.printError(x.toString()); } int index = list.getSelectedIndex(); if (index == -1) { list.setSelectedIndex(0); } index = list.getSelectedIndex(); Directory dir = directoryList.getDirectory(index); destButton.setEnabled(true); checkField.setValue(dir.getInterval()); waitField.setValue(dir.getWaitInterval()); checkField.setEditable(true); waitField.setEditable(true); // directoryList.addNewDirectory(d); // try { // listModel.addElement(d.getDirectory().toString()); // } catch (IOException x) { // printer.printError(x.toString()); // } // timer = new Timer(); // timer.schedule(new CopyTask(d.directory, d.destination, d.getWaitInterval(), printer, // d.copy), 0, d.getInterval()); } } else { System.out.println("did not find object"); } obj_in.close(); } catch (ClassNotFoundException x) { printer.printError(x.getLocalizedMessage()); System.err.format("Unable to read"); } catch (IOException y) { printer.printError(y.getLocalizedMessage()); } // Layout the text fields in a panel. JPanel buttonPane = new JPanel(); buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS)); buttonPane.add(fireButton); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.add(new JSeparator(SwingConstants.VERTICAL)); buttonPane.add(Box.createHorizontalStrut(5)); buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5)); add(buttonPanel, BorderLayout.PAGE_START); add(listPane, BorderLayout.LINE_START); // add(destListScrollPane, BorderLayout.CENTER); add(fieldPane, BorderLayout.LINE_END); add(labelPane, BorderLayout.CENTER); add(buttonPane, BorderLayout.PAGE_END); }