下面列出了java.io.LineNumberReader#getLineNumber ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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;
}
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;
}
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();
}
}
/**
*
* @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();
}
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();
}
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);
}
}
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;
}
/**
* @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));
}
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();
}
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;
}
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);
}
}
@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;
}
@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 ();
}
public ScannerException(LineNumberReader stream, String message) {
super("line " + stream.getLineNumber() + ": " + message);
this.m_lineno = stream.getLineNumber();
}
@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), "ប៉ែលហែល");
}
/**
* 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");
}
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);
}
}
@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);
}
/**
* 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();
}
}
/**
* 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();
}
}