public List<Supplier> GetSuppliers() {
    FileInputStream fis = null;
    ObjectInputStream ois = null;
    List<Supplier> suppliers = new ArrayList<Supplier>();

    try {
      fis = new FileInputStream("Suppliers.dat");
      ois = new ObjectInputStream(fis);

      while (true) {
        try {
          suppliers.add((Supplier) ois.readObject());
        } catch (EOFException ex) {
          break;
        }
      }
    } catch (IOException ex) {
      System.out.println("Falha ao Ler arquivo de fornecedores!");
      ex.getStackTrace();
    } catch (ClassNotFoundException e) {
      System.out.println("Falha ao Ler arquivo de fornecedores!");
      e.printStackTrace();
    } finally {
      try {
        if (ois != null) ois.close();
        if (fis != null) fis.close();
      } catch (IOException ex) {
        System.out.println("Falha ao Ler arquivo de fornecedores!");
        ex.getStackTrace();
      }
    }

    return suppliers;
  }
  public void InsertSupplier(Supplier supplier) {
    FileOutputStream fos = null;

    try {
      File file = new File("Suppliers.dat");

      if (file.exists()) {
        fos = new FileOutputStream(file, true);
        _aoos = new AppendableObjectOutputStream(fos);
        _aoos.writeObject(supplier);
        System.out.println("Fornecedor cadastrado com sucesso!");
      } else {
        fos = new FileOutputStream(file);
        _oos = new ObjectOutputStream(fos);
        _oos.writeObject(supplier);
        System.out.println("Fornecedor cadastrado com sucesso!");
      }

    } catch (IOException ex) {
      System.out.println("Falha ao cadastrar fornecedor!");
      ex.getStackTrace();
    } finally {
      try {
        if (_aoos != null) _aoos.close();
        if (_oos != null) _oos.close();
        if (fos != null) fos.close();
      } catch (IOException ex) {
        System.out.println("Falha ao cadastrar fornecedor!");
        ex.getStackTrace();
      }
    }
  }
 private Throwable createIoException() {
   Throwable exception = null;
   try {
     new FileInputStream(new File(NON_EXISTING_FILE));
     fail("Managed to find a file that shouldn't exist " + NON_EXISTING_FILE);
   } catch (IOException ioe) {
     if (ioe.getStackTrace() == null || ioe.getStackTrace().length == 0) {
       ioe.fillInStackTrace();
     }
     exception = ioe;
   }
   return exception;
 }
Esempio n. 4
0
  public static void main(String[] argv) {

    try {
      FileWriter f = new FileWriter("userlevels.txt");
      f.write("&userdata= \n\n");

      for (int i = 0; i <= 100; i++) {
        System.out.print(i + ". ");
        NMap map =
            new NMap(
                "11100011111111111000001110000011110000000000001000000000000000000000000001000000000100000001000000000000011100000111000000011000111000011111000000110000010000111111000000000000000000111111000000000000000011111111000010000000000111111111000111000000011111111111001111100001111111011110011111100011111100011000111111100011111000000001111111100011111000000111111111100011110000001111111111100011100000011111111111100110000000011111111111111100000000000001111100111000000000000001110000110001000000100000000001100111100001000000000010001111100000000000000100011111100000011100001000011111111111111000011000011111111111111100111100111111001111111100111100111000000111110001111100000000000000000011111100011110000000001");
        // NMap map = new NMap();
        map.checkAndFix();

        String output = map.toString();

        if (i < 10) {
          f.write("$00" + i + "#rennaT#none#" + output + "\n");
        } else if (i < 100) {
          f.write("$0" + i + "#rennaT#none#" + output + "\n");
        } else {
          f.write("$" + i + "#rennaT#none#" + output + "\n");
        }
      }
      f.close();
    } catch (IOException e) {
      System.err.println(e.getStackTrace());
    }
  }
  public static void writeToEOF(String s, File f) {

    try {
      BufferedWriter bw = new BufferedWriter(new FileWriter(f, true));
      bw.write(s);
      bw.close();

    } catch (IOException e) {
      System.out.println("Problems writing to file!" + e.getStackTrace().toString());
    }
  }
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    ByteArrayOutputStream baos = new ByteArrayOutputStream(); // 新建一个字节数组
    DataOutputStream dos = new DataOutputStream(baos); // 套结上一个数据流输出--可以直接输出基本类型到文本里

    try {
      dos.writeDouble(Math.random());
      dos.writeBoolean(true);

      ByteArrayInputStream bais =
          new ByteArrayInputStream(baos.toByteArray()); // 新建一个字节数组输入流--必须要是 byte[]
      System.out.println(bais.available()); // 输出能有多少个字符能被输出
      DataInputStream dis = new DataInputStream(bais);

      System.out.println(dis.readDouble());
      System.out.println(dis.readBoolean());

      dos.close(); // 关闭最外面的流就可以把全部都关闭了
      dis.close();
    } catch (IOException e) {
      e.getStackTrace();
      System.exit(-1);
    }
  }
Esempio n. 7
0
  public boolean parserCpYieldTxt(File txtFile) throws Exception {
    FileInputStream fIn = null;
    String fileNameUid = "";
    try {
      Calendar calendar = Calendar.getInstance();
      SimpleDateFormat df2 = new SimpleDateFormat("yyyyMM");

      // Using Find Target "|=124" or ",=44" Count
      DateFormat df = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);

      logger.debug(
          "File "
              + txtFile.getAbsolutePath()
              + "  "
              + txtFile.getName()
              + "  "
              + new MimetypesFileTypeMap().getContentType(txtFile));

      // 1.0 讀入檔案, 建立資料流
      fIn = new FileInputStream(txtFile);
      // FileInputStream fIn2 = new FileInputStream(csvFile);
      InputStreamReader isr = null;
      BufferedReader br = null;
      isr = new InputStreamReader(fIn, "UTF-8");
      br = new BufferedReader(isr);

      // byte[] byteArray = new byte[new Long(csvFile.length()).intValue()];
      // 讀入File Data Byte.....
      // fIn2.read(byteArray);
      logger.debug(txtFile.getName() + "<--讀入資料檔...成功");
      // Using get sign  "\n" 抓行數...

      // 1.1 讀入行數資料, 略過行數
      int l = -1;
      List<String> lineDataList = new ArrayList<String>();
      for (int i = 0; i <= l; i++) {
        br.readLine();
      }
      while (br.ready()) {
        String line = br.readLine();
        line = null != line ? line : "";
        // logger.debug(line);
        if (!line.trim().equals("")) {
          lineDataList.add(line);
        }
      }

      // 1.2 確認有資料開使處理
      if (lineDataList != null) {
        CpYieldParserDao cpYieldParserDao = new CpYieldParserDao();

        CpYieldLotTo cpYieldLotTo = new CpYieldLotTo();

        String cpYieldUuid = UUID.randomUUID().toString().toUpperCase();
        logger.debug("lineDataList.size() " + lineDataList.size());

        fileNameUid =
            FilenameUtils.getBaseName(txtFile.getName())
                + "_"
                + cpYieldUuid
                + "."
                + FilenameUtils.getExtension(txtFile.getName());

        File bkFolder =
            new File(fileOutUrl + File.separator + df2.format(calendar.getTime()).toString());

        bkFolder.mkdir();

        File bkFile = new File(bkFolder.getPath().toString() + File.separator + fileNameUid);
        // 1.2.1 處理每行資料
        String tmpWaferID = lineDataList.get(1);
        String arrayWafer[] = tmpWaferID.split("=")[1].trim().split("-");

        // logger.debug("arrayWafer[] " + arrayWafer.length);

        // 1.3 Prepare Data
        String cpLot = arrayWafer[0].trim();
        String waferId = arrayWafer[1].trim();
        String machineId = arrayWafer[2].trim();
        Integer cpTestTimes = cpYieldParserDao.getMaxCpTestTimes(cpLot, waferId);

        String xMaxCoor = lineDataList.get(2).split("=")[1].trim();
        String yMaxCoor = lineDataList.get(3).split("=")[1].trim();
        String flat = lineDataList.get(4).split("=")[1].trim();

        logger.debug("xMaxCoor " + xMaxCoor);
        logger.debug("yMaxCoor " + yMaxCoor);
        logger.debug("flat " + flat);

        // 1.3 Find Bin Data
        int sb = 0, eb = 0;
        for (int i = 0; i < lineDataList.size(); i++) {
          if (lineDataList.get(i).indexOf("Wafer Bin Summary") >= 0) {
            sb = i + 1;
            break;
          }
        }
        for (int i = sb; i < lineDataList.size(); i++) {
          if (lineDataList.get(i).indexOf("bin") < 0) {
            eb = i - 1;
            break;
          }
        }

        logger.debug("sb " + sb);
        logger.debug(lineDataList.get(sb).trim());
        logger.debug("eb " + eb);
        logger.debug(lineDataList.get(eb).trim());
        // 1.3.1 Get Bin Data
        List<CpYieldLotBinTo> cpYieldLotBins = new ArrayList<CpYieldLotBinTo>();
        String cpYieldBinUuid;

        String bin;
        Integer die;
        String percentage;
        String binString;

        for (int j = sb; j <= eb; j++) {
          cpYieldBinUuid = UUID.randomUUID().toString().toUpperCase();
          CpYieldLotBinTo cpYieldLotBinTo = new CpYieldLotBinTo();

          cpYieldLotBinTo.setCpYieldBinUuid(cpYieldBinUuid);
          cpYieldLotBinTo.setCpYieldUuid(cpYieldUuid);

          binString = lineDataList.get(j).trim();
          binString = binString.replaceAll("bin", "").trim();
          // Get Bin
          bin = binString.substring(0, binString.indexOf(" "));
          logger.debug("bin " + bin);
          // Get Die
          bin = binString.substring(0, binString.indexOf(" "));
          binString = binString.replaceAll(bin, "").trim();
          die = Integer.parseInt(binString.substring(0, binString.indexOf(" ")));
          logger.debug("die " + die);
          // Get Percentage
          binString = binString.replaceAll(die.toString(), "").trim();
          percentage = binString.substring(0, binString.length() - 1);
          logger.debug("percentage " + percentage);

          cpYieldLotBinTo.setBin(bin);
          cpYieldLotBinTo.setDie(die);
          cpYieldLotBinTo.setPercentage(percentage);

          cpYieldLotBins.add(cpYieldLotBinTo);
        }

        // 1.4 Die Data
        Integer passDie;
        Integer failDie;
        Integer totelDie;
        for (int i = eb + 1; i < lineDataList.size(); i++) {
          // pass die
          if (lineDataList.get(i).trim().indexOf("pass die") >= 0) {
            passDie = Integer.parseInt(lineDataList.get(i).trim().split(":")[1].trim());
            logger.debug("passDie " + passDie);
            cpYieldLotTo.setPassDie(passDie);
            continue;
          }
          // fail die
          if (lineDataList.get(i).trim().indexOf("fail die") >= 0) {
            failDie = Integer.parseInt(lineDataList.get(i).trim().split(":")[1].trim());
            logger.debug("failDie " + failDie);
            cpYieldLotTo.setFailDie(failDie);
            continue;
          }
          // totel die
          if (lineDataList.get(i).trim().indexOf("totel die") >= 0) {
            totelDie = Integer.parseInt(lineDataList.get(i).trim().split(":")[1].trim());
            logger.debug("totelDie " + totelDie);
            cpYieldLotTo.setTotelDie(totelDie);
            continue;
          }
        }

        // 1.5 Set data in To
        cpYieldLotTo.setCpYieldUuid(cpYieldUuid);
        cpYieldLotTo.setCpTestTimes(cpTestTimes);
        cpYieldLotTo.setCpLot(cpLot);
        cpYieldLotTo.setWaferId(waferId);
        cpYieldLotTo.setMachineId(machineId);
        cpYieldLotTo.setxMaxCoor(xMaxCoor);
        cpYieldLotTo.setyMaxCoor(yMaxCoor);
        cpYieldLotTo.setFlat(flat);

        String fileMimeType = new MimetypesFileTypeMap().getContentType(txtFile);
        cpYieldLotTo.setFileName(
            df2.format(calendar.getTime()).toString() + File.separator + fileNameUid);
        cpYieldLotTo.setFileMimeType(fileMimeType);
        cpYieldLotTo.setFtpFlag("N");

        fIn.close();
        br.close();

        Methods.copyFile(txtFile, bkFile);
        txtFile.delete();
        // 1.6 DataBasse
        // 1.6.1 Insert CP Lot Table
        cpYieldParserDao.insertCpYieldLot(cpYieldLotTo);
        cpYieldParserDao.insertCpYieldLotBin(cpYieldLotBins);
      }

      fIn.close();
      br.close();

      logger.info(txtFile.getName() + " is Parser complete");
      logger.info(fileNameUid + " is Parser complete");

      // logger.debug(tapeList.size());
      logger.info("---------------------------------");
    } catch (Exception e) {
      if (fIn != null) {
        fIn.close();
      }
      logger.info("ERROR MOVE FILE");
      Methods.copyFile(txtFile, new File(fileErrorUrl + "\\" + txtFile.getName()));
      txtFile.delete();

      StackTraceElement[] messages = e.getStackTrace();
      Exception ex = new Exception(txtFile.getName());
      ex.setStackTrace(messages);
      ex.printStackTrace();
      throw ex;
    } finally {
      try {
        if (fIn != null) {
          fIn.close();
        }
      } catch (IOException ie) {
        StackTraceElement[] messages = ie.getStackTrace();
        int length = messages.length;
        String error = "";
        for (int i = 0; i < length; i++) {
          error = error + "toString:" + messages[i].toString() + "\r\n";
        }
        ie.printStackTrace();
        logger.error(error);
        return false;
      }
    }
    return true;
  }
  public static void main(String[] args) throws Exception {

    String digestType = args[0];
    String digestFile = args[1];

    Vector<String> digestListFiles = new Vector<String>();
    Vector<byte[]> calcDigests = new Vector<byte[]>();

    BufferedReader reader = null;
    byte[] fileContent;
    int i;

    for (i = 2; i < args.length; i++) {
      digestListFiles.add(args[i]);
    }

    // le os arquivos e calcula o digest de seus conteudos.
    for (i = 0; i < digestListFiles.size(); i++) {

      File file = new File(digestListFiles.get(i));

      fileContent = readFile(file);
      MessageDigest messageDigest;

      try {
        messageDigest = MessageDigest.getInstance(digestType);
      } catch (Exception e) {
        System.out.println("Inexistant cryptography algorithm given!");
        return;
      }

      messageDigest.update(fileContent);
      calcDigests.add(messageDigest.digest());
    }

    File digestListFile = new File(digestFile);

    // verifica se o arquivo existe.
    // caso nao exista, cria um novo.
    if (!digestListFile.exists()) digestListFile.createNewFile();

    // lista dos arquivos ja lidos
    Vector<String> fileNames = new Vector<String>();
    // lista do tipos dos digest dos arquivos
    Vector<String> fileTypes = new Vector<String>();
    // lista dos digest dos conteudos dos arquivos
    Vector<String> fileDigests = new Vector<String>();

    try {
      reader = new BufferedReader(new FileReader(digestListFile));
      String line;
      String[] splitLine;

      while ((line = reader.readLine()) != null) {
        splitLine = line.split(" ");
        fileNames.add(splitLine[0]); // le o nome do arquivo
        fileTypes.add(splitLine[1]); // le o tipo do digest
        fileDigests.add(splitLine[2]); // le o digest em hex
      }

    } catch (IOException e) {
      System.out.println("Problems reading digest file! " + e.getStackTrace().toString());
    }

    STATUS status;
    int pos;
    for (i = 0; i < digestListFiles.size(); i++) {

      // procura
      pos = find(fileNames, digestListFiles.get(i), fileTypes, digestType);

      if (pos != -1) { // achou

        // verifica se o conteudo eh igual
        if (fileDigests.get(pos).contentEquals(calcHex(calcDigests.get(i)))) {
          status = STATUS.OK; // conteudo igual
        } else {
          status = STATUS.NOTOK; // conteudo diferente
        }

      } else {
        // nao achou!
        // insere no arquivo
        writeToEOF(
            digestListFiles.get(i) + " " + digestType + " " + calcHex(calcDigests.get(i)) + "\n",
            digestListFile);

        fileNames.add(digestListFiles.get(i));
        fileTypes.add(digestType);
        fileDigests.add(calcHex(calcDigests.get(i)));

        status = STATUS.NOTFOUND;
      }

      System.out.println(
          digestListFiles.get(i)
              + " "
              + digestType
              + " "
              + calcHex(calcDigests.get(i))
              + " ("
              + status.toString()
              + ")");
    }
  }