下面列出了org.apache.commons.io.IOUtils#readLines ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Import the already generated cdkey into system.
*
* @param fileName
* @return
*/
public int importCDKey(String fileName) throws IOException {
int count = 0;
File file = new File(fileName);
HashMap<String, String> cdkeyMap = new HashMap<String, String>();
if ( file.exists() && file.isFile() ) {
List<String> lines = IOUtils.readLines(new FileReader(file));
for ( String line : lines ) {
String[] cdkeys = line.split(Constant.COMMA);
if ( cdkeys.length == 2 ) {
String cdkey = cdkeys[0];
String pojoId = cdkeys[1];
cdkeyMap.put(cdkey, pojoId);
count++;
logger.info("Import cdKey:"+cdkey+", pojoId:"+pojoId);
}
}
if ( cdkeyMap.size() > 0 ) {
Jedis jedis = JedisFactory.getJedisDB();
jedis.hmset(REDIS_CDKEY, cdkeyMap);
}
} else {
logger.warn("CDKey File cannot be read:{}", file.getAbsolutePath());
}
return count;
}
/**
* {@inheritDoc}.
*/
@Override
public void displayPreview(String contentType, ByteArrayInputStream in) {
try {
try {
StringBuilder sb = new StringBuilder();
for (String line : IOUtils.readLines(in)) {
sb.append(line);
sb.append(System.getProperty("line.separator"));
}
area.setText(sb.toString());
} finally {
in.close();
}
} catch (IOException e) {
area.setText("");
logger.error("Error occurred while previewing text", e);
}
}
/**
* Loads the VAPID keypair from the file, or generate them if they don't exist.
*/
private void loadVAPIDKeys() {
try {
List<String> encodedKeys = IOUtils.readLines(
new FileInputStream(ConfigConstants.getUserDataFolder() + File.separator + VAPID_KEYS_FILE_NAME));
this.publicVAPIDKey = encodedKeys.get(0);
this.privateVAPIDKey = encodedKeys.get(1);
} catch (IOException e) {
try {
generateVAPIDKeyPair();
} catch (InvalidAlgorithmParameterException | NoSuchProviderException | NoSuchAlgorithmException
| IOException e1) {
RuntimeException ex = new RuntimeException("Cannot get the VAPID keypair for push notifications");
ex.initCause(e1);
throw ex;
}
}
}
private static EcoMappings<OWLClass> loadEcoMappings(Reader mappingsReader, OWLGraphWrapper eco) throws IOException {
EcoMappings<OWLClass> mappings = new EcoMappings<OWLClass>();
List<String> lines = IOUtils.readLines(mappingsReader);
for (String line : lines) {
line = StringUtils.trimToNull(line);
if (line != null) {
char c = line.charAt(0);
if ('#' != c) {
String[] split = StringUtils.split(line, '\t');
if (split.length == 3) {
String code = split[0];
String ref = split[1];
String ecoId = split[2];
OWLClass cls = eco.getOWLClassByIdentifier(ecoId);
if (cls != null) {
mappings.add(code, ref, cls);
}
}
}
}
}
return mappings;
}
/**
* Tests that activities matching core-ex* can be parsed by apache streams.
*
* @throws Exception test exception
*/
@Test
public void testCoreSerDe() throws Exception {
InputStream testActivityFolderStream = ExamplesSerDeIT.class.getClassLoader()
.getResourceAsStream("w3c/activitystreams-master/test");
List<String> files = IOUtils.readLines(testActivityFolderStream, StandardCharsets.UTF_8);
for (String file : files) {
if ( !file.startsWith(".") && file.contains("core-ex") ) {
LOGGER.info("File: activitystreams-master/test/" + file);
String testFileString = new String(Files.readAllBytes(Paths.get("target/test-classes/w3c/activitystreams-master/test/" + file)));
LOGGER.info("Content: " + testFileString);
ObjectNode testFileObjectNode = MAPPER.readValue(testFileString, ObjectNode.class);
LOGGER.info("Object:" + testFileObjectNode);
}
}
}
protected List<String> extractFilesFromList(String output, List<String> fileList, int entries) {
// Just in case, since NPE"s are a drag
output = ObjectUtils.defaultIfNull(output, "");
// If there's content add it to the file list
if (StringUtils.isNotEmpty(output.trim())) {
try {
List<String> lines = IOUtils.readLines(new StringReader(output));
int sz = lines.size();
for (int i = 0; i < sz; i++) {
fileList.add(lines.get(i));
if(i == entries){
break;
}
}
} catch (Exception e) {
logger.error( "Error getting directory listing.", e);
}
}
return fileList;
}
@Test
public void testHasQuotaList() throws IOException {
HttpGet get = new HttpGet("http://localhost:4567/filter?set=dirs&filters=hasQuota:eq:true");
HttpResponse res = client.execute(hostPort, get);
List<String> result = IOUtils.readLines(res.getEntity().getContent());
System.out.println(result);
assertThat(result.size(), is(not(0)));
assertThat(res.getStatusLine().getStatusCode(), is(200));
}
/**
* Write content of the record in a human readable form.
*
* @param writer where the data is written to
* @param lineSeparator line separator for multi line values
* @throws IOException if the record can't be written
*/
public void dump(Writer writer, String lineSeparator) throws IOException {
for (int i = 0; i < this.getRecordLenth(); i++) {
StringBuilder txt = new StringBuilder();
try (StringReader reader = new StringReader(StringUtils.trimToEmpty(this.get(i)))) {
for (String line : IOUtils.readLines(reader)) {
if (txt.length() > 0) txt.append(lineSeparator);
txt.append(line);
}
}
writer.write(i + ":" + txt.toString());
writer.write(System.lineSeparator());
}
}
@Test
public void testAverageSpacePerBlock() throws IOException {
HttpGet get =
new HttpGet(
"http://localhost:4567/divide?set1=files&sum1=fileSize&set2=files&sum2=numBlocks");
HttpResponse res = client.execute(hostPort, get);
List<String> strings = IOUtils.readLines(res.getEntity().getContent());
assertThat(strings.size(), is(1));
String average = strings.get(0);
System.out.println(average);
long v = Long.parseLong(average);
assertThat(v, is(not(0L)));
assertThat(res.getStatusLine().getStatusCode(), is(200));
}
@PostConstruct
public void setup() {
try {
byte[] graphBytes = FileUtils.readFileToByteArray(new File(modelConfig.getModelPath()));
init(graphBytes, modelConfig.getInputLayerName(), modelConfig.getOutputLayerName());
List<String> labelsList = IOUtils.readLines(modelConfig.getLabelsResource().getInputStream(), "UTF-8");
labels = new ArrayList<>(labelsList);
log.info("Initialized Tensorflow model ({}MB)", graphBytes.length / BYTES_IN_MB);
} catch (IOException e) {
log.error("Error during classifier initialization:", e);
}
}
private static void chopOff(File f, String annoPtn) throws IOException {
System.out.println("Processing " + f);
FileInputStream is = new FileInputStream(f);
List<String> lines = IOUtils.readLines(is, "UTF-8");
is.close();
List<String> outLines = new ArrayList<>(lines.size());
boolean del = false;
for (String l : lines) {
if (l.startsWith(" @") && l.contains(annoPtn))
del = true;
if (del)
System.out.println("x " + l);
else
outLines.add(l);
if (del && l.startsWith(" }"))
del = false;
}
if (!dryRun && outLines.size() < lines.size()) {
FileOutputStream os = new FileOutputStream(f);
IOUtils.writeLines(outLines, "\n", os, "UTF-8");
os.close();
System.out.println("UPDATED " + f);
} else {
System.out.println("skipped");
}
System.out.println("============================================================================");
}
@Override
public void open(Map map,
TopologyContext context,
SpoutOutputCollector outputCollector) {
this.outputCollector = outputCollector;
try {
commits = IOUtils.readLines(ClassLoader.getSystemResourceAsStream("changelog.txt"),
Charset.defaultCharset().name());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Extract lines that start an rdf element so basic assertions can be made.
*/
private static List<String> rdfOpenTags(String s) throws IOException {
String withoutSpaces = Pattern.compile("^\\s+", Pattern.MULTILINE).matcher(s).replaceAll("");
List<String> rdfLines = new ArrayList<>();
for (String l : IOUtils.readLines(new StringReader(withoutSpaces))) {
if (l.startsWith("<rdf:")) {
rdfLines.add(l.replaceAll(" .*", ""));
}
}
return rdfLines;
}
@Test
public void testFindAvgFileSizeUserHistogramCSV() throws IOException {
HttpGet get =
new HttpGet(
"http://localhost:4567/histogram?set=files&type=user&find=max:fileSize&histogramOutput=csv");
HttpResponse res = client.execute(hostPort, get);
List<String> text = IOUtils.readLines(res.getEntity().getContent());
assertThat(text.size(), is(1));
assertThat(text.get(0).split(",").length, is(2));
assertThat(res.getStatusLine().getStatusCode(), is(200));
}
@Test
public void testStorageTypeHistogram() throws IOException {
HttpGet get = new HttpGet("http://localhost:4567/histogram?set=files&type=storageType");
HttpResponse res = client.execute(hostPort, get);
List<String> strings = IOUtils.readLines(res.getEntity().getContent());
if (!String.join(" ", strings).contains("not supported")) {
strings.clear();
assertThat(res.getStatusLine().getStatusCode(), is(200));
}
}
@SuppressWarnings("unchecked")
public static List<String> getStopWords() {
try {
if(stopWords == null)
stopWords = IOUtils.readLines(new ClassPathResource("/stopwords").getInputStream());
} catch (IOException e) {
throw new RuntimeException(e);
}
return stopWords;
}
public static WhiteListBasedUriKBClassifier create(InputStream stream) {
try {
return new WhiteListBasedUriKBClassifier(IOUtils.readLines(stream));
} catch (IOException e) {
LOGGER.error("Exception while trying to read knowledge base namespaces.", e);
return null;
}
}
private List<String> execute(String... args) throws IOException {
String[] command;
if (runAsArgs != null) {
command = new String[runAsArgs.length + args.length];
System.arraycopy(runAsArgs, 0, command, 0, runAsArgs.length);
System.arraycopy(args, 0, command, runAsArgs.length, args.length);
} else {
command = args;
}
Process process = new ProcessBuilder(command).start();
return IOUtils.readLines(process.getInputStream(), Charset.defaultCharset());
}
protected String formatPlain(String content) {
// TODO do a proper algorithm
try {
int maxLine = 0;
List<String> linesIn = IOUtils.readLines(new StringReader(content));
for (int i = 0; i < linesIn.size(); i++) {
if (linesIn.get(i).startsWith(">")) {
maxLine = i - (StringUtils.isBlank(linesIn.get(i - 1)) ? 2 : 1);
break;
}
}
List<String> linesOut = new ArrayList<>();
boolean prevLineBlank = false;
for (int i = 0; i < maxLine; i++) {
String line = StringUtils.trimToEmpty(linesIn.get(i));
if (StringUtils.isBlank(line)) {
if (prevLineBlank) {
continue;
}
prevLineBlank = true;
} else {
prevLineBlank = false;
}
linesOut.add(line);
}
if (prevLineBlank) {
linesOut.remove(linesOut.size() - 1);
}
return StringUtils.join(linesOut, System.lineSeparator());
} catch (IOException e) {
// This should never happen, we can't deal with it here
throw new RuntimeException(e);
}
}
protected List<String> execute(String... args) throws IOException {
Process process = new ProcessBuilder(args).start();
@SuppressWarnings("unchecked")
List<String> lines = IOUtils.readLines(process.getInputStream());
return lines;
}