下面列出了怎么用com.google.common.io.Files的API类实例代码及写法,或者点击链接到github查看源代码。
@BeforeClass
public static void setup() throws IOException {
fs = LocalFileSystem.getInstance();
testDirectory = new Path(Files.createTempDir().getAbsolutePath());
FileSystemDatasetRepository repo = new FileSystemDatasetRepository(fs.getConf(),
testDirectory);
Dataset<MyRecord> writerDataset = repo.create("ns", "test", new DatasetDescriptor.Builder()
.schema(MyRecord.class)
.build(), MyRecord.class);
DatasetWriter<MyRecord> writer = writerDataset.newWriter();
for (int i = 0; i < totalRecords; i++) {
writer.write(new MyRecord(String.valueOf(i), i));
}
writer.close();
readerDataset = repo.load("ns", "test", TestGenericRecord.class);
}
@Test
public void testUnknownOverrideBuildSystemNoBuilders() throws Exception {
CDepYml config = new CDepYml();
System.out.printf(new Yaml().dump(config));
File yaml = new File(".test-files/testUnknownOverrideBuildSystem/cdep.yml");
deleteDirectory(yaml.getParentFile());
yaml.getParentFile().mkdirs();
Files.write("dependencies:\n"
+ "- compile: com.github.jomof:sqlite:3.16.2-rev45\n",
yaml, StandardCharsets.UTF_8);
File modulesFolder = new File(yaml.getParentFile(), "my-modules");
try {
main("-wf", yaml.getParent(),
"-gmf", modulesFolder.getAbsolutePath(),
"--builder", "unknown-build-system");
fail("Expected failure");
} catch (CDepRuntimeException e) {
assertThat(e).hasMessage("Builder unknown-build-system is not recognized.");
}
}
public static void main(String[] args) {
File file = new File(SystemConfig.getHomePath()+ "/conf","ruledata" );
if(file.exists()&&file.isDirectory())
{
File[] binFiles=file.listFiles();
for (File binFile : binFiles) {
String path= ZKUtils.getZKBasePath()+"ruledata/"+binFile.getName();
CuratorFramework zk= ZKUtils.getConnection();
try {
zk.create().creatingParentsIfNeeded().forPath(path) ;
zk.setData().forPath(path, Files.toByteArray(binFile)) ;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
protected void bindConf(File confDir) {
final File[] files = confDir.listFiles();
if (files != null) {
for (File file : files) {
if (!file.isDirectory()) {
if ("properties".equals(Files.getFileExtension(file.getName()))) {
Properties properties = new Properties();
try (Reader reader = Files.newReader(file, Charset.forName("UTF-8"))) {
properties.load(reader);
} catch (IOException e) {
throw new IllegalStateException(
format("Unable to read configuration file %s", file), e);
}
bindProperties(properties);
}
}
}
}
}
/**
* Chooses a directory name, based on a JAR file name, considering exploded-aar and classes.jar.
*/
public static String getDirectoryNameForJar(File inputFile) {
// add a hash of the original file path.
HashFunction hashFunction = Hashing.sha1();
HashCode hashCode = hashFunction.hashString(inputFile.getAbsolutePath(), Charsets.UTF_16LE);
String name = Files.getNameWithoutExtension(inputFile.getName());
if (name.equals("classes") && inputFile.getAbsolutePath().contains("exploded-aar")) {
// This naming scheme is coming from DependencyManager#computeArtifactPath.
File versionDir = inputFile.getParentFile().getParentFile();
File artifactDir = versionDir.getParentFile();
File groupDir = artifactDir.getParentFile();
name = Joiner.on('-').join(
groupDir.getName(), artifactDir.getName(), versionDir.getName());
}
name = name + "_" + hashCode.toString();
return name;
}
@Test
public void download() throws Exception {
CDepYml config = new CDepYml();
File yaml = new File(".test-files/download/cdep.yml");
yaml.getParentFile().mkdirs();
Files.write("builders: [cmake, cmakeExamples]\n" +
"dependencies:\n" +
"- compile: com.github.jomof:low-level-statistics:0.0.16\n", yaml,
StandardCharsets.UTF_8);
// Download first.
main("-wf", yaml.getParent());
// Redownload
String result = main("download", "-wf", yaml.getParent());
System.out.printf(result);
assertThat(result).doesNotContain("Redownload");
assertThat(result).contains("Generating");
}
public static <T> void saveData(Path filePath, DataFactory<T> factory, Collection<T> values) throws IOException {
try (BufferedWriter writer = Files.newWriter(filePath.toFile(), StandardCharsets.UTF_8)) {
for (T value : values) {
final String[] fields = factory.serialize(value);
if (fields == null) {
continue;
}
StringBuilder sb = new StringBuilder();
for (String field : fields) {
if (field == null) {
sb.append("\t");
} else {
sb.append("\t").append(field);
}
}
writer.write(sb.substring(1));
writer.write("\n");
}
}
}
private Map<String, Set<String>> readConfigurationAliases() {
URL aliasesResource = getClass().getClassLoader().getResource(PROPERTIES_ALIASES_CONFIG_FILE);
Map<String, Set<String>> aliases = new HashMap<>();
if (aliasesResource != null) {
Properties properties = new Properties();
File aliasesFile = new File(aliasesResource.getFile());
try (Reader reader = Files.newReader(aliasesFile, Charset.forName("UTF-8"))) {
properties.load(reader);
} catch (IOException e) {
throw new IllegalStateException(
format("Unable to read configuration aliases from file %s", aliasesFile), e);
}
for (Map.Entry<Object, Object> entry : properties.entrySet()) {
String value = (String) entry.getValue();
aliases.put(
(String) entry.getKey(),
Splitter.on(',').splitToList(value).stream().map(String::trim).collect(toSet()));
}
}
return aliases;
}
/**
* Checks whether the component database was correctly initialized.
*/
public void testComponentDatabase() throws IOException {
// Load component descriptor file
String componentDescriptorSource = Files.toString(
new File(TestUtils.APP_INVENTOR_ROOT_DIR + COMPONENT_DESCRIPTOR_FILE),
Charset.forName("UTF8"));
// Parse the data file and check the existence of some key components
final ComponentDatabase componentDatabase = new ComponentDatabase(
new ServerJsonParser().parse(componentDescriptorSource).asArray());
Set<String> components = componentDatabase.getComponentNames();
assertTrue(components.contains("Button"));
assertTrue(components.contains("Label"));
assertTrue(components.contains("TextBox"));
// Check some properties defined for the TextBox component
List<PropertyDefinition> properties = componentDatabase.getPropertyDefinitions("TextBox");
assertEquals("boolean", find(properties, "Enabled").getEditorType());
assertEquals("non_negative_float", find(properties, "FontSize").getEditorType());
assertEquals("string", find(properties, "Hint").getEditorType());
}
/**
* 读取 mapFile文件的信息
* 方法描述
* @param name 名称信息
* @return
* @创建日期 2016年9月18日
*/
private void writeMapFile(String name, String value) {
// 加载数据
String path = RuleszkToxmlLoader.class.getClassLoader().getResource(ZookeeperPath.ZK_LOCAL_WRITE_PATH.getKey())
.getPath();
checkNotNull(path, "write Map file curr Path :" + path + " is null! must is not null");
path = new File(path).getPath() + File.separator;
path += name;
// 进行数据写入
try {
Files.write(value.getBytes(), new File(path));
} catch (IOException e1) {
e1.printStackTrace();
}
}
@Test
public void testFileRoundTrip() throws Exception {
Map<String,TypeMetadata> map = prepareTypeMetadataMap();
final File tempDir = Files.createTempDir();
tempDir.deleteOnExit();
final File tempFile = new File(tempDir, "type_metadata_roundtrip_test");
FileOutputStream fos = new FileOutputStream(tempFile);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(map);
oos.close();
FileInputStream fis = new FileInputStream(tempFile);
ObjectInputStream ois = new ObjectInputStream(fis);
Object got = ois.readObject();
Assert.assertEquals(got, map);
}
/**
* Creates a new {@link ReadOnlyDocument} for the given file.
*
* @param file the file whose text will be stored in the document. UTF-8 charset is used to
* decode the contents of the file.
* @throws IOException if an error occurs while reading the file.
*/
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
ReadOnlyDocument(@NonNull File file) throws IOException {
String xml = Files.toString(file, Charsets.UTF_8);
if (xml.startsWith("\uFEFF")) { // Strip byte order mark if necessary
xml = xml.substring(1);
}
mFileContents = xml;
myFile = file;
myLastModified = file.lastModified();
myOffsets = Lists.newArrayListWithExpectedSize(mFileContents.length() / 30);
for (int i = 0; i < mFileContents.length(); i++) {
char c = mFileContents.charAt(i);
if (c == '\n') {
myOffsets.add(i + 1);
}
}
}
@Test
public void testFullyQualifiedUserDefinedImport() throws CompileException, IOException {
File dir = Files.createTempDir();
dir.deleteOnExit();
File f1 = new File(dir, "bagpathimport");
File f2 = createFile();
write(f1, "package edu.brown.cs;\nbag MyBag1 {bool test = 0;}");
write(f2, "import \"%s\";\nbag MyBag2 {edu.brown.cs.MyBag1 test = 0;}", f1.getName());
List<String> inputFiles = Lists.newArrayList(f2.getAbsolutePath());
List<String> bagPath = Lists.newArrayList(dir.getAbsolutePath());
Linker.link(inputFiles, bagPath);
}
@BeforeClass
public static void createSourceDataset() throws Exception {
repoUri = "hdfs://" + getDFS().getUri().getAuthority() + "/tmp/data";
TestUtil.run("delete", source, "-r", repoUri, "-d", "target/data");
String csv = "/tmp/users.csv";
BufferedWriter writer = Files.newWriter(
new File(csv), CSVSchemaCommand.SCHEMA_CHARSET);
writer.append("id,username,email\n");
writer.append("1,test,[email protected]\n");
writer.append("2,user,[email protected]\n");
writer.append("3,user3,[email protected]\n");
writer.append("4,user4,[email protected]\n");
writer.append("5,user5,[email protected]\n");
writer.append("6,user6,[email protected]\n");
writer.close();
TestUtil.run("-v", "csv-schema", csv, "-o", avsc, "--class", "User",
"--require", "id");
TestUtil.run("create", source, "-s", avsc,
"-r", repoUri, "-d", "target/data");
TestUtil.run("csv-import", csv, source, "-r", repoUri, "-d", "target/data");
}
@Override
public void persist(Date date, PerformanceTestDescriptor options, PerformanceTestResult result) {
try {
String dateStr = new SimpleDateFormat(Time.DATE_FORMAT_PREFERRED).format(date);
dir.mkdirs();
File file = new File(dir, "auto-test-results.txt");
file.createNewFile();
Files.append("date="+dateStr+"; test="+options+"; result="+result+"\n", file, Charsets.UTF_8);
File summaryFile = new File(dir, "auto-test-summary.txt");
summaryFile.createNewFile();
Files.append(
dateStr
+"\t"+options.summary
+"\t"+roundToSignificantFigures(result.ratePerSecond, 6)
+"\t"+result.duration
+(result.cpuTotalFraction != null ? "\t"+"cpu="+roundToSignificantFigures(result.cpuTotalFraction, 3) : "")
+"\n",
summaryFile, Charsets.UTF_8);
} catch (IOException e) {
LOG.warn("Failed to persist performance results to "+dir+" (continuing)", e);
}
}
@Before
public void setup() throws Exception {
URL url = getClass().getResource("/product-type-patterns.xml");
configFile = new File(url.toURI());
extractor = new ProdTypePatternMetExtractor();
extractor.setConfigFile(configFile);
tmpDir = Files.createTempDir();
book1 = new File(tmpDir, "book-1234567890.txt");
book2 = new File(tmpDir, "book-0987654321.txt");
page1 = new File(tmpDir, "page-1234567890-111.txt");
page2 = new File(tmpDir, "page-0987654321-222.txt");
Files.touch(book1);
Files.touch(book2);
Files.touch(page1);
Files.touch(page2);
url = getClass().getResource("/product-type-patterns-2.xml");
configFile2 = new File(url.toURI());
page1a = new File(tmpDir, "page-111-1234567890.txt");
page2a = new File(tmpDir, "page-222-0987654321.txt");
Files.touch(page1a);
Files.touch(page2a);
}
private void testPathInUri(boolean userDirisRoot) throws Exception {
String originPath =
currentThread().getContextClassLoader().getResource("remote-download-source/parseNoError").getPath();
File originDirFile = new File(originPath).listFiles()[0];
File tempDir = testFolder.newFolder();
File copied = new File(tempDir, originDirFile.getName());
Files.copy(originDirFile, copied);
setupServer(testFolder.getRoot().getAbsolutePath(), true);
String pathInUri = userDirisRoot ? tempDir.getName() : tempDir.getAbsolutePath();
RemoteDownloadSource origin = new TestRemoteDownloadSourceBuilder(scheme, port)
.withRemoteHost(scheme.name() + "://localhost:" + port + "/" + pathInUri)
.withUserDirIsRoot(userDirisRoot)
.build();
SourceRunner runner = new SourceRunner.Builder(RemoteDownloadDSource.class, origin)
.addOutputLane("lane")
.build();
runner.runInit();
StageRunner.Output op = runner.runProduce(RemoteDownloadSource.NOTHING_READ, 1000);
List<Record> expected = getExpectedRecords();
List<Record> actual = op.getRecords().get("lane");
Assert.assertEquals(expected.size(), actual.size());
for (int i = 0; i < 2; i++) {
Assert.assertEquals(expected.get(i).get(), actual.get(i).get());
}
destroyAndValidate(runner);
}
@Test
public void executeQueryFromFile() throws IOException {
HiveShell shell = createHiveCliShell();
shell.start();
String statement = "select current_database(), NULL, 100";
when(container.executeStatement(statement)).thenReturn(Arrays.<Object[]> asList( new Object[] {"default", null, 100}));
String hiveSql = statement + ";";
File file = tempFolder.newFile("script.sql");
Files.write(hiveSql, file, UTF_8);
List<String> results = shell.executeQuery(UTF_8, Paths.get(file.toURI()), "xxx", "yyy");
assertThat(results.size(), is(1));
assertThat(results.get(0), is("defaultxxxyyyxxx100"));
}
/** Force the pom file of a project to be "simple".
*
* @param projectDir the folder in which the pom file is located.
* @param monitor the progress monitor.
* @throws IOException if the pom file cannot be changed.
*/
static void forceSimplePom(File projectDir, IProgressMonitor monitor) throws IOException {
final File pomFile = new File(projectDir, POM_FILE);
if (pomFile.exists()) {
final SubMonitor submon = SubMonitor.convert(monitor, 4);
final File savedPomFile = new File(projectDir, POM_BACKUP_FILE);
if (savedPomFile.exists()) {
savedPomFile.delete();
}
submon.worked(1);
Files.copy(pomFile, savedPomFile);
submon.worked(1);
final StringBuilder content = new StringBuilder();
try (BufferedReader stream = new BufferedReader(new FileReader(pomFile))) {
String line = stream.readLine();
while (line != null) {
line = line.replaceAll("<extensions>\\s*true\\s*</extensions>", ""); //$NON-NLS-1$ //$NON-NLS-2$
content.append(line).append("\n"); //$NON-NLS-1$
line = stream.readLine();
}
}
submon.worked(1);
Files.write(content.toString().getBytes(), pomFile);
submon.worked(1);
}
}
private void doAddFile(File file, String archivePath) throws IZipEntryFilter.ZipAbortException,
IOException {
// If a file has to be merged, write it to a file in the merging folder and add it later.
if (mMergeFiles.keySet().contains(archivePath)) {
File mergingFile = mMergeFiles.get(archivePath);
Files.createParentDirs(mergingFile);
FileOutputStream fos = new FileOutputStream(mMergeFiles.get(archivePath), true);
try {
fos.write(Files.toByteArray(file));
} finally {
fos.close();
}
} else {
if (!mNoBinaryZipFilter.checkEntry(archivePath)) {
return;
}
mAddedFiles.put(archivePath, file);
mBuilder.writeFile(file, archivePath);
}
}
private Properties getProperties()
throws IOException {
Properties jobProperties =
GobblinLocalJobLauncherUtils.getJobProperties("runtime_test/writer_output_format_test.properties");
URL resource = getClass().getClassLoader().getResource("runtime_test/" + SAMPLE_FILE);
Assert.assertNotNull(resource, "Sample file should be present");
File sampleFile = new File(resource.getFile());
File testFile = File.createTempFile("writerTest", ".avro");
FileUtils.copyFile(sampleFile, testFile);
jobProperties.setProperty(ConfigurationKeys.SOURCE_FILEBASED_FILES_TO_PULL,
testFile.getAbsolutePath());
String outputRootDirectory = Files.createTempDir().getAbsolutePath() + "/";
jobProperties.setProperty(ConfigurationKeys.STATE_STORE_ROOT_DIR_KEY, outputRootDirectory + "state_store");
jobProperties.setProperty(ConfigurationKeys.WRITER_STAGING_DIR, outputRootDirectory + "writer_staging");
jobProperties.setProperty(ConfigurationKeys.WRITER_OUTPUT_DIR, outputRootDirectory + "writer_output");
jobProperties.setProperty(ConfigurationKeys.DATA_PUBLISHER_FINAL_DIR, outputRootDirectory + "final_dir");
return jobProperties;
}
@Before
public void startUp() throws IOException {
indexDir = Files.createTempDir().getAbsoluteFile();
System.setProperty(ZeppelinConfiguration.ConfVars.ZEPPELIN_SEARCH_INDEX_PATH.getVarName(), indexDir.getAbsolutePath());
noteSearchService = new LuceneSearch(ZeppelinConfiguration.create());
interpreterSettingManager = mock(InterpreterSettingManager.class);
InterpreterSetting defaultInterpreterSetting = mock(InterpreterSetting.class);
when(defaultInterpreterSetting.getName()).thenReturn("test");
when(interpreterSettingManager.getDefaultInterpreterSetting()).thenReturn(defaultInterpreterSetting);
notebook = new Notebook(ZeppelinConfiguration.create(), mock(AuthorizationService.class), mock(NotebookRepo.class), mock(NoteManager.class),
mock(InterpreterFactory.class), interpreterSettingManager,
noteSearchService,
mock(Credentials.class), null);
}
private static Map<String, String> readMetadata(File metadataPath, ObjectMapper mapper) throws IOException {
if (metadataPath.exists()) {
Map<String, String> ret = Maps.newLinkedHashMap();
JsonNode node = mapper.readTree(Files.toString(metadataPath, Charsets.UTF_8));
Iterator<Map.Entry<String,JsonNode>> iterator = node.getFields();
while (iterator.hasNext()) {
Map.Entry<String, JsonNode> entry = iterator.next();
ret.put(entry.getKey(), entry.getValue().getTextValue());
}
return ret;
}
return null;
}
private void initSnapshotState(String tableName, File snapshotCacheFolder) {
String snapshotID = snapshotCacheFolder.getName();
File stateFile = getCacheStateFile(snapshotCacheFolder.getAbsolutePath());
if (stateFile.exists()) {
try {
String stateStr = Files.toString(stateFile, Charsets.UTF_8);
String resourcePath = ExtTableSnapshotInfo.getResourcePath(tableName, snapshotID);
if (CacheState.AVAILABLE.name().equals(stateStr)) {
tablesCache.put(resourcePath, new CachedTableInfo(snapshotCacheFolder.getAbsolutePath()));
}
} catch (IOException e) {
logger.error("error to read state file:" + stateFile.getAbsolutePath());
}
}
}
@Before
public void prepareResources() throws IOException {
tempDir = Files.createTempDir();
configurationFile = extractResource(Resources.DEMO_CONFIGURATION, ".brm", tempDir);
chromedriverFile = extractResource(CHROMEDRIVER, tempDir);
if (!chromedriverFile.setExecutable(true)) {
throw new IllegalStateException("Cannot set chrome driver file to executable");
}
virtualScreenProcess = new UbuntuVirtualScreenProcessCreator().createXvfbProcess(screen);
caseFile = extractResource(Resources.DYNAMIC_TEST_CASE, tempDir);
}
@BeforeClass
public static void setup() throws AccumuloException, AccumuloSecurityException, IOException, InterruptedException {
if (!IS_MOCK) {
tempAccumuloDir = Files.createTempDir();
accumulo = MiniAccumuloClusterFactory.newAccumuloCluster(
new MiniAccumuloConfigImpl(tempAccumuloDir, ACCUMULO_PASSWORD),
GeoWaveIndexerTest.class);
accumulo.start();
}
}
/** Restore the original pom file.
*
* @param projectDir the folder in which the pom file is located.
* @param monitor the progress monitor.
* @throws IOException if the pom file cannot be changed.
*/
static void restorePom(File projectDir, IProgressMonitor monitor) throws IOException {
final File pomFile = new File(projectDir, POM_FILE);
final File savedPomFile = new File(projectDir, POM_BACKUP_FILE);
if (savedPomFile.exists()) {
if (pomFile.exists()) {
pomFile.delete();
}
Files.copy(savedPomFile, pomFile);
savedPomFile.delete();
}
monitor.worked(1);
}
@Provides
@SubprocessLogDir
public File provideSubprocessLogFile(@SpongeOutputDirectory String outputDir) throws IOException {
File logdir = new File(outputDir, "broker_logs");
if (!logdir.exists()) {
Files.createParentDirs(logdir);
logdir.mkdir();
}
return logdir;
}
private ArtifactFilesystem renameFileToSHA1Naming(final String tenant, final File file,
final AbstractDbArtifact artifact) throws IOException {
final File fileSHA1Naming = getFile(tenant, artifact.getHashes().getSha1());
if (fileSHA1Naming.exists()) {
FileUtils.deleteQuietly(file);
} else {
Files.move(file, fileSHA1Naming);
}
return new ArtifactFilesystem(fileSHA1Naming, artifact.getArtifactId(), artifact.getHashes(),
artifact.getSize(), artifact.getContentType());
}
@Override
protected void setUp() throws Exception {
fs = new NicerLocalFileSystem();
temp1 = File.createTempFile("sqar-test", "");
temp2 = fileToPath(Files.createTempDir());
temp3 = Files.createTempDir();
temp4 = Files.createTempDir();
}