Java Code Examples for com.google.common.io.Files#readLines()

The following examples show how to use com.google.common.io.Files#readLines() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileProxys.java    From gecco with MIT License 6 votes vote down vote up
public FileProxys() {
	try {
		proxys = new ConcurrentHashMap<String, Proxy>();
		proxyQueue = new ConcurrentLinkedQueue<Proxy>();
		URL url = Resources.getResource("proxys");
		File file = new File(url.getPath());
		List<String> lines = Files.readLines(file, Charsets.UTF_8);
		if(lines.size() > 0) {
			for(String line : lines) {
				line = line.trim();
				if(line.startsWith("#")) {
					continue;
				}
				String[] hostPort = line.split(":");
				if(hostPort.length == 2) {
					String host = hostPort[0];
					int port = NumberUtils.toInt(hostPort[1], 80);
					addProxy(host, port);
				}
			}
		}
	} catch(Exception ex) {
		log.info("proxys not load");
	}
}
 
Example 2
Source File: XmlCombinerTest.java    From xml-combiner with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldSupportReadingAndStoringFiles() throws IOException, ParserConfigurationException, SAXException,
		TransformerException {
	// given
	Path input = Paths.get("target/test.in");
	Path output = Paths.get("target/test.out");
	Files.write("<config/>", input.toFile(), StandardCharsets.UTF_8);

	// when
	XmlCombiner combiner = new XmlCombiner();
	combiner.combine(input);
	combiner.buildDocument(output);
	List<String> lines = Files.readLines(output.toFile(), StandardCharsets.UTF_8);

	// then
	assertThat(lines).hasSize(1);
	assertThat(lines.iterator().next()).contains("<config/>");
}
 
Example 3
Source File: DslStepsDumperTest.java    From bromium with MIT License 6 votes vote down vote up
@Test
public void correctlyHandlesNoAliases() throws IOException {
    Map<String, String> firstStep = new HashMap<>();
    firstStep.put(EVENT, clickLoginButtonActionName);

    TestScenarioSteps testScenarioSteps = new TestScenarioSteps();
    testScenarioSteps.add(firstStep);

    Map<String, ApplicationActionConfiguration> actionConfigurationMap = createNoAliasesMockConfiguration();

    StepsDumper stepsDumper = new DslStepsDumper(actionConfigurationMap);
    stepsDumper.dump(testScenarioSteps, outputFilename);

    List<String> lines = Files.readLines(outputFile, Charsets.UTF_8);
    assertEquals(1, lines.size());
    assertEquals("clickLoginButtonActionName: Click login button ", lines.get(0));
}
 
Example 4
Source File: ResourceUtilsTest.java    From brooklyn-server with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteStreamToTempFile() throws Exception {
    File tempFileLocal = Os.writeToTempFile(new ByteArrayInputStream("mycontents".getBytes()), "resourceutils-test", ".txt");
    try {
        List<String> lines = Files.readLines(tempFileLocal, Charsets.UTF_8);
        assertEquals(lines, ImmutableList.of("mycontents"));
    } finally {
        tempFileLocal.delete();
    }
}
 
Example 5
Source File: ReadTextFile.java    From levelup-java-examples with Apache License 2.0 5 votes vote down vote up
@Test
public void read_text_file_with_google_guava() {

	String filePath = this.getClass().getClassLoader()
			.getResource(FILE_PATH).getFile();

	List<String> lines = null;
	try {
		lines = Files.readLines(new File(filePath), Charsets.UTF_8);
	} catch (IOException e) {
		logger.error(e);
	}
	assertTrue(lines.size() == 28);
}
 
Example 6
Source File: JacocoCoverageRunner.java    From bazel with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an immutable list containing all the file paths found in mainFile. It uses the
 * javaRunfilesRoot prefix for every found file to compute its absolute path.
 */
private static ImmutableList<File> getFilesFromFileList(File mainFile, String javaRunfilesRoot)
    throws IOException {
  List<String> metadataFiles = Files.readLines(mainFile, UTF_8);
  ImmutableList.Builder<File> convertedMetadataFiles = new Builder<>();
  for (String metadataFile : metadataFiles) {
    convertedMetadataFiles.add(new File(javaRunfilesRoot + "/" + metadataFile));
  }
  return convertedMetadataFiles.build();
}
 
Example 7
Source File: Project.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
private void extractAospMinSdkVersion() {
    // Is the SDK level specified by a Makefile?
    boolean found = false;
    File makefile = new File(mDir, "Android.mk"); //$NON-NLS-1$
    if (makefile.exists()) {
        try {
            List<String> lines = Files.readLines(makefile, Charsets.UTF_8);
            Pattern p = Pattern.compile("LOCAL_SDK_VERSION\\s*:=\\s*(.*)"); //$NON-NLS-1$
            for (String line : lines) {
                line = line.trim();
                Matcher matcher = p.matcher(line);
                if (matcher.matches()) {
                    found = true;
                    String version = matcher.group(1);
                    if (version.equals("current")) { //$NON-NLS-1$
                        mManifestMinSdk = findCurrentAospVersion();
                    } else {
                        mManifestMinSdk = SdkVersionInfo.getVersion(version,
                                mClient.getTargets());
                    }
                    break;
                }
            }
        } catch (IOException ioe) {
            mClient.log(ioe, null);
        }
    }

    if (!found) {
        mManifestMinSdk = findCurrentAospVersion();
    }
}
 
Example 8
Source File: DefaultProcReader.java    From Dolphin with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listProcesses(File cgroupFile) throws IOException {
    File cgroupProcsFile = new File(cgroupFile, CGROUP_PROCS);
    if (!cgroupProcsFile.exists()) {
        throw new FileNotFoundException(cgroupProcsFile.getAbsolutePath() + "is not exist!");
    }

    return Files.readLines(cgroupProcsFile, Charset.defaultCharset());
}
 
Example 9
Source File: EnforceDemo.java    From metanome-algorithms with Apache License 2.0 5 votes vote down vote up
private Collection<Collection<ColumnMatchWithThreshold<?>>> getRules() {
	try {
		List<String> mds = Files.readLines(mdFile, CHARSET);
		return StreamUtils.seq(mds)
			.map(MDParser::parseMd)
			.toList();
	} catch (IOException e) {
		log.error("Error reading MDs", e);
		return Collections.emptyList();
	}
}
 
Example 10
Source File: BasicService.java    From Kylin with Apache License 2.0 5 votes vote down vote up
public DataSource getOLAPDataSource(String project) {

        project = ProjectInstance.getNormalizedProjectName(project);

        DataSource ret = olapDataSources.get(project);
        if (ret == null) {
            logger.debug("Creating a new data source");
            logger.debug("OLAP data source pointing to " + getConfig());

            File modelJson = OLAPSchemaFactory.createTempOLAPJson(project, getConfig());

            try {
                List<String> text = Files.readLines(modelJson, Charset.defaultCharset());
                logger.debug("The new temp olap json is :");
                for (String line : text)
                    logger.debug(line);
            } catch (IOException e) {
                e.printStackTrace(); // logging failure is not critical
            }

            DriverManagerDataSource ds = new DriverManagerDataSource();
            Properties props = new Properties();
            props.setProperty(OLAPQuery.PROP_SCAN_THRESHOLD, String.valueOf(KylinConfig.getInstanceFromEnv().getScanThreshold()));
            ds.setConnectionProperties(props);
            ds.setDriverClassName("net.hydromatic.optiq.jdbc.Driver");
            ds.setUrl("jdbc:calcite:model=" + modelJson.getAbsolutePath());

            ret = olapDataSources.putIfAbsent(project, ds);
            if (ret == null) {
                ret = ds;
            }
        }
        return ret;
    }
 
Example 11
Source File: CreateMainDexList.java    From javaide with GNU General Public License v3.0 5 votes vote down vote up
@TaskAction
public void output() throws IOException, ProcessException {
    if (getAllClassesJarFile() == null) {
        throw new NullPointerException("No input file");
    }


    // manifest components plus immediate dependencies must be in the main dex.
    File _allClassesJarFile = getAllClassesJarFile();
    Set<String> mainDexClasses = callDx(_allClassesJarFile, getComponentsJarFile());

    // add additional classes specified via a jar file.
    File _includeInMainDexJarFile = getIncludeInMainDexJarFile();
    if (_includeInMainDexJarFile != null) {
        // proguard shrinking is overly aggressive when it comes to removing
        // interface classes: even if an interface is implemented by a concrete
        // class, if no code actually references the interface class directly
        // (i.e., code always references the concrete class), proguard will
        // remove the interface class when shrinking.  This is problematic,
        // as the runtime verifier still needs the interface class to be
        // present, or the concrete class won't be valid.  Use a
        // ClassReferenceListBuilder here (only) to pull in these missing
        // interface classes.  Note that doing so brings in other unnecessary
        // stuff, too; next time we're low on main dex space, revisit this!
        mainDexClasses.addAll(callDx(_allClassesJarFile, _includeInMainDexJarFile));
    }


    if (mainDexListFile != null) {
        Set<String> mainDexList = new HashSet<String>(Files.readLines(mainDexListFile, Charsets.UTF_8));
        mainDexClasses.addAll(mainDexList);
    }


    String fileContent = Joiner.on(System.getProperty("line.separator")).join(mainDexClasses);

    Files.write(fileContent, getOutputFile(), Charsets.UTF_8);
}
 
Example 12
Source File: TintEvaluation.java    From tint with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
    try {
        final CommandLine cmd = CommandLine
                .parser()
                .withName("./evaluate-lemma")
                .withHeader("Calculate lemma evaluation for Tint")
                .withOption("t", "guessed", "Input file", "FILE",
                        CommandLine.Type.FILE_EXISTING, true, false, true)
                .withOption("g", "gold-standard", "Input gold standard file", "FILE",
                        CommandLine.Type.FILE, true, false, true)
                .withLogger(LoggerFactory.getLogger("eu.fbk")).parse(args);

        File guessed = cmd.getOptionValue("guessed", File.class);
        File gold = cmd.getOptionValue("gold-standard", File.class);

        List<String> guesses = Files.readLines(guessed, Charsets.UTF_8);
        List<String> trueLabels = Files.readLines(gold, Charsets.UTF_8);

        int total = 0;
        int correct = 0;

        for (int i = 0; i < guesses.size(); i++) {
            String guess = guesses.get(i);
            String goldLabel = trueLabels.get(i);

            if (goldLabel.length() == 0) {
                continue;
            }

            String[] parts;

            parts = goldLabel.split("\t");
            goldLabel = parts[1];
            String pos = parts[2];

            boolean doIt = false;
            if (pos.startsWith("V")) {
                doIt = true;
            } else if (pos.startsWith("S")) {
                doIt = true;
            } else if (pos.startsWith("A")) {
                doIt = true;
            } else if (pos.startsWith("B")) {
                doIt = true;
            }

            if (goldLabel.equals("_")) {
                doIt = false;
            }

            if (!doIt) {
                continue;
            }
            total++;

            parts = guess.split("\t");
            guess = parts[2];

            if (guess.equalsIgnoreCase(goldLabel)) {
                correct++;
            }
            else {
                System.out.printf("%s -> %s\n", guess, goldLabel);
            }

        }

        System.out.println(correct);
        System.out.println(total);
        System.out.println(correct * 1.0 / total);
    } catch (Exception e) {
        CommandLine.fail(e);
    }
}
 
Example 13
Source File: TextProEvaluation.java    From tint with GNU General Public License v3.0 4 votes vote down vote up
public static void main(String[] args) {
    try {
        final CommandLine cmd = CommandLine
                .parser()
                .withName("./evaluate-lemma")
                .withHeader("Calculate lemma evaluation for TextPro")
                .withOption("t", "guessed", "Input file", "FILE",
                        CommandLine.Type.FILE_EXISTING, true, false, true)
                .withOption("g", "gold-standard", "Input gold standard file", "FILE",
                        CommandLine.Type.FILE, true, false, true)
                .withLogger(LoggerFactory.getLogger("eu.fbk")).parse(args);

        File guessed = cmd.getOptionValue("guessed", File.class);
        File gold = cmd.getOptionValue("gold-standard", File.class);

        List<String> guesses = Files.readLines(guessed, Charsets.UTF_8);
        List<String> trueLabels = Files.readLines(gold, Charsets.UTF_8);

        int total = 0;
        int correct = 0;

        for (int i = 0; i < trueLabels.size(); i++) {
            String goldLabel = trueLabels.get(i);

            if (goldLabel.length() == 0) {
                continue;
            }

            String guess = guesses.get(i + 4); // TextPro output file has 4 starting lines
            String[] parts;

            parts = goldLabel.split("\t");
            goldLabel = parts[1];
            String pos = parts[2];

            boolean doIt = false;
            if (pos.startsWith("V")) {
                doIt = true;
            } else if (pos.startsWith("S")) {
                doIt = true;
            } else if (pos.startsWith("A")) {
                doIt = true;
            } else if (pos.startsWith("B")) {
                doIt = true;
            }

            if (goldLabel.equals("_")) {
                doIt = false;
            }

            if (!doIt) {
                continue;
            }
            total++;

            parts = guess.split("\t");
            guess = parts[2];
            guess = guess.replaceAll("\\s+.*", "");

            if (guess.equalsIgnoreCase(goldLabel)) {
                correct++;
            } else {
                System.out.printf("%s -> %s\n", guess, goldLabel);
            }

        }

        System.out.println(correct);
        System.out.println(total);
        System.out.println(correct * 1.0 / total);
    } catch (Exception e) {
        CommandLine.fail(e);
    }
}
 
Example 14
Source File: FileUtil.java    From j360-dubbo-app-all with Apache License 2.0 4 votes vote down vote up
/**
 * 读取文件的每行内容到List<String>
 */
public static List<String> toLines(final File file) throws IOException {
	return Files.readLines(file, Charsets.UTF_8);
}
 
Example 15
Source File: Main.java    From simhash-java with MIT License 4 votes vote down vote up
private static List<String> readDocs(String[] args) throws IOException {
	return Files.readLines(new File(args[0]), Charsets.UTF_8);
}
 
Example 16
Source File: ResourceClassGenerator.java    From NBANDROID-V2 with Apache License 2.0 4 votes vote down vote up
public static Map<ResourceType, Map<String, Object>> buildFullResourceMap(ResourceNamespace namespace, String fqcn, File projectR, ResourceClassGeneratorConfig config) {
    final Map<Integer, Integer> originalToGenerated = new HashMap<>();
    final Map<ResourceType, Map<String, Object>> resources = Maps.newHashMap();
    try {
        List<String> readLines = Files.readLines(projectR, StandardCharsets.UTF_8);
        for (String line : readLines) {
            StringTokenizer tok = new StringTokenizer(line, " ", false);
            if (tok.countTokens() > 3) {
                switch (tok.nextToken()) {
                    case "int":
                        handleFullLine(namespace, tok, resources, originalToGenerated, config);
                        break;
                    case "int[]":
                        handleFullLineArray(tok, line, resources);
                        break;
                }
            }
        }
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }
    //refresh array ids to new IDs
    for (Map.Entry<ResourceType, Map<String, Object>> entry : resources.entrySet()) {
        ResourceType type = entry.getKey();
        Map<String, Object> values = entry.getValue();
        for (Map.Entry<String, Object> entry1 : values.entrySet()) {
            String name = entry1.getKey();
            Object value = entry1.getValue();
            if (value instanceof List) {
                List<Integer> in = (List<Integer>) value;
                List<Integer> out = new ArrayList<>();
                for (Integer val : in) {
                    Integer newVal = originalToGenerated.get(val);
                    if (newVal == null) {
                        out.add(val);
                    } else {
                        out.add(newVal);
                    }
                }
                values.replace(name, out);
            }
        }
    }
    return resources;
}
 
Example 17
Source File: DebugHarnessTest.java    From tasmo with Apache License 2.0 4 votes vote down vote up
@Test(invocationCount = 1_000, singleThreaded = false, skipFailedInvocations = true, enabled = false)
    public void hackTest(TasmoMaterializerHarness t) throws Exception {
//        LogManager.getLogger("com.jivesoftware.os.tasmo").setLevel(Level.TRACE);
//        LogManager.getLogger("com.jivesoftware.os.tasmo.lib.concur.ConcurrencyAndExistanceCommitChange").setLevel(Level.TRACE);
//        LogManager.getLogger("com.jivesoftware.os.tasmo.reference.lib.ReferenceStore").setLevel(Level.TRACE);
//        LogManager.getLogger("com.jivesoftware.os.tasmo.view.reader.service.writer.WriteToViewValueStore").setLevel(Level.TRACE);

        ArrayNode rawViews = mapper.readValue(new File("/home/jonathan/jive/os/all_views.json"), ArrayNode.class);
        final JsonEventConventions eventConventions = new JsonEventConventions();
        Views views = TasmoModelFactory.modelToViews(rawViews);
        t.initModel(views);

        List<String> stringEvents = Files.readLines(new File("/home/jonathan/jive/os/events.txt"), Charset.defaultCharset());
        final Set<Id> instanceIds = new HashSet<>();
//        ExecutorService executorService = Executors.newFixedThreadPool(20);
        for (final String stringEvent : stringEvents) {
//            executorService.submit(new Runnable() {
//                public void run() {
            try {
                ObjectNode eventObjectNode = mapper.readValue(stringEvent, ObjectNode.class);
                //System.out.println("eventObjectNode:"+eventObjectNode);
                ObjectId instanceId = eventConventions.getInstanceObjectId(eventObjectNode);
                t.write(new Event(eventObjectNode, instanceId));
                if (instanceId.isClassName("UserFollowActivity")) {
                    instanceIds.add(instanceId.getId());
                }
            } catch (Exception e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
//                }
//            });
        }
        //Thread.sleep(5000);
        System.out.println("Checking " + instanceIds.size() + " views");
        boolean failed = false;
        for (Id commentInstanceId : instanceIds) {
            ObjectId viewObjectId = new ObjectId("UserFollowActivityView", commentInstanceId);
            ObjectNode view = t.readView(tenantId, actorId, viewObjectId, Id.NULL);
            System.err.println("View: " + commentInstanceId.toStringForm());
            System.err.println(mapper.writeValueAsString(view));
            if (view == null) {
                System.err.println("Failed to load view for " + viewObjectId);
            } else {
                //UserFollowActivityView.verbSubject.followedObject.instanceId
                if (!view.has("verbSubject")) {
                    JsonNode got = view.get("verbSubject");
                    if (!got.has("followedObject")) {
                        got = got.get("followedObject");
                        if (!got.has("instanceId")) {
                            System.out.println("ERROR");
                        } else {
                            failed = true;
                        }
                    } else {
                        failed = true;
                    }
                } else {
                    failed = true;
                }
            }
        }
        if (failed) {
            fail();
        }
    }
 
Example 18
Source File: CsvUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
private List<String> readFile(String filename) throws IOException {
    return Files.readLines(new File(filename), Charset.forName("utf-8"));
}
 
Example 19
Source File: IoUtils.java    From karamel with Apache License 2.0 4 votes vote down vote up
public static List<String> readLinesFromPath(String url) throws IOException {
  return Files.readLines(new File(url), Charsets.UTF_8);
}
 
Example 20
Source File: CsvRelationEvalationConsumerTest.java    From baleen with Apache License 2.0 4 votes vote down vote up
@Test
public void testCsv()
    throws AnalysisEngineProcessException, ResourceInitializationException, IOException {

  final File file = File.createTempFile("test", "relations");
  file.deleteOnExit();

  final String text = "John went to London.";
  jCas.setDocumentText(text);

  final Sentence s = new Sentence(jCas);
  s.setBegin(0);
  s.setEnd(text.length());
  s.addToIndexes();

  final Person p = new Person(jCas);
  p.setBegin(text.indexOf("John"));
  p.setEnd(p.getBegin() + "John".length());
  p.setValue("John");
  p.addToIndexes();

  final Location l = new Location(jCas);
  l.setBegin(text.indexOf("London"));
  l.setEnd(l.getBegin() + "London".length());
  l.setValue("London");
  l.addToIndexes();

  final Relation r = new Relation(jCas);
  r.setBegin(text.indexOf("went"));
  r.setEnd(r.getBegin() + "went".length());
  r.setValue("went");
  r.setRelationshipType("MOVEMENT");
  r.setRelationSubType("went");
  r.setSource(p);
  r.setTarget(l);
  r.addToIndexes();

  processJCas("filename", file.getAbsolutePath());

  final List<String> lines = Files.readLines(file, StandardCharsets.UTF_8);

  assertEquals(2, lines.size());

  // Header
  assertTrue(lines.get(0).contains("source"));
  // Relation
  assertTrue(lines.get(1).contains(",\"John went to London.\","));
  assertTrue(lines.get(1).contains(",\"John\","));
  assertTrue(lines.get(1).contains(",\"London\","));
  assertTrue(lines.get(1).contains(",\"MOVEMENT\","));
  assertTrue(lines.get(1).contains(",\"went\","));

  file.delete();
}