Example usage for java.io LineNumberReader skip

List of usage examples for java.io LineNumberReader skip

Introduction

In this page you can find the example usage for java.io LineNumberReader skip.

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skip characters.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    int i;// w  ww  . j  a  v a 2  s.  com
    // create new reader
    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr);

    // read till the end of the stream
    while ((i = lnr.read()) != -1) {
        // skip one byte
        lnr.skip(1);

        // converts integer to char
        char c = (char) i;

        // prints char
        System.out.print(c);
    }
    lnr.close();
}

From source file:Main.java

public static void main(String[] args) throws IOException {
    int i;/*from   w  w  w. ja  v  a 2  s  . c om*/
    // create new reader
    FileReader fr = new FileReader("C:/test.txt");
    LineNumberReader lnr = new LineNumberReader(fr, 200);

    // read till the end of the stream
    while ((i = lnr.read()) != -1) {
        // skip one byte
        lnr.skip(1);

        // converts integer to char
        char c = (char) i;

        // prints char
        System.out.print(c);
    }
    lnr.close();
}

From source file:Main.java

public static int getLineCount(String path) {
    LineNumberReader lnr = null;
    try {/*w w  w. j  a  va  2s . co  m*/
        lnr = new LineNumberReader(new FileReader(new File(path)));
        lnr.skip(Long.MAX_VALUE);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (lnr == null)
        return 0;
    return lnr.getLineNumber();
}

From source file:nl.uva.illc.dataselection.InvitationModel.java

public static void readFiles() throws IOException, InterruptedException {

    log.info("Reading files");

    src_codes = HashObjIntMaps.newMutableMap();
    trg_codes = HashObjIntMaps.newMutableMap();
    src_codes.put(null, 0);/*from   ww w . j a  v  a  2 s.  co m*/
    trg_codes.put(null, 0);

    LineNumberReader lr = new LineNumberReader(new FileReader(IN + "." + SRC));
    lr.skip(Long.MAX_VALUE);
    int indomain_size = lr.getLineNumber();
    lr.close();

    lr = new LineNumberReader(new FileReader(MIX + "." + SRC));
    lr.skip(Long.MAX_VALUE);
    int mixdomain_size = lr.getLineNumber();
    lr.close();

    src_indomain = new int[indomain_size][];
    trg_indomain = new int[indomain_size][];
    src_mixdomain = new int[mixdomain_size][];
    trg_mixdomain = new int[mixdomain_size][];

    latch = new CountDownLatch(2);
    readFile(IN + "." + SRC, src_codes, src_indomain);
    readFile(IN + "." + TRG, trg_codes, trg_indomain);
    latch.await();

    latch = new CountDownLatch(2);
    readFile(MIX + "." + SRC, src_codes, src_mixdomain);
    readFile(MIX + "." + TRG, trg_codes, trg_mixdomain);
    latch.await();

}

From source file:com.codecrate.shard.transfer.pcgen.PcgenObjectImporter.java

private int getNumberOfFileLines(File file) {
    int lines = 0;
    try {/* w ww . j  a v  a2s.c o  m*/
        long lastByte = file.length();
        LineNumberReader lineRead = new LineNumberReader(new FileReader(file));
        lineRead.skip(lastByte);
        lines = lineRead.getLineNumber() - 1;
        lineRead.close();
    } catch (IOException e) {
        LOG.warn("Error counting the number of lines in file: " + file, e);
    }
    return lines;
}

From source file:eu.eexcess.diversityasurement.wikipedia.RDFCategoryExtractor.java

private long getTotalNumberOfLines(File file) throws FileNotFoundException, IOException {
    long lines = 0;
    LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(file));
    lineNumberReader.skip(Long.MAX_VALUE);
    lines = lineNumberReader.getLineNumber();
    lineNumberReader.close();//from   w  w  w  . j a  v  a 2  s  .c o m
    return lines;
}

From source file:org.openmrs.module.initializer.AddressHierarchyMessagesLoadingTest.java

@Test
@Verifies(value = "should load i18n messages specific to the address hierarchy configuration", method = "refreshCache()")
public void refreshCache_shouldLoadAddressHierarchyMessages() throws IOException {

    // Replay/*from ww w  . jav a 2  s . c om*/
    inizSrc.refreshCache();
    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), "");
}

From source file:StockForecast.Stock.java

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
    // TODO add your handling code here:
    try {/*  www .  j  av  a2 s  .c o  m*/
        converttoraw();
        FileReader fr = new FileReader("RawData.txt");
        BufferedReader br = new BufferedReader(fr);
        LineNumberReader lnr = new LineNumberReader(new FileReader(new File("RawData.txt")));
        lnr.skip(Long.MAX_VALUE);
        int lineNum = lnr.getLineNumber();
        System.out.println(lineNum);
        lnr.close();
        Connect connect = new Connect();
        ResultSet rs = connect.connectSelect("1", "SELECT * FROM ROOT.STOCK");
        ArrayList<String> stringList = new ArrayList<>();
        while (rs.next()) {
            String sym = rs.getString("symbol");
            System.out.println(sym);
            stringList.add(sym);
        }
        for (int i = 0; i < lineNum; i++) {
            String lineNumber = br.readLine();
            String[] parts = lineNumber.split(",");
            String name = parts[0];
            String symbol = parts[1];
            if (stringList.contains(symbol)) {
                System.out.println(symbol + " : Is Already Added");
            } else if (symbol.length() > 4) {
                System.out.println(symbol + " : Is Not a Stock Symbol");
            } else {
                connect.connectInsert("INSERT INTO STOCK VALUES ('" + symbol + "','" + name + "')");
            }
        }
        br.close();
        fr.close();
    } catch (Exception e) {
        JOptionPane.showMessageDialog(this, e.getMessage());
    }
    try {
        displayStock();
    } catch (Exception ex) {
        Logger.getLogger(Stock.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:de.upb.timok.run.GenericSmacPipeline.java

private void splitTrainTestFile(String timedInputFile, String timedInputTrainFile, String timedInputTestFile,
        double trainPercentage, double testPercentage, double anomalyPercentage, boolean isRti)
        throws IOException {
    logger.info("TimedInputFile=" + timedInputFile);
    final File f = new File(timedInputFile);
    System.out.println(f);/*from  ww w . jav a 2s  . c  o m*/
    final LineNumberReader lnr = new LineNumberReader(new FileReader(timedInputFile));
    lnr.skip(Long.MAX_VALUE);
    int samples = lnr.getLineNumber();
    lnr.close();
    final int trainingSamples = (int) (samples * trainPercentage);
    final int testSamples = (int) (samples * testPercentage);
    final int anomalies = (int) (anomalyPercentage * testSamples);
    final int writtenTrainingSamples = 0;
    final int writtenTestSamples = 0;
    int insertedAnomalies = 0;
    final BufferedReader br = Files.newBufferedReader(Paths.get(timedInputFile), StandardCharsets.UTF_8);
    String line = null;
    final BufferedWriter trainWriter = Files.newBufferedWriter(Paths.get(timedInputTrainFile),
            StandardCharsets.UTF_8);
    final BufferedWriter testWriter = Files.newBufferedWriter(Paths.get(timedInputTestFile),
            StandardCharsets.UTF_8);
    final Random r = new Random(MasterSeed.nextLong());
    final Random mutation = new Random(MasterSeed.nextLong());
    boolean force = false;
    int lineIndex = 0;
    int linesLeft;
    int anomaliesToInsert;
    if (isRti) {
        br.readLine();
        samples--;
    }
    while ((line = br.readLine()) != null) {
        if (writtenTrainingSamples < trainingSamples && writtenTestSamples < testSamples) {
            // choose randomly according to train/test percentage
            if (r.nextDouble() > testPercentage) {
                // write to train
                writeSample(new TimedSequence(line, true, false).toTrebaString(), trainWriter);
            } else {
                // write to test
                insertedAnomalies = testAndWriteAnomaly(anomalies, insertedAnomalies, anomalyPercentage, line,
                        testWriter, mutation, force);
            }
        } else if (writtenTrainingSamples >= trainingSamples) {
            insertedAnomalies = testAndWriteAnomaly(anomalies, insertedAnomalies, anomalyPercentage, line,
                    testWriter, mutation, force);
        } else if (writtenTestSamples >= testSamples) {
            // only write trainSamples from now on
            writeSample(new TimedSequence(line, true, false).toTrebaString(), trainWriter);
        }
        lineIndex++;
        linesLeft = samples - lineIndex;
        anomaliesToInsert = anomalies - insertedAnomalies;
        if (linesLeft <= anomaliesToInsert) {
            force = true;
        }
    }
    br.close();
    trainWriter.close();
    testWriter.close();
}

From source file:org.wso2.carbon.ml.rest.api.neuralNetworks.FeedForwardNetwork.java

/**
 * method to createFeedForwardNetwork./* ww w  .  j a  va2  s.  c o  m*/
 * @param seed
 * @param learningRate
 * @param analysisID
 * @param bachSize
 * @param backprop
 * @param hiddenList
 * @param inputLayerNodes
 * @param iterations
 * @param versionID
 * @param momentum
 * @param nepoches
 * @param datasetId
 * @param noHiddenLayers
 * @param optimizationAlgorithms
 * @param outputList
 * @param pretrain
 * @param updater
 * @return an String object with evaluation result.
 */
public String createFeedForwardNetwork(long seed, double learningRate, int bachSize, double nepoches,
        int iterations, String optimizationAlgorithms, String updater, double momentum, boolean pretrain,
        boolean backprop, int noHiddenLayers, int inputLayerNodes, int datasetId, int versionID, int analysisID,
        List<HiddenLayerDetails> hiddenList, List<OutputLayerDetails> outputList)
        throws IOException, InterruptedException {

    String evaluationDetails = null;
    int numLinesToSkip = 0;
    String delimiter = ",";
    mlDataSet = getDatasetPath(datasetId, versionID);
    analysisFraction = getAnalysisFraction(analysisID);
    analysisResponceVariable = getAnalysisResponseVariable(analysisID);
    responseIndex = getAnalysisResponseVariableIndex(analysisID);
    SplitTestAndTrain splitTestAndTrain;
    DataSet currentDataset;
    DataSet trainingSet = null;
    DataSet testingSet = null;
    INDArray features = null;
    INDArray labels = null;
    INDArray predicted = null;
    Random rnd = new Random();
    int labelIndex = 0;
    int numClasses = 0;
    int fraction = 0;

    //Initialize RecordReader
    RecordReader rr = new CSVRecordReader(numLinesToSkip, delimiter);
    //read the dataset
    rr.initialize(new FileSplit(new File(mlDataSet)));
    labelIndex = responseIndex;
    numClasses = outputList.get(0).outputNodes;

    //Get the fraction to do the spliting data to training and testing
    FileReader fr = new FileReader(mlDataSet);
    LineNumberReader lineNumberReader = new LineNumberReader(fr);
    //Get the total number of lines
    lineNumberReader.skip(Long.MAX_VALUE);
    int lines = lineNumberReader.getLineNumber();

    //handling multiplication of 0 error
    if (analysisFraction == 0) {
        return null;
    }

    //Take floor value to set the numHold of training data
    fraction = ((int) Math.floor(lines * analysisFraction));

    org.nd4j.linalg.dataset.api.iterator.DataSetIterator trainIter = new RecordReaderDataSetIterator(rr, lines,
            labelIndex, numClasses);

    //Create NeuralNetConfiguration object having basic settings.
    NeuralNetConfiguration.ListBuilder neuralNetConfiguration = new NeuralNetConfiguration.Builder().seed(seed)
            .iterations(iterations).optimizationAlgo(mapOptimizationAlgorithm(optimizationAlgorithms))
            .learningRate(learningRate).updater(mapUpdater(updater)).momentum(momentum)
            .list(noHiddenLayers + 1);

    //Add Hidden Layers to the network with unique settings
    for (int i = 0; i < noHiddenLayers; i++) {
        int nInput = 0;
        if (i == 0)
            nInput = inputLayerNodes;
        else
            nInput = hiddenList.get(i - 1).hiddenNodes;

        neuralNetConfiguration.layer(i,
                new DenseLayer.Builder().nIn(nInput).nOut(hiddenList.get(i).hiddenNodes)
                        .weightInit(mapWeightInit(hiddenList.get(i).weightInit))
                        .activation(hiddenList.get(i).activationAlgo).build());
    }

    //Add Output Layers to the network with unique settings
    neuralNetConfiguration.layer(noHiddenLayers,
            new OutputLayer.Builder(mapLossFunction(outputList.get(0).lossFunction))
                    .nIn(hiddenList.get(noHiddenLayers - 1).hiddenNodes).nOut(outputList.get(0).outputNodes)
                    .weightInit(mapWeightInit(outputList.get(0).weightInit))
                    .activation(outputList.get(0).activationAlgo).build());

    //Create MultiLayerConfiguration network
    MultiLayerConfiguration conf = neuralNetConfiguration.pretrain(pretrain).backprop(backprop).build();

    MultiLayerNetwork model = new MultiLayerNetwork(conf);
    model.init();
    model.setListeners(Collections.singletonList((IterationListener) new ScoreIterationListener(1)));

    while (trainIter.hasNext()) {
        currentDataset = trainIter.next();
        splitTestAndTrain = currentDataset.splitTestAndTrain(fraction, rnd);
        trainingSet = splitTestAndTrain.getTrain();
        testingSet = splitTestAndTrain.getTest();
        features = testingSet.getFeatureMatrix();
        labels = testingSet.getLabels();
    }

    //Train the model with the training data
    for (int n = 0; n < nepoches; n++) {
        model.fit(trainingSet);
    }

    //Do the evaluations of the model including the Accuracy, F1 score etc.
    log.info("Evaluate model....");
    Evaluation eval = new Evaluation(outputList.get(0).outputNodes);
    predicted = model.output(features, false);

    eval.eval(labels, predicted);

    evaluationDetails = "{\"Accuracy\":\"" + eval.accuracy() + "\", \"Pecision\":\"" + eval.precision()
            + "\",\"Recall\":\"" + eval.recall() + "\",\"F1Score\":\"" + eval.f1() + "\"}";
    return evaluationDetails;

}