org.matsim.core.utils.io.IOUtils.getBufferedReader()方法的使用及代码示例

x33g5p2x  于2022-01-21 转载在 其他  
字(12.2k)|赞(0)|评价(0)|浏览(164)

本文整理了Java中org.matsim.core.utils.io.IOUtils.getBufferedReader()方法的一些代码示例,展示了IOUtils.getBufferedReader()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。IOUtils.getBufferedReader()方法的具体详情如下:
包路径:org.matsim.core.utils.io.IOUtils
类名称:IOUtils
方法名:getBufferedReader

IOUtils.getBufferedReader介绍

[英]Tries to open the specified file for reading and returns a BufferedReader for it. Supports gzip-compressed files, such files are automatically decompressed. If the file is not found, a gzip-compressed version of the file with the added ending ".gz" will be searched for and used if found. Assumes that the text in the file is stored in UTF-8 (without BOM).
[中]尝试打开指定的文件进行读取,并为其返回BufferedReader。支持gzip压缩文件,此类文件会自动解压缩。如果找不到该文件,将搜索并使用添加了结尾“.gz”的gzip压缩版本的文件。假定文件中的文本存储在UTF-8中(无BOM表)。

代码示例

代码示例来源:origin: matsim-org/matsim

  1. public static BufferedReader getBufferedReader(final URL url) throws UncheckedIOException {
  2. return getBufferedReader(url, StandardCharsets.UTF_8);
  3. }

代码示例来源:origin: matsim-org/matsim

  1. /**
  2. * Tries to open the specified file for reading and returns a BufferedReader for it.
  3. * Supports gzip-compressed files, such files are automatically decompressed.
  4. * If the file is not found, a gzip-compressed version of the file with the
  5. * added ending ".gz" will be searched for and used if found. Assumes that the text
  6. * in the file is stored in UTF-8 (without BOM).
  7. *
  8. * @param filename The file to read, may contain the ending ".gz" to force reading a compressed file.
  9. * @return BufferedReader for the specified file.
  10. * @throws UncheckedIOException
  11. *
  12. * <br> author mrieser
  13. */
  14. public static BufferedReader getBufferedReader(final String filename) throws UncheckedIOException {
  15. return getBufferedReader(filename, StandardCharsets.UTF_8);
  16. }

代码示例来源:origin: matsim-org/matsim

  1. private void loadConfig() {
  2. StringBuilder buffer = new StringBuilder(1024);
  3. try (BufferedReader in = IOUtils.getBufferedReader(this.configFile.getAbsolutePath())) {
  4. String line;
  5. while ((line = in.readLine()) != null) {
  6. buffer.append(line);
  7. buffer.append(IOUtils.NATIVE_NEWLINE);
  8. }
  9. } catch (IOException e) {
  10. e.printStackTrace();
  11. }
  12. this.xmlPane.setText(buffer.toString());
  13. this.xmlPane.setCaretPosition(0);
  14. }

代码示例来源:origin: matsim-org/matsim

  1. /**
  2. * Parses the specified file. The file can be gzip-compressed and is decompressed on-the-fly while parsing. A gzip-compressed
  3. * file must have the ending ".gz" to be correctly recognized. The passed filename may or may not contain the ending ".gz". If
  4. * no uncompressed file is found with the specified name, the ending ".gz" will be added to the filename and a compressed file
  5. * will be searched for and read if found.
  6. *
  7. * @param filename The filename of the file to read, optionally ending with ".gz" to force reading a gzip-compressed file.
  8. * @throws UncheckedIOException
  9. */
  10. @Override
  11. public final void readFile(final String filename) throws UncheckedIOException {
  12. log.info("starting to parse xml from file " + filename + " ...");
  13. this.theSource = filename;
  14. parse(new InputSource(IOUtils.getBufferedReader(filename)));
  15. }

代码示例来源:origin: matsim-org/matsim

  1. BufferedReader br = IOUtils.getBufferedReader(fileName);
  2. br.readLine();

代码示例来源:origin: matsim-org/matsim

  1. public static HbefaRoadTypeMapping createVisumRoadTypeMapping(URL filename){
  2. logger.info("entering createRoadTypeMapping ...") ;
  3. VisumHbefaRoadTypeMapping mapping = new VisumHbefaRoadTypeMapping();
  4. try{
  5. BufferedReader br = IOUtils.getBufferedReader(filename);
  6. String strLine = br.readLine();
  7. Map<String, Integer> indexFromKey = EmissionUtils.createIndexFromKey(strLine);
  8. while ((strLine = br.readLine()) != null){
  9. if ( strLine.contains("\"")) throw new RuntimeException("cannot handle this character in parsing") ;
  10. String[] inputArray = strLine.split(";");
  11. String visumRtNr = inputArray[indexFromKey.get("VISUM_RT_NR")];
  12. String hbefaRtName = (inputArray[indexFromKey.get("HBEFA_RT_NAME")]);
  13. mapping.put(visumRtNr, hbefaRtName);
  14. }
  15. } catch (IOException e) {
  16. e.printStackTrace();
  17. }
  18. logger.info("leaving createRoadTypeMapping ...") ;
  19. return mapping;
  20. }
  21. }

代码示例来源:origin: matsim-org/matsim

  1. private double getVolume(final String filename) throws IOException {
  2. BufferedReader reader = IOUtils.getBufferedReader(filename);
  3. reader.readLine(); // header
  4. String line = reader.readLine(); // link 100
  5. if (line == null) {
  6. return Double.NaN; // should never happen...
  7. }
  8. String[] parts = line.split("\t");// [0] = linkId, [1] = Count Station Id, [2] = matsim volume, [3] = real volume, [4] = normalized relative error
  9. return Double.parseDouble(parts[2]);
  10. }

代码示例来源:origin: matsim-org/matsim

  1. BufferedReader reader = IOUtils.getBufferedReader(this.getPath());

代码示例来源:origin: matsim-org/matsim

  1. private final void connectByFile(final ActivityFacilities facilities, final Network network, final String file, final Set<Id<ActivityFacility>> remainingFacilities) {
  2. log.info(" connecting facilities with links via "+CONFIG_F2L_INPUTF2LFile+"="+file);
  3. try (BufferedReader br = IOUtils.getBufferedReader(file)) {
  4. int lineCnt = 0;
  5. String currLine;
  6. br.readLine(); lineCnt++; // Skip header
  7. while ((currLine = br.readLine()) != null) {
  8. String[] entries = currLine.split("\t", -1);
  9. // fid lid
  10. // 0 1
  11. Id<ActivityFacility> fid = Id.create(entries[0].trim(), ActivityFacility.class);
  12. Id<Link> lid = Id.create(entries[1].trim(), Link.class);
  13. ActivityFacility f = facilities.getFacilities().get(fid);
  14. Link l = network.getLinks().get(lid);
  15. if ((f != null) && (l != null)) {
  16. l = network.getLinks().get(l.getId());
  17. mapFacilityToLink(f,l);
  18. remainingFacilities.remove(f.getId());
  19. }
  20. else { log.warn(lineCnt+": at least one of the two locations not found."); }
  21. lineCnt++;
  22. }
  23. } catch (IOException e) {
  24. throw new RuntimeException("Error while reading given inputF2LFile='"+file+"'.", e);
  25. }
  26. log.info(" number of facilities that are still not connected to a link = "+remainingFacilities.size());
  27. log.info(" done. (connecting facilities with links via "+CONFIG_F2L_INPUTF2LFile+"="+file+")");
  28. }

代码示例来源:origin: matsim-org/matsim

  1. private double[] getVolumes(final String filename) throws IOException {
  2. BufferedReader reader = IOUtils.getBufferedReader(filename);
  3. reader.readLine(); // header
  4. String line = reader.readLine(); // link 100
  5. if (line == null) {
  6. // should never happen...
  7. return new double[] {Double.NaN, Double.NaN, Double.NaN};
  8. }
  9. String[] parts = line.split("\t");// [0] = linkId, [1] = matsim volume, [2] = real volume
  10. return new double[] {
  11. Double.parseDouble(parts[7]), // min
  12. Double.parseDouble(parts[8]), // avg
  13. Double.parseDouble(parts[9]) // max
  14. };
  15. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedReader_UTFwithBOM() throws IOException {
  3. String filename = utils.getOutputDirectory() + "test.txt";
  4. FileOutputStream out = new FileOutputStream(filename);
  5. out.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
  6. out.write("ABCdef".getBytes());
  7. out.close();
  8. {
  9. BufferedReader in = IOUtils.getBufferedReader(filename);
  10. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  11. in.close();
  12. }
  13. {
  14. BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_UTF8);
  15. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  16. in.close();
  17. }
  18. {
  19. BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_WINDOWS_ISO88591);
  20. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  21. in.close();
  22. }
  23. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedReader_UTFwithoutBOM() throws IOException {
  3. String filename = utils.getOutputDirectory() + "test.txt";
  4. FileOutputStream out = new FileOutputStream(filename);
  5. out.write("ABCdef".getBytes());
  6. out.close();
  7. {
  8. BufferedReader in = IOUtils.getBufferedReader(filename);
  9. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  10. in.close();
  11. }
  12. {
  13. BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_UTF8);
  14. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  15. in.close();
  16. }
  17. {
  18. BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_WINDOWS_ISO88591);
  19. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  20. in.close();
  21. }
  22. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedReader_UTFwithBOM_lz4() throws IOException {
  3. String filename = utils.getOutputDirectory() + "test.txt.lz4";
  4. OutputStream out = IOUtils.getOutputStream(filename);
  5. out.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
  6. out.write("ABCdef".getBytes());
  7. out.close();
  8. {
  9. BufferedReader in = IOUtils.getBufferedReader(filename);
  10. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  11. in.close();
  12. }
  13. {
  14. BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_UTF8);
  15. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  16. in.close();
  17. }
  18. {
  19. BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_WINDOWS_ISO88591);
  20. Assert.assertEquals("ABCdef", new String(new byte[] { (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read(), (byte) in.read() }));
  21. in.close();
  22. }
  23. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedWriter_overwrite() throws IOException {
  3. String filename = this.utils.getOutputDirectory() + "test.txt";
  4. BufferedWriter writer = IOUtils.getBufferedWriter(filename);
  5. writer.write("aaa");
  6. writer.close();
  7. BufferedWriter writer2 = IOUtils.getBufferedWriter(filename);
  8. writer2.write("bbb");
  9. writer2.close();
  10. BufferedReader reader = IOUtils.getBufferedReader(filename);
  11. String line = reader.readLine();
  12. Assert.assertEquals("bbb", line);
  13. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedWriter_append() throws IOException {
  3. String filename = this.utils.getOutputDirectory() + "test.txt";
  4. BufferedWriter writer = IOUtils.getAppendingBufferedWriter(filename);
  5. writer.write("aaa");
  6. writer.close();
  7. BufferedWriter writer2 = IOUtils.getAppendingBufferedWriter(filename);
  8. writer2.write("bbb");
  9. writer2.close();
  10. BufferedReader reader = IOUtils.getBufferedReader(filename);
  11. String line = reader.readLine();
  12. Assert.assertEquals("aaabbb", line);
  13. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedWriter_overwrite_gzipped() throws IOException {
  3. String filename = this.utils.getOutputDirectory() + "test.txt.gz";
  4. BufferedWriter writer = IOUtils.getBufferedWriter(filename);
  5. writer.write("aaa");
  6. writer.close();
  7. BufferedWriter writer2 = IOUtils.getBufferedWriter(filename);
  8. writer2.write("bbb");
  9. writer2.close();
  10. BufferedReader reader = IOUtils.getBufferedReader(filename);
  11. String line = reader.readLine();
  12. Assert.assertEquals("bbb", line);
  13. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedReader_encodingMacRoman() throws IOException {
  3. String filename = this.utils.getClassInputDirectory() + "textsample_MacRoman.txt";
  4. BufferedReader reader = IOUtils.getBufferedReader(filename, Charset.forName("MacRoman"));
  5. String line = reader.readLine();
  6. Assert.assertNotNull(line);
  7. Assert.assertEquals("äöüÉç", line);
  8. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedReader_encodingIsoLatin1() throws IOException {
  3. String filename = this.utils.getClassInputDirectory() + "textsample_IsoLatin1.txt";
  4. BufferedReader reader = IOUtils.getBufferedReader(filename, Charset.forName("ISO-8859-1"));
  5. String line = reader.readLine();
  6. Assert.assertNotNull(line);
  7. Assert.assertEquals("äöüÉç", line);
  8. }

代码示例来源:origin: matsim-org/matsim

  1. @Test
  2. public void testGetBufferedReader_encodingUTF8() throws IOException {
  3. String filename = this.utils.getClassInputDirectory() + "textsample_UTF8.txt";
  4. BufferedReader reader = IOUtils.getBufferedReader(filename);
  5. String line = reader.readLine();
  6. Assert.assertNotNull(line);
  7. Assert.assertEquals("äöüÉç", line);
  8. }

代码示例来源:origin: matsim-org/matsim

  1. public static List<String[]> readFile(String file, char separator) {
  2. try (CSVReader reader = new CSVReaderBuilder(IOUtils.getBufferedReader(file))
  3. .withCSVParser(new CSVParserBuilder().withSeparator(separator).build()).build()) {
  4. return reader.readAll();
  5. } catch (IOException e) {
  6. throw new UncheckedIOException(e);
  7. }
  8. }
  9. }

相关文章