*************************************************************************
Takes a property file and generates new vector and lexicon files from an
existing lexicon and raw file

@throws IOException
@throws FileNotFoundException
330872
public static void generateVectorFile(String raw, String lexiconF,
            File propertiesFile) throws FileNotFoundException, IOException {
        Properties props = new Properties();
        props.load(new FileInputStream(propertiesFile));

        String vector_location = props.getProperty("vector_location");
        String newLexicon = props.getProperty("new_lexicon_file");

        boolean compact = "true".equals(props
                .getProperty("compact.attribute.nums"));

        String format = props.getProperty("format");

        // load the lexicon and the raw file
        Lexicon lexicon = new Lexicon(lexiconF);

        String weightingScheme = props.getProperty(
                "classification_weight_scheme", "tfidf");
        WeightingMethod method = WeightingMethod
                .methodFromString(weightingScheme);
        lexicon.setMethod(method);

        // get the raw file
        FileTrainingCorpus ftc = new FileTrainingCorpus(new File(raw));

        int keepNBestAttributes = Integer.parseInt(props.getProperty(
                "keepNBestAttributes", "-1"));

        if (keepNBestAttributes != -1) {
            // double scores[] = logLikelihoodAttributeFilter.getScores(ftc,
            // lexicon);
            // lexicon.setLogLikelihoodRatio(scores);
            // lexicon.keepTopNAttributesLLR(keepNBestAttributes);
            AttributeScorer scorer = logLikelihoodAttributeScorer.getScorer(
                    ftc, lexicon);
            lexicon.setAttributeScorer(scorer);
            lexicon.applyAttributeFilter(scorer, keepNBestAttributes);
        } else {
            // apply the filters on the Lexicon
            int minFreq = Integer.parseInt(props
                    .getProperty("classification_minFreq"));
            int maxFreq = Integer.MAX_VALUE;

            lexicon.pruneTermsDocFreq(minFreq, maxFreq);
        }

        // change the indices of the attributes to remove
        // gaps between them
        Map<Integer, Integer> equiv = null;
        if (compact) {
            // create a new Lexicon object
            equiv = lexicon.compact();
        }

        // save the modified lexicon file
        if (newLexicon != null)
            lexicon.saveToFile(newLexicon);

        // dump a new vector file
        Utils.writeExamples(ftc, lexicon, true, vector_location, equiv, format);
    }
1055949
@GwtIncompatible("Unnecessary")
  private void outputNameMaps() throws IOException {

    String propertyMapOutputPath = null;
    String variableMapOutputPath = null;

    // Check the create_name_map_files FLAG.
    if (config.createNameMapFiles) {
      String basePath = getMapPath(config.jsOutputFile);

      propertyMapOutputPath = basePath + "_props_map.out";
      variableMapOutputPath = basePath + "_vars_map.out";
    }

    // Check the individual FLAGS.
    if (!config.variableMapOutputFile.isEmpty()) {
      if (variableMapOutputPath != null) {
        throw new FlagUsageException("The flags variable_map_output_file and "
            + "create_name_map_files cannot both be used simultaneously.");
      }

      variableMapOutputPath = config.variableMapOutputFile;
    }

    if (!config.propertyMapOutputFile.isEmpty()) {
      if (propertyMapOutputPath != null) {
        throw new FlagUsageException("The flags property_map_output_file and "
            + "create_name_map_files cannot both be used simultaneously.");
      }

      propertyMapOutputPath = config.propertyMapOutputFile;
    }

    // Output the maps.
    if (variableMapOutputPath != null && compiler.getVariableMap() != null) {
      compiler.getVariableMap().save(variableMapOutputPath);
    }

    if (propertyMapOutputPath != null && compiler.getPropertyMap() != null) {
      compiler.getPropertyMap().save(propertyMapOutputPath);
    }
  }
5635
private void includeFile(File file)
        throws IOException
    {
        if (!file.exists())
            throw new FileNotFoundException(file.toString());
        
        if (file.isDirectory())
        {
            List list = new List(List.Unordered);       
            String[] ls = file.list();
            for (int i=0 ; i< ls.length ; i++)
                list.add(ls[i]);
            StringWriter sw = new StringWriter();
            list.write(sw);
            reader = new StringReader(sw.toString());
        }
        else
        {
            reader = new BufferedReader(new FileReader(file));
        }
    }
1103428
public void load() throws IOException {
        if (resource.toExternalForm().endsWith(".xml")) {
            // XML Format
            bundle = new XMLResourceBundle(resource.openStream());
        } else {
            // Properties file (loaded using UTF-8)
            bundle = new UTF8PropertiesResourceBundle(resource.openStream());
        }

        if (bundle.keySet().isEmpty()) {
            throw new IOException(resource.toExternalForm() + " is not a valid resource - no keys");
        }
    }
822424
private void createUtilityFiles() throws Exception {
        // create the right files in the right places
        // cats/<name>
        File ds = new File(mapsetPath + File.separator + GrassLegacyConstans.CATS + File.separator + name);
        OutputStreamWriter catsWriter = new OutputStreamWriter(new FileOutputStream(ds));
        catsWriter.write("# xyz categories\n#\n#\n 0.00 0.00 0.00 0.00");
        catsWriter.close();

        // cell/<name>
        ds = new File(mapsetPath + File.separator + GrassLegacyConstans.CELL + File.separator + name);
        OutputStreamWriter cellWriter = new OutputStreamWriter(new FileOutputStream(ds));
        cellWriter.write("");
        cellWriter.close();

        // cell_misc/<name>/<files>
        // the directory <name> in cell_misc has already been created in
        // writeMapInActiveRegion (or extended) of the Class Mapset (or
        // extended)

        // f_format
        ds = new File(mapsetPath + File.separator + GrassLegacyConstans.CELL_MISC + File.separator + name + File.separator
                + GrassLegacyConstans.CELLMISC_FORMAT);
        OutputStreamWriter cell_miscFormatWriter = new OutputStreamWriter(new FileOutputStream(ds));
        if (outputToDiskType * 4 == 8) {
            cell_miscFormatWriter.write("type: double\nbyte_order: xdr\nlzw_compression_bits: -1");
        } else {
            cell_miscFormatWriter.write("type: float\nbyte_order: xdr\nlzw_compression_bits: -1");
        }

        cell_miscFormatWriter.close();

        // f_quant
        ds = new File(mapsetPath + File.separator + GrassLegacyConstans.CELL_MISC + File.separator + name + File.separator
                + GrassLegacyConstans.CELLMISC_QUANT);
        OutputStreamWriter cell_miscQantWriter = new OutputStreamWriter(new FileOutputStream(ds));
        cell_miscQantWriter.write("round");
        cell_miscQantWriter.close();

        // f_range
        ds = new File(mapsetPath + File.separator + GrassLegacyConstans.CELL_MISC + File.separator + name + File.separator
                + GrassLegacyConstans.CELLMISC_RANGE);
        OutputStream cell_miscRangeStream = new FileOutputStream(ds);
        // if (logger.isDebugEnabled())
        // logger.debug("RRRRRRRRRRRRRANGES: " + range[0] + "/" + range[1]);
        cell_miscRangeStream.write(FileUtilities.double2bytearray(range[0]));
        cell_miscRangeStream.write(FileUtilities.double2bytearray(range[1]));
        cell_miscRangeStream.close();

        // write the file cellhd
        /*
         * this won't work since the default window lost the proj information somewhere. When it is
         * set at the begin, it is there. This is a problem, since grass 6.0 complains about wrong
         * proj!!! It doesn't show rasters. createCellhd(GrassEnvironmentManager.getInstance()
         * .getCurrentSessionGrassEnvironment().getLocation().getDefaultWindow() .getProj(),
         * GrassEnvironmentManager.getInstance()
         * .getCurrentSessionGrassEnvironment().getLocation().getDefaultWindow() .getZone(),
         * dataWindow.getNorth(), dataWindow.getSouth(), dataWindow .getEast(),
         * dataWindow.getWest(), dataWindow.getCols(), dataWindow .getRows(),
         * dataWindow.getNSResolution(), dataWindow.getWEResolution(), -1, 1);
         */

        /*
         * need to reread the wind file to get the proj and zone (GRASS will not work if the cellhd
         * is not equal to the WIND proj)
         */
        Window tmp = new Window(locationPath + File.separator + GrassLegacyConstans.PERMANENT_MAPSET + File.separator
                + GrassLegacyConstans.WIND);
        createCellhd(tmp.getProj(), tmp.getZone(), dataWindow.getNorth(), dataWindow.getSouth(), dataWindow.getEast(),
                dataWindow.getWest(), dataWindow.getCols(), dataWindow.getRows(), dataWindow.getNSResolution(),
                dataWindow.getWEResolution(), -1, 1);

        // hist/<name>
        ds = new File(mapsetPath + File.separator + GrassLegacyConstans.HIST + File.separator + name);
        OutputStreamWriter windFile = new OutputStreamWriter(new FileOutputStream(ds));
        Date date = new Date();
        windFile.write(date + "\n");
        windFile.write(name + "\n");
        windFile.write(mapsetPath + "\n");
        windFile.write("generic user\n");
        windFile.write("DCELL\n");
        windFile.write("\n\nCreated by JGrass\n");
        windFile.write(historyComment + "\n");
        windFile.close();
        // now all the files have been created
    }
592693
public static void createFile(final File dest, final InputStream sourceInput) throws IOException {
        if (sourceInput == null || dest == null)
            return;

        FileOutputStream fos = null;
        try {
            if (!dest.getParentFile().exists()) {
                if (!dest.getParentFile().mkdirs()) {
                    throw new FileNotFoundException();
                }
            }
            fos = TextFileOutputStreamFactory.createOutputStream(dest);
            byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];

            int count = -1;
            while ((count = sourceInput.read(buffer)) > 0) {
                fos.write(buffer, 0, count);
            }
            fos.flush();
        } finally {
            Utils.tryToClose(fos);
            Utils.tryToClose(sourceInput);
        }

    }
188673
public static void main(String[] args) throws InterruptedException, FileNotFoundException {
    SpeechToText service = new SpeechToText();
    IamOptions options = new IamOptions.Builder()
        .apiKey("<iam_api_key>")
        .build();
    service.setIamCredentials(options);

    // Create language model
    CreateLanguageModelOptions createOptions = new CreateLanguageModelOptions.Builder()
        .name("IEEE-permanent")
        .baseModelName("en-US_BroadbandModel")
        .description("My customization")
        .build();
    LanguageModel myModel = service.createLanguageModel(createOptions).execute().getResult();
    String id = myModel.getCustomizationId();

    try {
      // Add a corpus file to the model
      AddCorpusOptions addOptions = new AddCorpusOptions.Builder()
          .customizationId(id)
          .corpusName("corpus-1")
          .corpusFile(new File(CORPUS_FILE))
          .corpusFileContentType(HttpMediaType.TEXT_PLAIN)
          .allowOverwrite(false)
          .build();
      service.addCorpus(addOptions).execute();

      // Get corpus status
      GetCorpusOptions getOptions = new GetCorpusOptions.Builder()
          .customizationId(id)
          .corpusName("corpus-1")
          .build();
      for (
          int x = 0; x < 30
          && (service.getCorpus(getOptions).execute().getResult()).getStatus() != Corpus.Status.ANALYZED; x++
      ) {
        Thread.sleep(5000);
      }

      // Get all corpora
      ListCorporaOptions listCorporaOptions = new ListCorporaOptions.Builder()
          .customizationId(id)
          .build();
      Corpora corpora = service.listCorpora(listCorporaOptions).execute().getResult();
      System.out.println(corpora);

      // Get specific corpus
      Corpus corpus = service.getCorpus(getOptions).execute().getResult();
      System.out.println(corpus);

      // Now add some user words to the custom model
      service.addWord(new AddWordOptions.Builder()
          .customizationId(id)
          .wordName("IEEE")
          .word("IEEE")
          .displayAs("IEEE")
          .addSoundsLike("I. triple E.")
          .build()).execute();
      service.addWord(new AddWordOptions.Builder()
          .customizationId(id)
          .wordName("hhonors")
          .word("hhonors")
          .displayAs("IEEE")
          .addSoundsLike("H. honors")
          .addSoundsLike("Hilton honors")
          .build()).execute();

      // Display all words in the words resource (OOVs from the corpus and
      // new words just added) in ascending alphabetical order
      ListWordsOptions listWordsAlphabeticalOptions = new ListWordsOptions.Builder()
          .customizationId(id)
          .wordType(ListWordsOptions.WordType.ALL)
          .build();
      Words words = service.listWords(listWordsAlphabeticalOptions).execute().getResult();
      System.out.println("\nASCENDING ALPHABETICAL ORDER:");
      System.out.println(words);

      // Then display all words in the words resource in descending order
      // by count
      ListWordsOptions listWordsCountOptions = new ListWordsOptions.Builder()
          .customizationId(id)
          .wordType(ListWordsOptions.WordType.ALL)
          .sort("-" + ListWordsOptions.Sort.COUNT)
          .build();
      words = service.listWords(listWordsCountOptions).execute().getResult();
      System.out.println("\nDESCENDING ORDER BY COUNT:");
      System.out.println(words);

      // Now start training of the model
      TrainLanguageModelOptions trainOptions = new TrainLanguageModelOptions.Builder()
          .customizationId(id)
          .wordTypeToAdd(TrainLanguageModelOptions.WordTypeToAdd.ALL)
          .build();
      service.trainLanguageModel(trainOptions).execute();

      for (int x = 0; x < 30 && myModel.getStatus() != LanguageModel.Status.AVAILABLE; x++) {
        GetLanguageModelOptions getLanguageModelOptions = new GetLanguageModelOptions.Builder()
            .customizationId(id)
            .build();
        myModel = service.getLanguageModel(getLanguageModelOptions).execute().getResult();
        Thread.sleep(10000);
      }

      File audio = new File(AUDIO_FILE);
      RecognizeOptions recognizeOptionsWithModel = new RecognizeOptions.Builder()
          .model(RecognizeOptions.Model.EN_US_BROADBANDMODEL)
          .customizationId(id)
          .audio(audio)
          .contentType(HttpMediaType.AUDIO_WAV)
          .build();
      RecognizeOptions recognizeOptionsWithoutModel = new RecognizeOptions.Builder()
          .model(RecognizeOptions.Model.EN_US_BROADBANDMODEL)
          .audio(audio)
          .contentType(HttpMediaType.AUDIO_WAV)
          .build();

      // First decode WITHOUT the custom model
      SpeechRecognitionResults transcript = service.recognize(recognizeOptionsWithoutModel).execute().getResult();
      System.out.println(transcript);

      // Now decode with the custom model
      transcript = service.recognize(recognizeOptionsWithModel).execute().getResult();
      System.out.println(transcript);
    } finally {
      DeleteLanguageModelOptions deleteOptions = new DeleteLanguageModelOptions.Builder()
          .customizationId(id)
          .build();
      service.deleteLanguageModel(deleteOptions).execute();
    }

  }
593473
static public Properties loadProperties(Properties base, File file) throws IOException {
	FileReader reader = new FileReader(file);
	Properties props = new Properties();
	props.load(reader);
	if (base != null) props.putAll(base);
	reader.close();
	return props;
    }
1060588
public void generate() throws IOException {
        final Document doc = DocumentFactory.getInstance().createDocument();
        final Element root = doc.addElement("project");
        //iterate through entities in correct order
        for (final ResourceXMLParser.Entity entity : entities) {
            if (NODE_ENTITY_TAG.equals(entity.getResourceType())) {
                final Element ent = genEntityCommon(root, entity);
                genNode(ent, entity);
            }
        }

        if (null != file) {
            FileOutputStream out=new FileOutputStream(file);
            try{
                serializeDocToStream(out, doc);
            }finally{
                out.close();
            }
        } else if (null != output) {
            serializeDocToStream(output, doc);
        }
    }
1196258
static void createFileFromResource(String resource, Resource file, String password) throws IOException {
	SystemOut.printDate(SystemUtil.getPrintWriter(SystemUtil.OUT), "write file:" + file);
	file.delete();

	InputStream is = InfoImpl.class.getResourceAsStream(resource);
	if (is == null) throw new IOException("file [" + resource + "] does not exist.");
	file.createNewFile();
	IOUtil.copy(is, file, true);
    }
265999
@Override
    public InputStream newInputStream() throws FileNotFoundException, NewInputStreamException {
        if (isDirectory()) {
            throw new FileNotFoundException(this, "file not found - this is a directory");
        }
        try {
            return Files.newInputStream(path);
        } catch (NoSuchFileException e) {
            throw new FileNotFoundException(this, e);
        } catch (IOException e) {
            throw new NewInputStreamException(this, e);
        }
    }
200513
protected void create(File file) throws IOException {
        if(!file.exists()) {
            File dir = file.getParentFile();
            if(dir.exists()) file.createNewFile();
            else if(dir.mkdirs()) file.createNewFile();
            else throw new IOException("Failed to create " + file.getAbsolutePath());
        }
        
        if(file.isDirectory()) {
            throw new IOException("Cannot open directory " + file.getAbsolutePath());
        }
    }
438111
public static void copyFile(File sourceFile, File destinationFile) throws FileNotFoundException, IOException {
		Streams.copyStream(new FileInputStream(sourceFile), new FileOutputStream(destinationFile), true);
	}
650522
@Override
	protected void loadProperties(final Properties props) throws IOException {
		if (locations != null) {
			for (int i = 0; i < locations.length; i++) {
				final Resource location = locations[i];
				if (logger.isInfoEnabled()) {
					logger.info("Loading properties file from " + location);
				}
				File file = null;
				InputStream is = null;
				try {
					try {
						file = location.getFile();
						is = new BufferedInputStream(new FileInputStream(file));
					} catch (final IOException ie) {// ignore
					} finally {
						if (file == null) {
							is = location.getInputStream();
						}
					}

					if (location.getFilename().toLowerCase().endsWith(Constants.XML_FILE_EXTENSION)) {
						persister.loadFromXml(props, is);
					} else if (location.getFilename().toLowerCase().endsWith(Constants.JSON_FILE_EXTENSION)) {
						if (fileEncoding != null) {
							persister.loadFromJson(props, new InputStreamReader(is, Charset.forName(fileEncoding)));
						} else {
							persister.loadFromJson(props, is);
						}
					} else if (location.getFilename().toLowerCase().endsWith(Constants.YAML_FILE_EXTENSION)) {
						if (fileEncoding != null) {
							persister.loadFromYAML(props, new InputStreamReader(is, Charset.forName(fileEncoding)));
						} else {
							persister.loadFromYAML(props, is);
						}
					} else {
						if (fileEncoding != null) {
							persister.load(props, new InputStreamReader(is, Charset.forName(fileEncoding)));
						} else {
							persister.load(props, is);
						}
					}
				} catch (final IOException ex) {
					if (ignoreResourceNotFound) {
						if (logger.isWarnEnabled()) {
							logger.warn("Could not load properties from " + location + ": " + ex.getMessage());
						}
					} else {
						throw ex;
					}
				} finally {
					if (is != null) {
						is.close();
					}
				}
			}
		}
	}
641328
private void buildupPropertiesBundles(final File file) throws IOException {
		File[] files = file.listFiles();

		for (File f : files) {
			if (f.getName().endsWith("properties")) {
				String bundleName = f.getName().substring(0,
						f.getName().indexOf("properties") - 1);

				LOG.info("Loading: " + bundleName);
				ResourceBundle bundle = new PropertyResourceBundle(
						new FileInputStream(f));
				bundles.put(bundleName, bundle);
			}
		}
	}
1072869
private void writeProperties() throws IOException
   {
      writeAttributeTypes("property_types", ProjectField.values());
      writeFields("property_values", m_projectFile.getProjectProperties(), ProjectField.values());
   }
519048
public static Properties fromFile(
    final File file)
    throws IOException
  {
    Objects.requireNonNull(file, "File");

    try (InputStream stream = Files.newInputStream(file.toPath())) {
      final Properties p = new Properties();
      p.load(stream);
      return p;
    }
  }