private void saveCurrent() {
    PrintWriter writer = null;

    try {
      writer = new PrintWriter("config.txt", "UTF-8");
      writer.println("PhoneNumbers:");

      for (String s : Main.getEmails()) {
        writer.println(s);
      }

      writer.println("Items:");

      for (Item s : Main.getItems()) {
        writer.println(s.getName() + "," + s.getWebsite());
      }

      results.setText("Current settings have been saved sucessfully.");
    } catch (FileNotFoundException e1) {
      e1.printStackTrace();
    } catch (UnsupportedEncodingException e1) {
      e1.printStackTrace();
    }

    writer.close();
  }
Beispiel #2
0
  private void onOK() {
    // add your code here
    try {
      PrintWriter pw = new PrintWriter("data\\out.txt");
      pw.println(textField1.getText());
      pw.close();

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }
  }
Beispiel #3
0
  private Scanner scanFile() {

    Scanner s = null;

    try {
      s = new Scanner(new BufferedReader(new FileReader("./lab3/Liv.xml")));
      s.nextLine();
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    return s;
  }
Beispiel #4
0
  private OutputStream createFile(String fileName) {

    OutputStream outputStream = null;

    File outputFile = new File(fileName);

    try {
      outputStream = new FileOutputStream(outputFile);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    return outputStream;
  }
Beispiel #5
0
  public static void main(final String[] args) {

    File logdir = new File(LOG_DIR);
    if (!logdir.exists()) logdir.mkdirs();
    File log = new File(logdir, "client.log");
    // redirect all console output to the file
    try {
      PrintStream out = new PrintStream(new FileOutputStream(log, true), true);
      out.format("[%s] ===== Client started =====%n", timestampf.format(new Date()));
      System.setOut(out);
      System.setErr(out);
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    }

    /* Set up the error handler as early as humanly possible. */
    ThreadGroup g = new ThreadGroup("Haven main group");
    String ed;
    if (!(ed = Utils.getprop("haven.errorurl", "")).equals("")) {
      try {
        final haven.error.ErrorHandler hg = new haven.error.ErrorHandler(new java.net.URL(ed));
        hg.sethandler(
            new haven.error.ErrorGui(null) {
              public void errorsent() {
                hg.interrupt();
              }
            });
        g = hg;
      } catch (java.net.MalformedURLException e) {
      }
    }
    Thread main =
        new HackThread(
            g,
            new Runnable() {
              public void run() {
                main2(args);
              }
            },
            "Haven main thread");
    main.start();
  }
Beispiel #6
0
  public static void music(char gameResult) {
    AudioPlayer MGP = AudioPlayer.player;
    AudioStream BGM, BGM1;
    AudioData MD;

    ContinuousAudioDataStream loop = null;

    try {
      InputStream winAudio = new FileInputStream("Audio/winrevised.wav");
      InputStream loseAudio = new FileInputStream("Audio/loserevised.wav");
      BGM = new AudioStream(winAudio);
      BGM1 = new AudioStream(loseAudio);
      if (gameResult == 'w') AudioPlayer.player.start(BGM);
      else if (gameResult == 'l') AudioPlayer.player.start(BGM1);
      // MD = BGM.getData();
      // loop = new ContinuousAudioDataStream(MD);

    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (IOException error) {
      error.printStackTrace();
    }
    MGP.start(loop);
  }
  private void savePhone() {

    if (phone.getText().length() == 10) {
      File f = new File("config.txt");
      Scanner sc;

      ArrayList<String> config = new ArrayList<String>();

      try {
        sc = new Scanner(f);

        while (sc.hasNext()) {
          String s = sc.nextLine();
          config.add(s);
        }
        sc.close();

      } catch (FileNotFoundException e2) {
        results.setText("Error reading config.txt");
      }

      int i = 0;

      for (String s : config) {

        if (s.equals("PhoneNumbers:")) {
          break;
        }
        i++;
      }

      if (carriers.getSelectedIndex() == 0) {
        config.add(i + 1, phone.getText() + "@txt.att.net");
      }
      if (carriers.getSelectedIndex() == 1) {
        config.add(i + 1, phone.getText() + "@myboostmobile.com");
      }
      if (carriers.getSelectedIndex() == 2) {
        config.add(i + 1, phone.getText() + "@mobile.celloneusa.com");
      }
      if (carriers.getSelectedIndex() == 3) {
        config.add(i + 1, phone.getText() + "@messaging.nextel.com");
      }
      if (carriers.getSelectedIndex() == 4) {
        config.add(i + 1, phone.getText() + "@tmomail.net");
      }
      if (carriers.getSelectedIndex() == 5) {
        config.add(i + 1, phone.getText() + "@txt.att.net");
      }
      if (carriers.getSelectedIndex() == 6) {
        config.add(i + 1, phone.getText() + "@email.uscc.net");
      }
      if (carriers.getSelectedIndex() == 7) {
        config.add(i + 1, phone.getText() + "@messaging.sprintpcs.com");
      }
      if (carriers.getSelectedIndex() == 8) {
        config.add(i + 1, phone.getText() + "@vtext.com");
      }
      if (carriers.getSelectedIndex() == 9) {
        config.add(i + 1, phone.getText() + "@vmobl.com");
      }

      PrintWriter writer = null;
      try {
        writer = new PrintWriter("config.txt", "UTF-8");
        for (String s : config) {
          writer.println(s);
        }

      } catch (FileNotFoundException e1) {
        e1.printStackTrace();
      } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
      }
      writer.close();
      addPhone();
    } else {
      results.setText("Please add 10 digit cell number.");
    }
  }
Beispiel #8
0
  @Override
  public void actionPerformed(ActionEvent action) {
    // Get the file the user wants to use and store it.
    if (action.getSource() == chooseFile) {
      // *sigh* I spent like 10 minutes trying to figure out why my if statement was not working
      // and then I found a semicolon on the end...noob mistake...
      // I had written a paragraph about it to send you too.
      if (fileChooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) {
        try {
          System.out.println(
              "File: "
                  + fileChooser.getSelectedFile()
                  + "\nInt: "
                  + fileChooser.showOpenDialog(this));
          file = fileChooser.getSelectedFile();
          inputFile = new Scanner(file);
          if (JOptionPane.showConfirmDialog(
                  this,
                  "Overwrite orginal file?",
                  "Overwrite Confimration",
                  JOptionPane.YES_NO_OPTION)
              == JOptionPane.YES_OPTION) {
            outputFile = new PrintWriter(file);
          } else {
            fileChooser.showOpenDialog(this);
            outputFile = new PrintWriter(fileChooser.getSelectedFile());
          }
          // Enable the buttons now that a file has been chosen.
          sort.setEnabled(true);
          next.setEnabled(true);
        } catch (FileNotFoundException exception) {
          JOptionPane.showMessageDialog(this, "Error: Could not find or open file.");
          exception.printStackTrace();
        }
        // Just in case something weird happens and all those files don't get linked correctly,
        // this will get called and prevent the buttons from being enabled.  Essentially, the user
        // won't see anything.
        catch (NullPointerException exception) {
          sort.setEnabled(false);
          next.setEnabled(false);
          exception.printStackTrace();
        }
      }
    } else if (action.getSource() == sort) {
      // Call the quicksort method to sort, and handle the file writing.
      while (inputFile.hasNextLine()) {
        arrayList.add(inputFile.nextLine());
      }
      inputFile.close();
      temp = new String[arrayList.size()];
      array = arrayList.toArray(temp);
      sorter.quicksort(array, 0, array.length - 1);

      for (int i = 0; i < array.length; i++) {
        outputFile.println(array[i]);
      }
      outputFile.close();
      JOptionPane.showMessageDialog(this, "List successfully sorted.");
    } else if (action.getSource() == next) {
      // Go to the search options.
      buttonPanel.remove(chooseFile);
      buttonPanel.remove(sort);
      buttonPanel.add(search);
      repaint();
      setVisible(true);
    } else if (action.getSource() == exit) {
      // Exit, obviously.
      dispose();
    } else if (action.getSource() == search) {
      // Call the binarySearch method and display the result.
      String searchValue = JOptionPane.showInputDialog("Enter the value to search for:");
      boolean ignoreCase;
      int result;
      if (JOptionPane.showConfirmDialog(
              this,
              "Case-sensitive search?",
              "Case-sensitive",
              JOptionPane.YES_NO_OPTION,
              JOptionPane.QUESTION_MESSAGE)
          == JOptionPane.YES_OPTION) {
        ignoreCase = false;
      } else {
        ignoreCase = true;
      }
      result = searcher.binarySearch(array, searchValue, ignoreCase) + 1;
      if (result == 0) {
        text.setText("The value, " + searchValue + ", was not found.");
      } else {
        text.setText("The value, " + searchValue + ", was found on line: " + result + ".");
      }
    }
  }
  private void doSave() {
    myFile = doRead();
    if (myFile == null) {
      return;
    }

    String name = myFile.getName();
    showMessage("compressing " + name);
    String newName =
        JOptionPane.showInputDialog(this, "Name of compressed file", name + HUFF_SUFFIX);
    if (newName == null) {
      return;
    }
    String path = null;
    try {
      path = myFile.getCanonicalPath();
    } catch (IOException e) {
      showError("trouble with file canonicalizing");
      return;
    }
    int pos = path.lastIndexOf(name);
    newName = path.substring(0, pos) + newName;
    final File file = new File(newName);
    try {
      final FileOutputStream out = new FileOutputStream(file);
      ProgressMonitorInputStream temp = null;
      if (myFast) {
        temp = getMonitorableStream(getFastByteReader(myFile), "compressing bits...");
      } else {
        temp = getMonitorableStream(myFile, "compressing bits ...");
      }
      final ProgressMonitorInputStream pmis = temp;
      final ProgressMonitor progress = pmis.getProgressMonitor();
      Thread fileWriterThread =
          new Thread() {
            public void run() {
              try {
                while (!myFirstReadingDone) {
                  try {
                    sleep(100);
                  } catch (InterruptedException e) {
                    // what to do?
                    HuffViewer.this.showError("Trouble in Thread " + e);
                  }
                }
                myModel.compress(pmis, out, myForce);
              } catch (IOException e) {
                HuffViewer.this.showError("compression exception\n " + e);
                cleanUp(file);
                // e.printStackTrace();
              }
              if (progress.isCanceled()) {
                HuffViewer.this.showError("compression cancelled");
                cleanUp(file);
              }
            }
          };
      fileWriterThread.start();
    } catch (FileNotFoundException e) {
      showError("could not open " + file.getName());
      e.printStackTrace();
    }
    myFile = null;
  }
  private void doDecode() {
    File file = null;
    showMessage("uncompressing");
    try {
      int retval = ourChooser.showOpenDialog(null);
      if (retval != JFileChooser.APPROVE_OPTION) {
        return;
      }
      file = ourChooser.getSelectedFile();
      String name = file.getName();
      String uname = name;
      if (name.endsWith(HUFF_SUFFIX)) {
        uname = name.substring(0, name.length() - HUFF_SUFFIX.length()) + UNHUFF_SUFFIX;
      } else {
        uname = name + UNHUFF_SUFFIX;
      }
      String newName = JOptionPane.showInputDialog(this, "Name of uncompressed file", uname);
      if (newName == null) {
        return;
      }
      String path = file.getCanonicalPath();

      int pos = path.lastIndexOf(name);
      newName = path.substring(0, pos) + newName;
      final File newFile = new File(newName);
      ProgressMonitorInputStream temp = null;
      if (myFast) {
        temp = getMonitorableStream(getFastByteReader(file), "uncompressing bits ...");
      } else {
        temp = getMonitorableStream(file, "uncompressing bits...");
      }
      final ProgressMonitorInputStream stream = temp;

      final ProgressMonitor progress = stream.getProgressMonitor();
      final OutputStream out = new FileOutputStream(newFile);
      Thread fileReaderThread =
          new Thread() {
            public void run() {
              try {
                myModel.uncompress(stream, out);
              } catch (IOException e) {

                cleanUp(newFile);
                HuffViewer.this.showError("could not uncompress\n " + e);
                // e.printStackTrace();
              }
              if (progress.isCanceled()) {
                cleanUp(newFile);
                HuffViewer.this.showError("reading cancelled");
              }
            }
          };
      fileReaderThread.start();
    } catch (FileNotFoundException e) {
      showError("could not open " + file.getName());
      e.printStackTrace();
    } catch (IOException e) {
      showError("IOException, uncompression halted from viewer");
      e.printStackTrace();
    }
  }