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

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

本文整理了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

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

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

/**
 * 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).
 *
 * @param filename The file to read, may contain the ending ".gz" to force reading a compressed file.
 * @return BufferedReader for the specified file.
 * @throws UncheckedIOException
 *
 * <br> author mrieser
 */
public static BufferedReader getBufferedReader(final String filename) throws UncheckedIOException {
  return getBufferedReader(filename, StandardCharsets.UTF_8);
}

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

private void loadConfig() {
  StringBuilder buffer = new StringBuilder(1024);
  try (BufferedReader in = IOUtils.getBufferedReader(this.configFile.getAbsolutePath())) {
    String line;
    while ((line = in.readLine()) != null) {
      buffer.append(line);
      buffer.append(IOUtils.NATIVE_NEWLINE);
    }
  } catch (IOException e) {
    e.printStackTrace();
  }
  this.xmlPane.setText(buffer.toString());
  this.xmlPane.setCaretPosition(0);
}

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

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

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

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

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

public static HbefaRoadTypeMapping createVisumRoadTypeMapping(URL filename){
    logger.info("entering createRoadTypeMapping ...") ;

    VisumHbefaRoadTypeMapping mapping = new VisumHbefaRoadTypeMapping();
    try{
      BufferedReader br = IOUtils.getBufferedReader(filename);
      String strLine = br.readLine();
      Map<String, Integer> indexFromKey = EmissionUtils.createIndexFromKey(strLine);

      while ((strLine = br.readLine()) != null){
        if ( strLine.contains("\"")) throw new RuntimeException("cannot handle this character in parsing") ;

        String[] inputArray = strLine.split(";");
        String visumRtNr = inputArray[indexFromKey.get("VISUM_RT_NR")];
        String hbefaRtName = (inputArray[indexFromKey.get("HBEFA_RT_NAME")]);

        mapping.put(visumRtNr, hbefaRtName);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
    logger.info("leaving createRoadTypeMapping ...") ;
    return mapping;
  }
}

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

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

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

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

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

private final void connectByFile(final ActivityFacilities facilities, final Network network, final String file, final Set<Id<ActivityFacility>> remainingFacilities) {
  log.info("    connecting facilities with links via "+CONFIG_F2L_INPUTF2LFile+"="+file);
  try (BufferedReader br = IOUtils.getBufferedReader(file)) {
    int lineCnt = 0;
    String currLine;
    br.readLine(); lineCnt++; // Skip header
    while ((currLine = br.readLine()) != null) {
      String[] entries = currLine.split("\t", -1);
      // fid  lid
      // 0    1
      Id<ActivityFacility> fid = Id.create(entries[0].trim(), ActivityFacility.class);
      Id<Link> lid = Id.create(entries[1].trim(), Link.class);
      ActivityFacility f = facilities.getFacilities().get(fid);
      Link l = network.getLinks().get(lid);
      if ((f != null) && (l != null)) {
        l = network.getLinks().get(l.getId());
        mapFacilityToLink(f,l);
        remainingFacilities.remove(f.getId());
      }
      else { log.warn(lineCnt+": at least one of the two locations not found."); }
      lineCnt++;
    }
  } catch (IOException e) {
    throw new RuntimeException("Error while reading given inputF2LFile='"+file+"'.", e);
  }
  log.info("      number of facilities that are still not connected to a link = "+remainingFacilities.size());
  log.info("    done. (connecting facilities with links via "+CONFIG_F2L_INPUTF2LFile+"="+file+")");
}

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

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

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

@Test
public void testGetBufferedReader_UTFwithBOM() throws IOException {
  String filename = utils.getOutputDirectory() + "test.txt";
  FileOutputStream out = new FileOutputStream(filename);
  out.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
  out.write("ABCdef".getBytes());
  out.close();
  
  {
    BufferedReader in = IOUtils.getBufferedReader(filename);
    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() }));
    in.close();
  }
  {
    BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_UTF8);
    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() }));
    in.close();
  }
  {
    BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_WINDOWS_ISO88591);
    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() }));
    in.close();
  }
}

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

@Test
public void testGetBufferedReader_UTFwithoutBOM() throws IOException {
  String filename = utils.getOutputDirectory() + "test.txt";
  FileOutputStream out = new FileOutputStream(filename);
  out.write("ABCdef".getBytes());
  out.close();
  
  {
    BufferedReader in = IOUtils.getBufferedReader(filename);
    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() }));
    in.close();
  }
  {
    BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_UTF8);
    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() }));
    in.close();
  }
  {
    BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_WINDOWS_ISO88591);
    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() }));
    in.close();
  }
}

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

@Test
public void testGetBufferedReader_UTFwithBOM_lz4() throws IOException {
  String filename = utils.getOutputDirectory() + "test.txt.lz4";
  OutputStream out = IOUtils.getOutputStream(filename);
  out.write(new byte[] {(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
  out.write("ABCdef".getBytes());
  out.close();
  
  {
    BufferedReader in = IOUtils.getBufferedReader(filename);
    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() }));
    in.close();
  }
  {
    BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_UTF8);
    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() }));
    in.close();
  }
  {
    BufferedReader in = IOUtils.getBufferedReader(filename, IOUtils.CHARSET_WINDOWS_ISO88591);
    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() }));
    in.close();
  }
}

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

@Test
public void testGetBufferedWriter_overwrite() throws IOException {
  String filename = this.utils.getOutputDirectory() + "test.txt";
  BufferedWriter writer = IOUtils.getBufferedWriter(filename);
  writer.write("aaa");
  writer.close();
  BufferedWriter writer2 = IOUtils.getBufferedWriter(filename);
  writer2.write("bbb");
  writer2.close();
  BufferedReader reader = IOUtils.getBufferedReader(filename);
  String line = reader.readLine();
  Assert.assertEquals("bbb", line);
}

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

@Test
public void testGetBufferedWriter_append() throws IOException {
  String filename = this.utils.getOutputDirectory() + "test.txt";
  BufferedWriter writer = IOUtils.getAppendingBufferedWriter(filename);
  writer.write("aaa");
  writer.close();
  BufferedWriter writer2 = IOUtils.getAppendingBufferedWriter(filename);
  writer2.write("bbb");
  writer2.close();
  BufferedReader reader = IOUtils.getBufferedReader(filename);
  String line = reader.readLine();
  Assert.assertEquals("aaabbb", line);
}

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

@Test
public void testGetBufferedWriter_overwrite_gzipped() throws IOException {
  String filename = this.utils.getOutputDirectory() + "test.txt.gz";
  BufferedWriter writer = IOUtils.getBufferedWriter(filename);
  writer.write("aaa");
  writer.close();
  BufferedWriter writer2 = IOUtils.getBufferedWriter(filename);
  writer2.write("bbb");
  writer2.close();
  BufferedReader reader = IOUtils.getBufferedReader(filename);
  String line = reader.readLine();
  Assert.assertEquals("bbb", line);
}

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

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

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

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

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

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

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

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

相关文章