List of usage examples for java.io FileReader read
public int read() throws IOException
From source file:org.apache.slider.common.tools.SliderUtils.java
/** * Look for the windows executable and check it has the right headers. * <code>File.canRead()</code> doesn't work on windows, so the reading * is mandatory.//from w w w . j a v a 2 s . c o m * * @param program program name for errors * @param exe executable * @throws IOException IOE */ public static void verifyWindowsExe(String program, File exe) throws IOException { verifyIsFile(program, exe); verifyFileSize(program, exe, 0x100); // now read two bytes and verify the header. FileReader reader = null; try { int[] header = new int[2]; reader = new FileReader(exe); header[0] = reader.read(); header[1] = reader.read(); if ((header[0] != 'M' || header[1] != 'Z')) { throw new FileNotFoundException(program + " at " + exe + " is not a windows executable file"); } } finally { IOUtils.closeStream(reader); } }
From source file:org.kuali.kfs.gl.document.service.impl.CorrectionDocumentServiceImpl.java
public String createOutputFileForProcessing(String docId, java.util.Date today) { File outputFile = new File(glcpDirectoryName + File.separator + docId + OUTPUT_ORIGIN_ENTRIES_FILE_SUFFIX); String newFileName = batchFileDirectoryName + File.separator + GLCP_OUTPUT_PREFIX + "." + docId + buildFileExtensionWithDate(today); File newFile = new File(newFileName); FileReader inputFileReader; FileWriter newFileWriter;//w ww. j a va 2s. c om try { // copy output file and put in OriginEntryInformation directory inputFileReader = new FileReader(outputFile); newFileWriter = new FileWriter(newFile); int c; while ((c = inputFileReader.read()) != -1) { newFileWriter.write(c); } inputFileReader.close(); newFileWriter.close(); // create done file, after successfully copying output file String doneFileName = newFileName.replace(GeneralLedgerConstants.BatchFileSystem.EXTENSION, GeneralLedgerConstants.BatchFileSystem.DONE_FILE_EXTENSION); File doneFile = new File(doneFileName); if (!doneFile.exists()) { doneFile.createNewFile(); } } catch (IOException e) { throw new RuntimeException(e); } return newFileName; }
From source file:org.fhcrc.cpl.viewer.ms2.commandline.PostProcessPepXMLCLM.java
/** * * @param featureFile/*from w w w .j av a 2 s .c om*/ * @param outputFile * @throws CommandLineModuleExecutionException */ protected void handleFeatureFile(File featureFile, File outputFile) throws CommandLineModuleExecutionException { ApplicationContext.infoMessage("Handling file " + featureFile.getAbsolutePath() + "..."); if (maxFDR < Float.MAX_VALUE) { minPeptideProphet = (float) calcMinPeptideProphetForMaxFDR(featureFile); ApplicationContext.infoMessage("Minimum PeptideProphet for FDR " + maxFDR + ": " + minPeptideProphet); } try { Iterator<FeatureSet> featureSetIterator = new PepXMLFeatureFileHandler.PepXMLFeatureSetIterator( featureFile); List<File> tempFeatureFiles = new ArrayList<File>(); int numSetsProcessed = 0; while (featureSetIterator.hasNext()) { FeatureSet featureSet = featureSetIterator.next(); ApplicationContext.infoMessage("\tProcessing fraction " + (numSetsProcessed + 1) + "..."); processFeatureSet(featureSet); String baseName = MS2ExtraInfoDef.getFeatureSetBaseName(featureSet); if (baseName == null) { baseName = featureFile.getName(); if (numSetsProcessed > 0 || featureSetIterator.hasNext()) baseName = baseName + "_" + numSetsProcessed; } //if PeptideProphet was run from a directory below the directory containing the //mzXML files, we may have ../ in the baseName, which causes trouble in saving //the temporary files // while (baseName.contains(".." + File.separator)) // baseName.replaceFirst(".." + File.separator, ""); if (baseName.contains(File.separator)) baseName = baseName.substring(baseName.lastIndexOf(File.separator) + 1); File thisFractionFile = TempFileManager.createTempFile(baseName + ".pep.xml", this); featureSet.savePepXml(thisFractionFile); _log.debug("Saved fraction file as " + thisFractionFile.getAbsolutePath()); tempFeatureFiles.add(thisFractionFile); numSetsProcessed++; } ApplicationContext.infoMessage("Saving output file " + outputFile.getAbsolutePath() + " ..."); if (numSetsProcessed == 1) { FileReader in = new FileReader(tempFeatureFiles.get(0)); FileWriter out = new FileWriter(outputFile); int c; while ((c = in.read()) != -1) out.write(c); in.close(); out.close(); } else { ApplicationContext.infoMessage( "\tCombining individual fraction files... " + outputFile.getAbsolutePath() + "..."); new PepXMLFeatureFileHandler().combinePepXmlFiles(tempFeatureFiles, outputFile); } ApplicationContext.infoMessage("Done."); } catch (IOException e) { throw new CommandLineModuleExecutionException( "Failed to process features from file " + featureFile.getAbsolutePath(), e); } finally { TempFileManager.deleteTempFiles(this); } }
From source file:SWTFileViewerDemo.java
/** * Copies a file or entire directory structure. * /*www . ja v a 2s . c o m*/ * @param oldFile * the location of the old file or directory * @param newFile * the location of the new file or directory * @return true iff the operation succeeds without errors */ boolean copyFileStructure(File oldFile, File newFile) { if (oldFile == null || newFile == null) return false; // ensure that newFile is not a child of oldFile or a dupe File searchFile = newFile; do { if (oldFile.equals(searchFile)) return false; searchFile = searchFile.getParentFile(); } while (searchFile != null); if (oldFile.isDirectory()) { /* * Copy a directory */ if (progressDialog != null) { progressDialog.setDetailFile(oldFile, ProgressDialog.COPY); } if (simulateOnly) { // System.out.println(getResourceString("simulate.DirectoriesCreated.text", // new Object[] { newFile.getPath() })); } else { if (!newFile.mkdirs()) return false; } File[] subFiles = oldFile.listFiles(); if (subFiles != null) { if (progressDialog != null) { progressDialog.addWorkUnits(subFiles.length); } for (int i = 0; i < subFiles.length; i++) { File oldSubFile = subFiles[i]; File newSubFile = new File(newFile, oldSubFile.getName()); if (!copyFileStructure(oldSubFile, newSubFile)) return false; if (progressDialog != null) { progressDialog.addProgress(1); if (progressDialog.isCancelled()) return false; } } } } else { /* * Copy a file */ if (simulateOnly) { // System.out.println(getResourceString("simulate.CopyFromTo.text", // new Object[] { oldFile.getPath(), newFile.getPath() })); } else { FileReader in = null; FileWriter out = null; try { in = new FileReader(oldFile); out = new FileWriter(newFile); int count; while ((count = in.read()) != -1) out.write(count); } catch (FileNotFoundException e) { return false; } catch (IOException e) { return false; } finally { try { if (in != null) in.close(); if (out != null) out.close(); } catch (IOException e) { return false; } } } } return true; }
From source file:org.jab.docsearch.DocSearch.java
/** * Load settings// ww w. j a v a2s . c o m */ private void loadSettings() { logger.info("loadSettings() entered"); Properties props = null; // check for old incorrect setting file format File oldPropsFile = new File(fEnv.getOldUserPreferencesFile()); if (oldPropsFile.isFile()) { logger.log(NoticeLevel.NOTICE, "loadSettings() found old settings file and convert it now"); props = new Properties(); // read file and set the properties FileReader fileReader = null; try { fileReader = new FileReader(oldPropsFile); int c; StringBuilder line = new StringBuilder(); while ((c = fileReader.read()) != -1) { // is end of line if (c == '\n' || c == '\r') { String str = line.toString(); int pos = str.indexOf('='); if (pos != -1 && pos + 1 != str.length()) { String key = str.substring(0, pos); String value = str.substring(pos + 1); logger.debug("loadSettings() convert " + key + "=" + value); props.setProperty(key, value); } line = new StringBuilder(); } else { line.append((char) c); } } } catch (IOException ioe) { logger.fatal("loadSettings() failed", ioe); showMessage(dsErrLdgFi, "\n" + oldPropsFile + "\n\n : " + ioe.toString()); } finally { IOUtils.closeQuietly(fileReader); } // delete old properties file if (!isCDSearchTool) { if (!oldPropsFile.delete()) { logger.error("loadSettings() can't delete old properties file (" + oldPropsFile + ")"); } } } else { // loads prefs stored in saveSettings props = loadProperties(fEnv.getUserPreferencesFile()); } // sendEmailNotice String tmpStr = props.getProperty("sendEmailNotice", ""); if (!"".equals(tmpStr)) { sendEmailNotice = tmpStr; } // numAdminEmails tmpStr = props.getProperty("numAdminEmails", ""); int adminEmNo = 0; try { adminEmNo = Integer.parseInt(tmpStr); } catch (NumberFormatException nfe) { logger.error("loadSettings() failure with numAdminEmails (" + nfe.getMessage() + ")"); adminEmNo = 0; } if (adminEmNo > 0) { // add to arraylist of emails for (int i = 0; i < adminEmNo; i++) { tmpStr = props.getProperty("email" + i, ""); if (!"".equals(tmpStr)) { addEmail(tmpStr); } } } // maxFileSizeToIndex tmpStr = props.getProperty("maxFileSizeToIndex", ""); long newMaxFiSiInt = 0; if (!"".equals(tmpStr)) { try { newMaxFiSiInt = Long.parseLong(tmpStr); setMaxFileSize(newMaxFiSiInt); } catch (NumberFormatException nfe) { logger.error("loadSettings() failure with maxFileSizeToIndex (" + nfe.getMessage() + ")"); } } else { logger.info("loadSettings() no prefs for maxFileSizeToIndex found, using default: " + getMaxFileSize()); } // emailFormat tmpStr = props.getProperty("emailFormat", ""); if (!"".equals(tmpStr)) { emailFormat = tmpStr; } // gatewayUser tmpStr = props.getProperty("gatewayUser", ""); if (!"".equals(tmpStr)) { gatewayUser = tmpStr; } // loadExternal tmpStr = props.getProperty("loadExternal", ""); if ("false".equals(tmpStr)) { loadExternal = false; } else { loadExternal = true; } // gatewayPWD tmpStr = props.getProperty("gatewayPwd", ""); if (!"".equals(tmpStr)) { gatewayPwd = tmpStr; } // gateway tmpStr = props.getProperty("gateway", ""); if (!"".equals(tmpStr)) { gateway = tmpStr; } // browser tmpStr = props.getProperty("browser", ""); // TODO if browser empty, try to get from system with method // getBrowserFile() if (!"".equals(tmpStr)) { defaultHndlr = tmpStr; } // maxNumHitsShown tmpStr = props.getProperty("maxNumHitsShown", ""); if (!"".equals(tmpStr)) { try { maxNumHitsShown = Integer.parseInt(tmpStr); } catch (NumberFormatException nfe) { logger.error("loadSettings() failure with maxNumHitsShown", nfe); maxNumHitsShown = 250; } } // tempDir tmpStr = props.getProperty("tempDir", ""); if (!"".equals(tmpStr)) { resetTempDir(tmpStr); } // laf tmpStr = props.getProperty("laf", ""); if (!"".equals(tmpStr) && !"de.muntjak.tinylookandfeel.TinyLookAndFeel".equals(tmpStr)) { lafChosen = tmpStr; if (env.isGUIMode()) { try { UIManager.setLookAndFeel(lafChosen); SwingUtilities.updateComponentTreeUI(this); pack(); } catch (Exception e) { logger.error("loadSettings() failure with laf", e); setStatus(ERROR + " : " + e.toString()); } } } // indexDir tmpStr = props.getProperty("indexDir", ""); if (!"".equals(tmpStr)) { File testIdxDir = new File(tmpStr); if (testIdxDir.isDirectory()) { fEnv.setIndexDirectory(tmpStr); } else { logger.info("loadSettings() index dir doesn't exist (" + tmpStr + "). Using Default:" + fEnv.getIndexDirectory()); } } // workingDir tmpStr = props.getProperty("workingDir", ""); if (!"".equals(tmpStr)) { File testworkingDir = new File(tmpStr); if (testworkingDir.isDirectory()) { resetWorkingDir(tmpStr); } else { logger.info("loadSettings() index dir doesn't exist (" + tmpStr + "). Using Default:" + fEnv.getWorkingDirectory()); } } }