Java Code Examples for java.io.LineNumberReader#getLineNumber()

The following examples show how to use java.io.LineNumberReader#getLineNumber() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBigFileRead.java    From basic-tools with MIT License 6 votes vote down vote up
public static long getLineNumber(File file) {
    if (file.exists()) {
        try {
            FileReader fileReader = new FileReader(file);
            LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
            lineNumberReader.skip(Long.MAX_VALUE);
            long lines = lineNumberReader.getLineNumber() + 1;
            fileReader.close();
            lineNumberReader.close();
            return lines;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return 0;
}
 
Example 2
Source File: J2DBench.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static String loadOptions(FileReader fr, String filename) {
    LineNumberReader lnr = new LineNumberReader(fr);
    Group.restoreAllDefaults();
    String line;
    try {
        while ((line = lnr.readLine()) != null) {
            String reason = Group.root.setOption(line);
            if (reason != null) {
                System.err.println("Option "+line+
                                   " at line "+lnr.getLineNumber()+
                                   " ignored: "+reason);
            }
        }
    } catch (IOException e) {
        Group.restoreAllDefaults();
        return ("IO Error reading "+filename+
                " at line "+lnr.getLineNumber());
    }
    return null;
}
 
Example 3
Source File: IdlPreprocessorReader.java    From cxf with Apache License 2.0 6 votes vote down vote up
private void popInclude() throws IOException {
    final IncludeStackEntry poppedStackEntry = includeStack.pop();
    if (!includeStack.isEmpty()) {
        buf.append(LF);
    }
    try {
        if (!includeStack.isEmpty()) {
            final IncludeStackEntry newTopEntry = includeStack.peek();
            final LineNumberReader reader = getReader();
            final int lineNumber = reader.getLineNumber();
            final String location = newTopEntry.getLocation();
            signalFileChange(location, lineNumber, POP);
        }
    } finally {
        poppedStackEntry.getReader().close();
    }
}
 
Example 4
Source File: LanguageModelTrueCaser.java    From phrasal with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 
 * @param args
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
  if (args.length != 2) {
    System.err.printf("Usage: java %s lm_file uncased_input > cased_output%n",
        LanguageModelTrueCaser.class.getName());
    System.exit(-1);
  }

  LanguageModelTrueCaser trueCaser = new LanguageModelTrueCaser(args[0]);

  // enter main truecasing loop
  LineNumberReader reader = IOTools.getReaderFromFile(args[1]);
  for (String line; (line = reader.readLine()) != null;) {
    final int inputId = reader.getLineNumber() - 1;
    String output = trueCaser.trueCase(line, inputId);
    System.out.println(output);
  }
  reader.close();
}
 
Example 5
Source File: LogFileReader.java    From tlaplus with MIT License 5 votes vote down vote up
public long getLineNumbers() throws IOException {
final LineNumberReader reader = new LineNumberReader(new FileReader(logFile));
reader.skip(Long.MAX_VALUE);
final int numberOfLines = reader.getLineNumber();
reader.close();
return numberOfLines;
}
 
Example 6
Source File: MethodComparator.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private MethodPosition getIndexOfMethodPosition(final Class<?> aClass, final String methodName, final char methodSeparator)
{
    final InputStream inputStream = aClass.getResourceAsStream(aClass.getSimpleName() + ".class");
    final LineNumberReader lineNumberReader = new LineNumberReader(new InputStreamReader(inputStream));
    final String methodNameWithSeparator = methodName + methodSeparator;
    try
    {
        try
        {
            String line;
            while ((line = lineNumberReader.readLine()) != null)
            {
                if (line.contains(methodNameWithSeparator))
                {
                    return new MethodPosition(lineNumberReader.getLineNumber(), line.indexOf(methodNameWithSeparator));
                }
            }
        }
        finally
        {
            lineNumberReader.close();
        }
    }
    catch (IOException e)
    {
        return new NullMethodPosition();
    }
    return new NullMethodPosition();
}
 
Example 7
Source File: ComputeBitextIDF.java    From phrasal with GNU General Public License v3.0 5 votes vote down vote up
/**
 * @param args
 */
public static void main(String[] args) {
  if (args.length > 0) {
    System.err.printf("Usage: java %s < files > idf-file%n", ComputeBitextIDF.class.getName());
    System.exit(-1);
  }

  Counter<String> documentsPerTerm = new ClassicCounter<String>(1000000);
  LineNumberReader reader = new LineNumberReader(new InputStreamReader(System.in));
  double nDocuments = 0.0;
  try {
    for (String line; (line = reader.readLine()) != null;) {
      String[] tokens = line.trim().split("\\s+");
      Set<String> seen = new HashSet<String>(tokens.length);
      for (String token : tokens) {
        if ( ! seen.contains(token)) {
          seen.add(token);
          documentsPerTerm.incrementCount(token);
        }
      }
    }
    nDocuments = reader.getLineNumber();
    reader.close();
  } catch (IOException e) {
    e.printStackTrace();
  }

  // Output the idfs
  System.err.printf("Bitext contains %d sentences and %d word types%n", (int) nDocuments, documentsPerTerm.keySet().size());
  for (String wordType : documentsPerTerm.keySet()) {
    double count = documentsPerTerm.getCount(wordType);
    System.out.printf("%s\t%f%n", wordType, Math.log(nDocuments / count));
  }
  System.out.printf("%s\t%f%n", UNK_TOKEN, Math.log(nDocuments / 1.0));
}
 
Example 8
Source File: RSLPStemmerBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private static Rule[] parseRules(LineNumberReader r, int type) throws IOException {
  List<Rule> rules = new ArrayList<>();
  String line;
  while ((line = readLine(r)) != null) {
    Matcher matcher = stripPattern.matcher(line);
    if (matcher.matches()) {
      rules.add(new Rule(matcher.group(1), Integer.parseInt(matcher.group(2)), ""));
    } else {
      matcher = repPattern.matcher(line);
      if (matcher.matches()) {
        rules.add(new Rule(matcher.group(1), Integer.parseInt(matcher.group(2)), matcher.group(3)));
      } else {
        matcher = excPattern.matcher(line);
        if (matcher.matches()) {
          if (type == 0) {
            rules.add(new RuleWithSuffixExceptions(matcher.group(1), 
                      Integer.parseInt(matcher.group(2)), 
                      matcher.group(3), 
                      parseList(matcher.group(4))));
          } else {
            rules.add(new RuleWithSetExceptions(matcher.group(1), 
                      Integer.parseInt(matcher.group(2)), 
                      matcher.group(3), 
                      parseList(matcher.group(4))));
          }
        } else {
          throw new RuntimeException("Illegal Step rule specified at line " + r.getLineNumber());
        }
      }
    }
    if (line.endsWith(";"))
      return rules.toArray(new Rule[rules.size()]);
  }
  return null;
}
 
Example 9
Source File: SwiftFieldReader.java    From banking-swift-messages-java with MIT License 5 votes vote down vote up
private FieldLine readFieldLine(LineNumberReader lineReader) throws FieldLineParseException {
    try {
        String line = lineReader.readLine();
        return line == null ? null : new FieldLine(line);
    } catch (IOException e) {
        throw new FieldLineParseException(e.getMessage(), lineReader.getLineNumber(), e);
    }
}
 
Example 10
Source File: MosesCompoundSplitter.java    From phrasal with GNU General Public License v3.0 5 votes vote down vote up
private void loadModel(String modelFileName) throws IOException {
  System.err.println("Loading MosesCompoundSplitter from " + modelFileName);
  LineNumberReader reader = new LineNumberReader(new FileReader(modelFileName));
  
  lcModel = new ClassicCounter<String>();
  trueCase = new HashMap<>();
  double totalCount = 0.0;
  if(useUnigramProbs) probs = new ClassicCounter<String>();

  int minCnt = Math.min(MAX_COUNT, MIN_COUNT);
  
  for (String line; (line = reader.readLine()) != null;) {
    String[] input = line.split("\t");
    if(input.length != 3) {
      reader.close();
      throw new IOException("Illegal input in model file, line " + reader.getLineNumber() + ": " + line);
    }
    long cnt = Long.parseLong(input[2]);
    totalCount += cnt;
    String tc = input[1];
    if(cnt < minCnt || tc.length() < MIN_SIZE + 1) continue; // these will never be used for splitting anyway
    
    String lc = tc.toLowerCase();
    // use the most frequent casing
    if(lcModel.getCount(lc) < cnt) {
      lcModel.setCount(lc, cnt);
      trueCase.put(lc, tc);
      //System.err.println("adding: " + input[1] + " ::: " + input[2]);
    }
  }
  
  totalCount = Math.log(totalCount);
  if(useUnigramProbs) {
    for(Entry<String, Double> e : lcModel.entrySet()) {
      probs.setCount(e.getKey(), Math.log(e.getValue()) - totalCount);
    }
  }
  reader.close();
}
 
Example 11
Source File: ScannerException.java    From pegasus with Apache License 2.0 4 votes vote down vote up
public ScannerException(LineNumberReader stream, String message) {
    super("line " + stream.getLineNumber() + ": " + message);
    this.m_lineno = stream.getLineNumber();
}
 
Example 12
Source File: AddressHierarchyMessagesLoadingTest.java    From openmrs-module-initializer with MIT License 4 votes vote down vote up
@Test
@Verifies(value = "should load i18n messages specific to the address hierarchy configuration", method = "refreshCache()")
public void refreshCache_shouldLoadAddressHierarchyMessages() throws IOException {
	
	// Replay
	inizSrc.getCachedMessages();
	AddressConfigurationLoader.loadAddressConfiguration();
	
	AddressHierarchyService ahs = Context.getService(AddressHierarchyService.class);
	ahs.initI18nCache();
	InitializerService iniz = Context.getService(InitializerService.class);
	
	File csvFile = (new ConfigDirUtil(iniz.getConfigDirPath(), iniz.getChecksumsDirPath(), iniz.getRejectionsDirPath(),
	        InitializerConstants.DOMAIN_ADDR)).getConfigFile("addresshierarchy.csv");
	LineNumberReader lnr = new LineNumberReader(new FileReader(csvFile));
	lnr.skip(Long.MAX_VALUE);
	int csvLineCount = lnr.getLineNumber() + 1;
	lnr.close();
	Assert.assertTrue(csvLineCount < ahs.getAddressHierarchyEntryCount()); // there should be more entries than the
	                                                                       // number of lines in CSV import
	
	// Working in km_KH
	Context.getUserContext().setLocale(new Locale("km", "KH"));
	PersonAddress address = new PersonAddress();
	address.setStateProvince("កំពង់ស្ពឺ");
	address.setCountyDistrict("ច្បារមន");
	address.setAddress1("សុព័រទេព");
	
	// Looking for possible villages based on an address provided in km_KH
	AddressHierarchyLevel villageLevel = ahs.getAddressHierarchyLevelByAddressField(AddressField.CITY_VILLAGE);
	List<AddressHierarchyEntry> villageEntries = ahs.getPossibleAddressHierarchyEntries(address, villageLevel);
	Assert.assertFalse(CollectionUtils.isEmpty(villageEntries));
	
	// Verifying that possible villages are provided as i18n message codes
	final Set<String> expectedVillageNames = new HashSet<String>(); // filled by looking at the test CSV
	expectedVillageNames.add("addresshierarchy.tangTonle");
	expectedVillageNames.add("addresshierarchy.rumloung");
	expectedVillageNames.add("addresshierarchy.thlokChheuTeal");
	expectedVillageNames.add("addresshierarchy.trachChrum");
	expectedVillageNames.add("addresshierarchy.paelHael");
	expectedVillageNames.add("addresshierarchy.krangPhka");
	expectedVillageNames.add("addresshierarchy.runloungPrakhleah");
	expectedVillageNames.add("addresshierarchy.preyKanteach");
	expectedVillageNames.add("addresshierarchy.snaoTiPir");
	expectedVillageNames.add("addresshierarchy.roleangSangkae");
	for (AddressHierarchyEntry entry : villageEntries) {
		Assert.assertTrue(expectedVillageNames.contains(entry.getName()));
	}
	
	// Pinpointing a specific village
	address.setCityVillage("ប៉ែលហែល");
	
	// Looking for possible villages
	villageEntries = ahs.getPossibleAddressHierarchyEntries(address, villageLevel);
	
	// We should find our one village
	Assert.assertEquals(1, villageEntries.size());
	String messageKey = villageEntries.get(0).getName();
	Assert.assertEquals(messageKey, "addresshierarchy.paelHael");
	Assert.assertEquals(Context.getMessageSourceService().getMessage(messageKey), "ប៉ែលហែល");
}
 
Example 13
Source File: AbstractSecretKeySigningService.java    From packagedrone with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public void sign ( final InputStream in, final OutputStream out, final boolean inline ) throws Exception
{
    final int digest = HashAlgorithmTags.SHA1;
    final PGPSignatureGenerator signatureGenerator = new PGPSignatureGenerator ( new BcPGPContentSignerBuilder ( this.privateKey.getPublicKeyPacket ().getAlgorithm (), digest ) );

    if ( inline )
    {
        signatureGenerator.init ( PGPSignature.CANONICAL_TEXT_DOCUMENT, this.privateKey );
    }
    else
    {
        signatureGenerator.init ( PGPSignature.BINARY_DOCUMENT, this.privateKey );
    }

    final ArmoredOutputStream armoredOutput = new ArmoredOutputStream ( out );
    armoredOutput.setHeader ( "Version", VersionInformation.VERSIONED_PRODUCT );

    if ( inline )
    {
        armoredOutput.beginClearText ( digest );

        final LineNumberReader lnr = new LineNumberReader ( new InputStreamReader ( in, StandardCharsets.UTF_8 ) );

        String line;
        while ( ( line = lnr.readLine () ) != null )
        {
            if ( lnr.getLineNumber () > 1 )
            {
                signatureGenerator.update ( NL_DATA );
            }

            final byte[] data = trimTrailing ( line ).getBytes ( StandardCharsets.UTF_8 );

            if ( inline )
            {
                armoredOutput.write ( data );
                armoredOutput.write ( NL_DATA );
            }
            signatureGenerator.update ( data );
        }

        armoredOutput.endClearText ();
    }
    else
    {

        final byte[] buffer = new byte[4096];
        int rc;
        while ( ( rc = in.read ( buffer ) ) >= 0 )
        {
            signatureGenerator.update ( buffer, 0, rc );
        }
    }

    final PGPSignature signature = signatureGenerator.generate ();
    signature.encode ( new BCPGOutputStream ( armoredOutput ) );

    armoredOutput.close ();
}
 
Example 14
Source File: BlastTabularParser.java    From biojava with GNU Lesser General Public License v2.1 4 votes vote down vote up
@Override
public List<Result> createObjects(double maxEScore) throws IOException, ParseException {
	List<Result> results = new ArrayList<Result>();

	log.info("Query for hits");
	LineNumberReader  lnr = new LineNumberReader(new FileReader(targetFile));
	lnr.skip(Long.MAX_VALUE);
	fileLinesCount = lnr.getLineNumber();
	log.info(fileLinesCount + " hits approximately in all results");
	lnr.close();

	FileInputStream fileInputStream = new FileInputStream(targetFile);
	Scanner scanner = new Scanner(fileInputStream);

	String line = fetchData(scanner);
	int lineNumber=0;
	while (lineNumber < fileLinesCount){
		try {
			BlastResultBuilder resultBuilder = new BlastResultBuilder();
			resultBuilder
					.setQueryID(queryId)
					.setDbFile(databaseFile)
					.setProgram(programName)
					.setQueryDef(queryName)
					.setReference(blastReference);

			List<Hit> hits = new ArrayList<Hit>();

			String currentQueryId = queryId;
			while (currentQueryId.equals(queryId) && lineNumber < fileLinesCount){
				BlastHitBuilder hitBuilder = new BlastHitBuilder();

				List<Hsp> hsps = new ArrayList<Hsp>();

				String currentSubjectId=subjectId;
				while (currentSubjectId.equals(subjectId) && lineNumber < fileLinesCount){
					if (new Double(evalue) > maxEScore) {
						line = fetchData(scanner);
						lineNumber++;
						continue;
					}
					BlastHspBuilder hspBuilder = new BlastHspBuilder();
					hspBuilder
						.setHspAlignLen(new Integer(alnLength))
						.setHspGaps(new Integer(gapOpenCount))
						.setHspQueryFrom(new Integer(queryStart))
						.setHspQueryTo(new Integer(queryEnd))
						.setHspHitFrom(new Integer(subjectStart))
						.setHspHitTo(new Integer(subjectEnd))
						.setHspEvalue(new Double(evalue))
						.setHspBitScore(new Double(bitScore))
						.setPercentageIdentity(new Double(percIdentity)/100)
						.setMismatchCount(new Integer(mismatchCount));
					hsps.add(hspBuilder.createBlastHsp());
					if (scanner.hasNext()) line = fetchData(scanner);
					lineNumber++;
				}
				hits.add(hitBuilder.setHsps(hsps).createBlastHit());
			}
			results.add(resultBuilder.setHits(hits).createBlastResult());
		} catch (NumberFormatException e) {
			throw new ParseException("Invalid numeric value met at line "+ lineNumber+" in:\n"+line,0);
		}
	}
	return results;
}
 
Example 15
Source File: Matrix.java    From KEEL with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Reads a matrix from a reader. The first line in the file should
 * contain the number of rows and columns. Subsequent lines
 * contain elements of the matrix.
 * (FracPete: taken from old keel.Algorithms.Statistical_Classifiers.Logistic.core.Matrix class)
 *
 * @param     r the reader containing the matrix
 * @throws    Exception if an error occurs
 * @see       #write(Writer)
 */
public Matrix(Reader r) throws Exception {
  LineNumberReader lnr = new LineNumberReader(r);
  String line;
  int currentRow = -1;

  while ((line = lnr.readLine()) != null) {

    // Comments
    if (line.startsWith("%"))  
      continue;
    
    StringTokenizer st = new StringTokenizer(line);
    // Ignore blank lines
    if (!st.hasMoreTokens())  
      continue;

    if (currentRow < 0) {
      int rows = Integer.parseInt(st.nextToken());
      if (!st.hasMoreTokens())
        throw new Exception("Line " + lnr.getLineNumber() 
            + ": expected number of columns");

      int cols = Integer.parseInt(st.nextToken());
      A = new double[rows][cols];
      m = rows;
      n = cols;
      currentRow++;
      continue;

    } 
    else {
      if (currentRow == getRowDimension())
        throw new Exception("Line " + lnr.getLineNumber() 
            + ": too many rows provided");

      for (int i = 0; i < getColumnDimension(); i++) {
        if (!st.hasMoreTokens())
          throw new Exception("Line " + lnr.getLineNumber() 
              + ": too few matrix elements provided");

        set(currentRow, i, Double.valueOf(st.nextToken()).doubleValue());
      }
      currentRow++;
    }
  }

  if (currentRow == -1)
    throw new Exception("Line " + lnr.getLineNumber() 
        + ": expected number of rows");
  else if (currentRow != getRowDimension())
    throw new Exception("Line " + lnr.getLineNumber() 
        + ": too few rows provided");
}
 
Example 16
Source File: ClassAndMethodDetails.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static ClassAndMethodDetails create(LineNumberReader in) throws IOException {
    String line;
    while ((line = in.readLine()) != null) {
      line = line.trim();
      if (line.length() == 0 || line.startsWith("#") || line.startsWith("//")) {
        continue;
      }
      break;
    }
    if (line == null) {
      return null;
    }
    ClassAndMethodDetails instance = new ClassAndMethodDetails();
    String[] fields = line.split(",");
    try {
      instance.className = fields[0];
      int numMethods = Integer.parseInt(fields[1]);
      for (int i=0; i<numMethods; i++) {
        line = in.readLine();
        fields = line.split(",");
        String methodName = fields[0];
        int codeLength = Integer.parseInt(fields[1]);
        String codeString = fields[2];
        int codeStringLength = codeString.length();
        if (codeStringLength != codeLength*2) {
          System.err.println("Code string has been tampered with on line " + in.getLineNumber());
          continue;
        }
        byte[] code = new byte[codeLength];
        int codeIdx=0;
        for (int j=0; j<codeStringLength; j+=2) {
          String substr = codeString.substring(j, j+2);
//            System.out.println("parsing " + j + ": '" + substr + "'");
          code[codeIdx++] = (byte)(0xff & Integer.parseInt(substr, 16));
        }
        instance.methodCode.put(methodName, code);
      }
      return instance;
    } catch (Exception e) {
      throw new IOException("Error parsing line " + in.getLineNumber(), e);
    }
  }
 
Example 17
Source File: ClassAndMethodDetails.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public static ClassAndMethodDetails create(LineNumberReader in) throws IOException {
    String line;
    while ((line = in.readLine()) != null) {
      line = line.trim();
      if (line.length() == 0 || line.startsWith("#") || line.startsWith("//")) {
        continue;
      }
      break;
    }
    if (line == null) {
      return null;
    }
    ClassAndMethodDetails instance = new ClassAndMethodDetails();
    String[] fields = line.split(",");
    try {
      instance.className = fields[0];
      int numMethods = Integer.parseInt(fields[1]);
      for (int i=0; i<numMethods; i++) {
        line = in.readLine();
        fields = line.split(",");
        String methodName = fields[0];
        int codeLength = Integer.parseInt(fields[1]);
        String codeString = fields[2];
        int codeStringLength = codeString.length();
        if (codeStringLength != codeLength*2) {
          System.err.println("Code string has been tampered with on line " + in.getLineNumber());
          continue;
        }
        byte[] code = new byte[codeLength];
        int codeIdx=0;
        for (int j=0; j<codeStringLength; j+=2) {
          String substr = codeString.substring(j, j+2);
//            System.out.println("parsing " + j + ": '" + substr + "'");
          code[codeIdx++] = (byte)(0xff & Integer.parseInt(substr, 16));
        }
        instance.methodCode.put(methodName, code);
      }
      return instance;
    } catch (Exception e) {
      throw new IOException("Error parsing line " + in.getLineNumber(), e);
    }
  }
 
Example 18
Source File: RunQuery.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Read the contents of a file.
 * <p>
 * Note: This makes default platform assumptions about the encoding of the
 * file.
 * 
 * @param file
 *            The file.
 * @return The file's contents.
 * 
 * @throws IOException
 */
static private String readFromFile(final File file) throws IOException {

	final LineNumberReader r = new LineNumberReader(new FileReader(file));

       try {

           final StringBuilder sb = new StringBuilder();

           String s;
           while ((s = r.readLine()) != null) {

               if (r.getLineNumber() > 1)
                   sb.append("\n");

               sb.append(s);

           }

           return sb.toString();

       } finally {

           r.close();

       }

   }
 
Example 19
Source File: CountLinesTextFile.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void count_lines_text_java() throws IOException {

	LineNumberReader lineReader = new LineNumberReader(new FileReader(Paths
			.get(fileLocation).toFile()));
	lineReader.skip(Long.MAX_VALUE);

	long totalNumberOfLines = lineReader.getLineNumber() + 1;

	lineReader.close();

	assertEquals(10, totalNumberOfLines);
}
 
Example 20
Source File: RunQuery.java    From database with GNU General Public License v2.0 3 votes vote down vote up
/**
 * Read from stdin.
 * <p>
 * Note: This makes default platform assumptions about the encoding of the
 * data being read.
 * 
 * @return The data read.
 * 
 * @throws IOException
 */
static private String readFromStdin() throws IOException {

    final LineNumberReader r = new LineNumberReader(new InputStreamReader(System.in));

    try {

        final StringBuilder sb = new StringBuilder();

        String s;
        while ((s = r.readLine()) != null) {

            if (r.getLineNumber() > 1)
                sb.append("\n");

            sb.append(s);

        }

        return sb.toString();

    } finally {

        r.close();

    }

}