private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) { // 监听事件 // TODO add your handling code here: if (jRadioButton1.isSelected()) { jRadioButtonName = jRadioButton1.getText(); } else if (jRadioButton2.isSelected()) { jRadioButtonName = jRadioButton2.getText(); } try { fwriter = new FileWriter(filename); fwriter.write(jTextField1.getText()); fwriter.write("\r\n"); fwriter.write(jTextField2.getText()); fwriter.write("\r\n"); fwriter.write(jTextField3.getText()); fwriter.write("\r\n"); fwriter.write(jTextField4.getText()); fwriter.write("\r\n"); fwriter.write(jRadioButtonName); fwriter.write("\r\n"); } catch (IOException e) { e.printStackTrace(); } finally { try { fwriter.flush(); fwriter.close(); } catch (IOException e) { e.printStackTrace(); } } this.dispose(); }
/** * Saves an OLS data file to the given file. * * @param aFile the file to save the OLS data to, cannot be <code>null</code>. * @throws IOException in case of I/O problems. */ public void saveDataFile(final File aFile) throws IOException { final FileWriter writer = new FileWriter(aFile); try { final Project tempProject = this.projectManager.createTemporaryProject(); tempProject.setCapturedData(this.dataContainer); OlsDataHelper.write(tempProject, writer); } finally { writer.flush(); writer.close(); } }
public static void stringToFile(String file, String string) { try { if (file.lastIndexOf('\\') != -1) { File par = new File(file.substring(0, file.lastIndexOf('\\'))); par.mkdirs(); } FileWriter fw = new FileWriter(file); fw.write(string); fw.flush(); fw.close(); } catch (IOException e) { System.out.println("Failed to save file."); } }
/** * This method is called whenever a status message is received from the Cryogen Monitor. * * @param iWhat An integer coding what the value is for (status=152). * @param iValue Is true. */ protected void processStatus(String iWhat, boolean iValue) { StringTokenizer toker, dateToker; String elem, elem2; int errors = 0; String time = " "; String heLevel = "000"; String n2Level = "000"; // System.out.println("received:"+iWhat); iWhat = iWhat.replace("Connected to E5025", ""); if (!iValue) { if (m_cryoPanel != null) { m_cryoPanel.cryomonNotCommunicating(); } return; } // String key = new String(iWhat); if (iWhat == null) return; if (m_cryoPanel != null) m_cryoPanel.writeAdvanced("Received: " + iWhat + "\n"); toker = new StringTokenizer(iWhat, ","); if (iWhat.endsWith("Z*")) { // m_statusPoller.clrDataCount(); } else if (iWhat.endsWith("NH*")) { // m_statusPoller.clrDataCount(); } else if (iWhat.endsWith("EH*")) { if (m_cryoPanel != null) { // String msg=iWhat.replace("Connected", ""); m_cryoPanel.setErrors(iWhat); } } else if (iWhat.endsWith(" t*")) { // String s=iWhat.replace("Connected",""); Date date = getDate(iWhat); last_date = date; if (m_cryoPanel != null) m_cryoPanel.setTime(iWhat); } else if (iWhat.startsWith("V")) { if (m_cryoPanel != null) { String msg = iWhat.replace("*", ""); // System.out.println("CryoSocketControl version: " + msg); DataFileManager.setFirmwareVersion(msg); } } else if (iWhat.startsWith("Date")) { m_cryoPanel.writeAdvanced("Reading Log: " + iWhat + "\n"); // open file & read rest of lines into file String logFile = m_cryoPanel.getLogFile(); try { m_write2Log = true; pause(true); // System.out.println("Opening log file " + logFile); FileWriter outLog = new FileWriter(logFile, false); // open file and do not append data // System.out.println("Writing log file " + logFile); outLog.write(iWhat + "\n"); outLog.flush(); } catch (Exception e) { // System.out.println("Could not write log file."); } } else if (iWhat.endsWith("W*")) { if (m_cryoPanel != null) { errors = 0; heLevel = "000"; n2Level = "000"; while (toker.hasMoreTokens()) { elem = toker.nextToken(); if (elem.startsWith("E5") || elem.startsWith("E6")) { if (m_cryoPanel != null) DataFileManager.setMonitorType(elem); } else if (elem.startsWith("ND")) { elem = elem.substring(2, 5); m_cryoPanel.writeAdvanced("N2 level: " + elem + "\n"); n2Level = elem; } else if (elem.startsWith("HL")) { elem = elem.substring(2, 6); try { int HL = Integer.parseInt(elem); DataFileManager.HL = HL; m_cryoPanel.writeAdvanced("HL level: " + HL + "\n"); } catch (Exception e) { System.out.println("Input format error in HL"); } } else if (elem.startsWith("SA")) { errors = Integer.parseInt(elem.substring(2)); } else if (elem.startsWith("SB")) { int sb = Integer.parseInt(elem.substring(2)); errors += sb << 8; // m_cryoPanel.setStatus(errors); } else if (elem.startsWith("P1D")) { elem2 = toker.nextToken(); // get P2D for second probe if (elem.substring(8, 12).startsWith("+")) { elem = elem.substring(9, 12); } else { elem = elem.substring(8, 12); } if (elem2.substring(8, 12).startsWith("+")) { elem2 = elem2.substring(9, 12); } else { elem2 = elem2.substring(8, 12); } m_cryoPanel.writeAdvanced("He levelP1: " + elem + " levelP2: " + elem2 + "\n"); if (elem.startsWith("#") && elem2.startsWith("#")) // no probes enabled heLevel = "000"; else if (elem.startsWith("#")) heLevel = elem2; else if (elem2.startsWith("#")) heLevel = elem; else { // both probes enabled heLevel = elem; try { int v1 = Integer.parseInt(elem); int v2 = Integer.parseInt(elem2); int max_val = v1 > v2 ? v1 : v2; DecimalFormat myFormatter = new DecimalFormat("000"); String output = myFormatter.format(max_val); // System.out.println("He1:"+elem+" He2:"+elem2+" ave:"+output); heLevel = output; } catch (Exception e) { System.out.println("Input format error in He level"); } } } else if (elem.startsWith("t")) { time = elem.replace("t", ""); dateToker = new StringTokenizer(time, " "); if (dateToker.hasMoreTokens()) { prev_read = last_read; last_read = getDate(time); } } } if (last_date != null) { writeDataToFile(last_date, heLevel, n2Level, errors); } } } else if (m_write2Log) { if (iWhat.startsWith("*")) { // close file m_cryoPanel.popupMsg("Log File", "Cryogen Monitor log written", false); m_cryoPanel.writeAdvanced("Reading Log Done.\n"); m_write2Log = false; pause(false); m_cryoPanel.setReadLogFlag(false); } else { String logFile = m_cryoPanel.getLogFile(); try { FileWriter outLog = new FileWriter(logFile, true); // open file and append data outLog.write(iWhat + "\n"); outLog.flush(); } catch (Exception e) { // System.out.println("Could not write log file."); } } } }
private void initComponents() { // 初始化构件 jLabel1 = new JLabel(); jLabel2 = new JLabel(); jLabel3 = new JLabel(); jLabel4 = new JLabel(); jLabel5 = new JLabel(); jTextField1 = new JTextField(null); jTextField2 = new JTextField(null); jTextField3 = new JTextField(null); jTextField4 = new JTextField(null); jRadioButton1 = new JRadioButton("男"); jRadioButton2 = new JRadioButton("女"); jRadioButtonName = new String(); jButton1 = new JButton(); ButtonGroup buttonGroup = new ButtonGroup(); // 单选按钮组 buttonGroup.add(jRadioButton1); buttonGroup.add(jRadioButton2); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); setResizable(false); // 不可更改大小 jLabel1.setText("心电仪IP地址"); jLabel2.setText("端口号"); jLabel3.setText("档案ID"); jLabel4.setText("姓名"); jLabel5.setText("性别"); jButton1.setText("确定"); this.addWindowListener( new WindowAdapter() { @Override public void windowClosing(WindowEvent e) { jLabel1.setText(null); jLabel2.setText(null); jLabel3.setText(null); jLabel4.setText(null); jLabel5.setText(null); jRadioButtonName = null; System.out.println("windowClosing"); } }); if (!file.exists()) { try { fwriter = new FileWriter(filename); fwriter.write(jTextField1.getText()); fwriter.write("\r\n"); fwriter.write(jTextField2.getText()); fwriter.write("\r\n"); fwriter.write(jTextField3.getText()); fwriter.write("\r\n"); fwriter.write(jTextField4.getText()); fwriter.write("\r\n"); fwriter.write(jRadioButtonName); fwriter.write("\r\n"); } catch (IOException e) { e.printStackTrace(); } finally { try { fwriter.flush(); fwriter.close(); } catch (IOException e) { e.printStackTrace(); } } } else { File file = new File(filename); String[] fileContent = new String[5]; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1; // 一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { System.out.println("line" + line + ": " + tempString); // content=tempString; fileContent[line - 1] = tempString; line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } jTextField1.setText(fileContent[0]); jTextField2.setText(fileContent[1]); jTextField3.setText(fileContent[2]); jTextField4.setText(fileContent[3]); jRadioButtonName = fileContent[4]; if (jRadioButtonName.equals((String) "男")) { jRadioButton1.setSelected(true); } else if (jRadioButtonName.equals((String) "女")) { jRadioButton2.setSelected(true); } } jButton1.addActionListener( new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { jButton1ActionPerformed(evt); // jButton1ActionPerformed这个方法在后面定义 } }); // 按钮的监听事件 GroupLayout layout = new GroupLayout(getContentPane()); // GroupLayout布局 getContentPane().setLayout(layout); layout.setHorizontalGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addGap(40, 40, 40) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(jButton1) .addGroup( layout .createSequentialGroup() .addGroup( layout .createParallelGroup(GroupLayout.Alignment.TRAILING) .addComponent(jLabel5) .addComponent(jLabel4) .addComponent(jLabel3) .addComponent(jLabel2) .addComponent(jLabel1)) .addGap(40, 40, 40) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addComponent(jRadioButton1) .addComponent(jRadioButton2)) .addComponent( jTextField4, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE) .addComponent( jTextField3, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE) .addComponent( jTextField2, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE) .addComponent( jTextField1, GroupLayout.PREFERRED_SIZE, 160, GroupLayout.PREFERRED_SIZE)))) .addContainerGap(85, Short.MAX_VALUE))); layout.setVerticalGroup( layout .createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup( layout .createSequentialGroup() .addGap(20, 20, 20) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel1) .addComponent( jTextField1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel2) .addComponent( jTextField2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel3) .addComponent( jTextField3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel4) .addComponent( jTextField4, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) .addGap(20, 20, 20) .addGroup( layout .createParallelGroup(GroupLayout.Alignment.BASELINE) .addComponent(jLabel5) .addGroup( layout .createParallelGroup() .addComponent(jRadioButton1) .addComponent(jRadioButton2))) .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED) .addComponent(jButton1) .addContainerGap(23, Short.MAX_VALUE))); pack(); // 调整此窗口的大小,以适合其子组件的首选大小和布局 setLocationRelativeTo(null); // 窗口居中显示 } // 初始化构件initComponents() 结束