Exemple #1
0
 public void windowClosing(WindowEvent e) {
   // System.out.println("whenUserClicksXOnTheCornerOfWindow was run");
   try {
     FileWriter outFile = new FileWriter("saveDataIDNumber.txt");
     PrintWriter out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write((Integer.toString(IDNumber[y])) + "\r\n");
     }
     out.close();
     outFile = new FileWriter("saveDatamovieName.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write(movieName[y] + "\r\n");
     }
     out.close();
     outFile = new FileWriter("saveDatamovieLength.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write(Integer.toString(movieLength[y]) + "\r\n");
     }
     out.close();
     outFile = new FileWriter("saveDatamovieDirector.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write(movieDirector[y] + "\r\n");
     }
     out.close();
     outFile = new FileWriter("saveDatamovieRating.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write(movieRating[y] + "\r\n");
     }
     out.close();
     outFile = new FileWriter("saveDatamovieReleaseYear.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write(Integer.toString(movieReleaseYear[y]) + "\r\n");
     }
     out.close();
     outFile = new FileWriter("saveDatamovieReviewRating.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     for (int y = 0; y < 100; y++) {
       out.write(Double.toString(movieReviewRating[y]) + "\r\n");
     }
     outFile.close(); // Stops the program from printing any more
     outFile = new FileWriter("IDNumberAndIndexNumber.txt");
     out = new PrintWriter(outFile); // Prepares the file to be written on
     out.write(Integer.toString(lastIndexNumber) + "\r\n");
     out.write(Integer.toString(lastIDNumber));
     outFile.close();
     System.out.println("data has been saved successfully");
   } catch (Exception error) {
     System.out.println("ERROR : " + error);
     System.out.println("The System could not succesfully save the data");
   }
   System.exit(0);
 }
    private static void createBrokenMarkerFile(@Nullable Throwable reason) {
      final File brokenMarker = getCorruptionMarkerFile();

      try {
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final PrintStream stream = new PrintStream(out);
        try {
          new Exception().printStackTrace(stream);
          if (reason != null) {
            stream.print("\nReason:\n");
            reason.printStackTrace(stream);
          }
        } finally {
          stream.close();
        }
        LOG.info("Creating VFS corruption marker; Trace=\n" + out.toString());

        final FileWriter writer = new FileWriter(brokenMarker);
        try {
          writer.write(
              "These files are corrupted and must be rebuilt from the scratch on next startup");
        } finally {
          writer.close();
        }
      } catch (IOException e) {
        // No luck.
      }
    }
Exemple #3
0
 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();
 }
 private void saveFile(String fileName) {
   try {
     FileWriter w = new FileWriter(fileName);
     area1.write(w);
     w.close();
     currentFile = fileName;
     changed = false;
     Save.setEnabled(false);
   } catch (IOException e) {
   }
 }
    public void actionPerformed(ActionEvent e) {
      System.out.println(backEnd);

      try {
        FileWriter f = new FileWriter(output);
        f.write(backEnd.toString());
        f.close();
      } catch (IOException a) {
        JOptionPane.showMessageDialog(null, "Error loading file to save to.");
      }
    }
Exemple #6
0
  /**
   * 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 boolean saveToFile(String namefile) {
   try {
     File file = new File(namefile);
     FileWriter out = new FileWriter(file);
     String text = textArea.getText();
     out.write(text);
     out.close();
     return true;
   } catch (IOException e) {
     System.out.println("Error saving file.");
   }
   return false;
 }
 private void saveFile(String fileName) {
   try {
     FileWriter w = new FileWriter(fileName);
     area.write(w);
     w.close();
     currentFile = fileName;
     setTitle(currentFile + " - CoreyTextEditor");
     changed = false;
     Save.setEnabled(false);
   } catch (IOException e) {
     // No handling done here
   }
 }
Exemple #9
0
  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.");
    }
  }
  // Write the output in Uintah format
  private void writeUintah(File outputFile) {

    // Create filewriter and printwriter
    try {
      FileWriter fw = new FileWriter(outputFile);
      PrintWriter pw = new PrintWriter(fw);

      uintahInputPanel.writeUintah(pw);

      pw.close();
      fw.close();

    } catch (Exception event) {
      System.out.println("Could not write to file " + outputFile.getName());
    }
  }
  // {{{ EditServer constructor
  EditServer(String portFile) {
    super("jEdit server daemon [" + portFile + "]");
    setDaemon(true);
    this.portFile = portFile;
    try {
      // On Unix, set permissions of port file to rw-------,
      // so that on broken Unices which give everyone read
      // access to user home dirs, people can't see your
      // port file (and hence send arbitriary BeanShell code
      // your way. Nasty.)
      if (OperatingSystem.isUnix()) {
        new File(portFile).createNewFile();
        FileVFS.setPermissions(portFile, 0600);
      }

      // Bind to any port on localhost; accept 2 simultaneous
      // connection attempts before rejecting connections
      socket = new ServerSocket(0, 2, InetAddress.getByName("127.0.0.1"));
      authKey = new Random().nextInt(Integer.MAX_VALUE);
      int port = socket.getLocalPort();

      FileWriter out = new FileWriter(portFile);

      try {
        out.write("b\n");
        out.write(String.valueOf(port));
        out.write("\n");
        out.write(String.valueOf(authKey));
        out.write("\n");
      } finally {
        out.close();
      }

      ok = true;

      Log.log(Log.DEBUG, this, "jEdit server started on port " + socket.getLocalPort());
      Log.log(Log.DEBUG, this, "Authorization key is " + authKey);
    } catch (IOException io) {
      /* on some Windows versions, connections to localhost
       * fail if the network is not running. To avoid
       * confusing newbies with weird error messages, log
       * errors that occur while starting the server
       * as NOTICE, not ERROR */
      Log.log(Log.NOTICE, this, io);
    }
  } // }}}
    // Query user for a filename and attempt to open and write the text
    // component’s content to the file.
    public void actionPerformed(ActionEvent ev) {
      JFileChooser chooser = new JFileChooser();
      if (chooser.showSaveDialog(SimpleEditor.this) != JFileChooser.APPROVE_OPTION) return;
      File file = chooser.getSelectedFile();
      if (file == null) return;

      FileWriter writer = null;
      try {
        writer = new FileWriter(file);
        textComp.write(writer);
      } catch (IOException ex) {
        JOptionPane.showMessageDialog(
            SimpleEditor.this, "File Not Saved", "ERROR", JOptionPane.ERROR_MESSAGE);
      } finally {
        if (writer != null) {
          try {
            writer.close();
          } catch (IOException x) {
          }
        }
      }
    }
Exemple #13
0
 /**
  * This function writes the generated relative overview to a file.
  *
  * @param f The file to write to.
  * @throws IOException Thrown if unable to open or write to the file.
  * @throws InsufficientDataException Thrown if unable to generate the overview.
  */
 public void writeToFile(File f) throws IOException, InsufficientDataException {
   f.createNewFile();
   FileWriter fw = new FileWriter(f);
   fw.write(generateOverviewText());
   fw.close();
 }
Exemple #14
0
  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() 结束
  public EditorPaneHTMLHelp(String htmlFile) {
    if (GlobalValues.useSystemBrowserForHelp) {
      Desktop d = GlobalValues.desktop;
      try {
        // create a temp file
        GlobalValues.forHTMLHelptempFile = new File("tempHTMLHelpSynthetic.html");
        FileWriter fw = new FileWriter(GlobalValues.forHTMLHelptempFile);

        java.util.List<String> list = readTextFromJar(htmlFile);
        Iterator<String> it = list.iterator();
        while (it.hasNext()) {
          fw.write(it.next() + "\n");
        }

        String canonicalPathOfFile = GlobalValues.forHTMLHelptempFile.getCanonicalPath();
        URL urlFile = new URL("file://" + canonicalPathOfFile);
        URI uriOfFile = urlFile.toURI();

        fw.close();

        d.browse(uriOfFile);

      } catch (Exception e) {
        e.printStackTrace();
      }

    } else {
      URL initialURL = getClass().getResource(htmlFile);

      font = GlobalValues.htmlfont;

      String title = "HTML Help";
      setTitle(title);

      final Stack<String> urlStack = new Stack<String>();
      final JEditorPane editorPane;

      editorPane = new JEditorPane(new HTMLEditorKit().getContentType(), " ");

      editorPane.setOpaque(false);
      editorPane.setBorder(null);
      editorPane.setEditable(false);

      JPanel magPanel = new JPanel();

      magnificationFactor = GlobalValues.helpMagnificationFactor;
      magFactor = new JTextField(Double.toString(magnificationFactor));

      JButton magButton = new JButton("Set Magnification: ");
      magButton.addActionListener(
          new ActionListener() {

            public void actionPerformed(ActionEvent e) {
              magnificationFactor = Double.parseDouble(magFactor.getText());
              GlobalValues.helpMagnificationFactor = magnificationFactor;
            }
          });

      magPanel.setLayout(new GridLayout(1, 2));
      magPanel.add(magButton);
      magPanel.add(magFactor);

      final JTextField url = new JTextField(initialURL.toString());

      // set up hyperlink listener
      editorPane.setEditable(false);

      try {
        // remember URL for back button
        urlStack.push(initialURL.toString());
        // show URL in text field
        url.setText(initialURL.toString());

        // add a CSS rule to force body tags to use the default label font
        // instead of the value in javax.swing.text.html.default.csss

        String bodyRule =
            "body { font-family: "
                + font.getFamily()
                + "; "
                + "font-size: "
                + font.getSize() * GlobalValues.helpMagnificationFactor
                + "pt; }";
        ((HTMLDocument) editorPane.getDocument()).getStyleSheet().addRule(bodyRule);

        editorPane.setPage(initialURL);

        editorPane.firePropertyChange("dummyProp", true, false);

      } catch (IOException e) {
        editorPane.setText("Exception: " + e);
      }

      editorPane.addPropertyChangeListener(
          new PropertyChangeListener() {

            public void propertyChange(PropertyChangeEvent evt) {

              String bodyRule =
                  "body { font-family: "
                      + font.getFamily()
                      + "; "
                      + "font-size: "
                      + font.getSize() * GlobalValues.helpMagnificationFactor
                      + "pt; }";
              ((HTMLDocument) editorPane.getDocument()).getStyleSheet().addRule(bodyRule);
            }
          });

      editorPane.addHyperlinkListener(
          new HyperlinkListener() {
            public void hyperlinkUpdate(HyperlinkEvent event) {
              if (event.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
                try {
                  // remember URL for back button
                  urlStack.push(event.getURL().toString());
                  // show URL in text field
                  url.setText(event.getURL().toString());
                  editorPane.setPage(event.getURL());

                  editorPane.firePropertyChange("dummyProp", true, false);

                } catch (IOException e) {
                  editorPane.setText("Exception: " + e);
                }
              }
            }
          });

      // set up checkbox for toggling edit mode
      final JCheckBox editable = new JCheckBox();
      editable.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              editorPane.setEditable(editable.isSelected());
            }
          });

      // set up load button for loading URL
      ActionListener listener =
          new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              try {
                // remember URL for back button
                urlStack.push(url.getText());
                editorPane.setPage(url.getText());

                editorPane.firePropertyChange("dummyProp", true, false);

              } catch (IOException e) {
                editorPane.setText("Exception: " + e);
              }
            }
          };

      JButton loadButton = new JButton("Load/Magnify");
      loadButton.addActionListener(listener);
      url.addActionListener(listener);

      // set up back button and button action

      JButton backButton = new JButton("Back");
      backButton.addActionListener(
          new ActionListener() {
            public void actionPerformed(ActionEvent event) {
              if (urlStack.size() <= 1) return;
              try {
                // get URL from back button
                urlStack.pop();
                // show URL in text field
                String urlString = urlStack.peek();
                url.setText(urlString);
                editorPane.setPage(urlString);

                editorPane.firePropertyChange("dummyProp", true, false);

              } catch (IOException e) {
                editorPane.setText("Exception: " + e);
              }
            }
          });

      JPanel allPanel = new JPanel(new BorderLayout());

      // put all control components in a panel

      JPanel ctrlPanel = new JPanel(new BorderLayout());
      JPanel urlPanel = new JPanel();
      urlPanel.add(new JLabel("URL"));
      urlPanel.add(url);
      JPanel buttonPanel = new JPanel();
      buttonPanel.add(loadButton);
      buttonPanel.add(backButton);
      buttonPanel.add(new JLabel("Editable"));
      buttonPanel.add(magPanel);
      buttonPanel.add(editable);
      ctrlPanel.add(buttonPanel, BorderLayout.NORTH);
      ctrlPanel.add(urlPanel, BorderLayout.CENTER);

      allPanel.add(ctrlPanel, BorderLayout.NORTH);
      allPanel.add(new JScrollPane(editorPane), BorderLayout.CENTER);

      add(allPanel);
    }
  }