本文整理了Java中net.roboconf.core.utils.Utils.readFileContent()
方法的一些代码示例,展示了Utils.readFileContent()
的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.readFileContent()
方法的具体详情如下:
包路径:net.roboconf.core.utils.Utils
类名称:Utils
方法名:readFileContent
[英]Reads a text file content and returns it as a string.
The file is tried to be read with UTF-8 encoding. If it fails, the default system encoding is used.
[中]读取文本文件内容并将其作为字符串返回。
尝试使用UTF-8编码读取该文件。如果失败,则使用默认的系统编码。
代码示例来源:origin: net.roboconf/roboconf-dm
@Override
public String getCommandInstructions( Application app, String commandName ) throws IOException {
File cmdFile = findCommandFile( app, commandName );
String result = cmdFile.exists() ? result = Utils.readFileContent( cmdFile ) : "";
return result;
}
代码示例来源:origin: net.roboconf/roboconf-doc-generator
/**
* Reads user-specified information from the project.
* @param applicationDirectory the application's directory
* @param prefix the prefix name
* @param suffix the file's suffix (see the DocConstants interface)
* @return the read information, as a string (never null)
* @throws IOException if the file could not be read
*/
private String readCustomInformation( File applicationDirectory, String prefix, String suffix )
throws IOException {
// Prepare the file name
StringBuilder sb = new StringBuilder();
sb.append( prefix );
if( this.locale != null )
sb.append( "_" + this.locale );
sb.append( suffix );
sb.insert( 0, "/" ); //$NON-NLS-1$
sb.insert( 0, DocConstants.DOC_DIR );
// Handle usual (doc) and Maven (src/main/doc) cases
File f = new File( applicationDirectory, sb.toString());
if( ! f.exists())
f = new File( f.getParentFile().getParentFile(), sb.toString());
String result = ""; //$NON-NLS-1$
if( f.exists())
result = Utils.readFileContent( f );
return result;
}
代码示例来源:origin: net.roboconf/roboconf-agent
/**
* Collect the main log files into a map.
* @param karafData the Karaf's data directory
* @return a non-null map
*/
public static Map<String,byte[]> collectLogs( String karafData ) throws IOException {
Map<String,byte[]> logFiles = new HashMap<>( 2 );
if( ! Utils.isEmptyOrWhitespaces( karafData )) {
String[] names = { "karaf.log", "roboconf.log" };
for( String name : names ) {
File log = new File( karafData, AgentConstants.KARAF_LOGS_DIRECTORY + "/" + name );
if( ! log.exists())
continue;
String content = Utils.readFileContent( log );
logFiles.put( name, content.getBytes( StandardCharsets.UTF_8 ));
}
}
return logFiles;
}
代码示例来源:origin: roboconf/roboconf-platform
/**
* Reads a text file content and returns it as a string.
* <p>
* The file is tried to be read with UTF-8 encoding.
* If it fails, the default system encoding is used.
* </p>
*
* @param file the file whose content must be loaded (can be null)
* @param logger a logger (not null)
* @return the file content or the empty string if an error occurred
*/
public static String readFileContentQuietly( File file, Logger logger ) {
String result = "";
try {
if( file != null && file.exists())
result = readFileContent( file );
} catch( Exception e ) {
logger.severe( "File " + file + " could not be read." );
logException( logger, e );
}
return result;
}
代码示例来源:origin: roboconf/roboconf-platform
/**
* Reads user-specified information from the project.
* @param applicationDirectory the application's directory
* @param prefix the prefix name
* @param suffix the file's suffix (see the DocConstants interface)
* @return the read information, as a string (never null)
* @throws IOException if the file could not be read
*/
private String readCustomInformation( File applicationDirectory, String prefix, String suffix )
throws IOException {
// Prepare the file name
StringBuilder sb = new StringBuilder();
sb.append( prefix );
if( this.locale != null )
sb.append( "_" + this.locale );
sb.append( suffix );
sb.insert( 0, "/" ); //$NON-NLS-1$
sb.insert( 0, DocConstants.DOC_DIR );
// Handle usual (doc) and Maven (src/main/doc) cases
File f = new File( applicationDirectory, sb.toString());
if( ! f.exists())
f = new File( f.getParentFile().getParentFile(), sb.toString());
String result = ""; //$NON-NLS-1$
if( f.exists())
result = Utils.readFileContent( f );
return result;
}
代码示例来源:origin: roboconf/roboconf-platform
/**
* Loads an application template's descriptor.
* @param f a file
* @return an application descriptor (not null)
* @throws IOException if the file could not be read
*/
public static ApplicationTemplateDescriptor load( File f ) throws IOException {
// Read the file's content
String fileContent = Utils.readFileContent( f );
Logger logger = Logger.getLogger( ApplicationTemplateDescriptor.class.getName());
Properties properties = Utils.readPropertiesQuietly( fileContent, logger );
if( properties.get( "fail.read" ) != null )
throw new IOException( "This is for test purpose..." );
ApplicationTemplateDescriptor result = load( properties );
// Resolve line numbers then
int lineNumber = 1;
for( String line : fileContent.split( "\n" )) {
for( String property : ALL_PROPERTIES ) {
if( line.trim().matches( "(" + LEGACY_PREFIX + ")?" + property + "\\s*[:=].*" )) {
result.propertyToLine.put( property, lineNumber );
break;
}
}
lineNumber ++;
}
return result;
}
代码示例来源:origin: roboconf/roboconf-platform
/**
* Parses the whole file and extracts instructions.
*/
private void parse() {
try {
// We assume these files are not that big.
String fileContent = Utils.readFileContent( this.context.getCommandFile());
parse( fileContent );
} catch( IOException e ) {
this.logger.severe( "A commands file could not be read. File path: " + this.context.getName());
}
}
代码示例来源:origin: net.roboconf/roboconf-agent
continue;
String content = Utils.readFileContent( source );
content = content.replace( "<domain>", domain );
content = content.replace( "<application-name>", applicationName );
代码示例来源:origin: net.roboconf/roboconf-dm
@Override
public String createTarget( File targetPropertiesFile, ApplicationTemplate creator ) throws IOException {
String fileContent = Utils.readFileContent( targetPropertiesFile );
代码示例来源:origin: roboconf/roboconf-platform
String s = Utils.readFileContent( ruleFile );
s = s.replaceAll( MULTILINE_COMMENT_PATTERN, "" );
s = s.replaceAll( SINGLE_COMMENT_PATTERN, "" );
代码示例来源:origin: roboconf/roboconf-platform
String content = Utils.readFileContent( commandFileToExecute );
Pattern p = Pattern.compile( PREFIX + "\\s+" + Pattern.quote( this.commandName ), Pattern.CASE_INSENSITIVE );
if( p.matcher( content ).find())
代码示例来源:origin: net.roboconf/roboconf-dm
String storedCreator = null;
if( createdByFile.exists())
storedCreator = Utils.readFileContent( createdByFile );
代码示例来源:origin: roboconf/roboconf-platform
/**
* Compares an assumed ZIP file with a content described in a map.
* @param rootDirectory the root directory of the unzipped content
* @param entryToContent the map associating entries and content (null for directories)
* @throws IOException
*/
public static void compareUnzippedContent( File rootDirectory, Map<String,String> entryToContent ) throws IOException {
for( Map.Entry<String,String> entry : entryToContent.entrySet()) {
File extractedFile = new File( rootDirectory, entry.getKey());
Assert.assertTrue( "Missing entry: " + entry.getKey(), extractedFile.exists());
if( entry.getValue() == null ) {
Assert.assertTrue( entry.getKey() + " was supposed to be a directory.", extractedFile.isDirectory());
continue;
}
Assert.assertTrue( entry.getKey() + " was supposed to be a file.", extractedFile.isFile());
String fileContent = Utils.readFileContent( extractedFile );
Assert.assertEquals( entry.getValue(), fileContent );
}
}
代码示例来源:origin: roboconf/roboconf-platform
@Test
public void testUpdateProperties() throws Exception {
File before = TestUtils.findTestFile( "/properties/before.properties" );
String beforeTxt = Utils.readFileContent( before );
File after = TestUtils.findTestFile( "/properties/after.properties" );
String afterTxt = Utils.readFileContent( after );
// One way
Map<String,String> keyToNewValue = new HashMap<> ();
keyToNewValue.put( "application-name", "app" );
keyToNewValue.put( "scoped-instance-path", "/vm" );
keyToNewValue.put( "parameters", "file:/something" );
keyToNewValue.put( "messaging-type", "http" );
String out = Utils.updateProperties( beforeTxt, keyToNewValue );
Assert.assertEquals( afterTxt, out );
// Other way
keyToNewValue.clear();
keyToNewValue.put( "application-name", "" );
keyToNewValue.put( "scoped-instance-path", "" );
keyToNewValue.put( "paraMeters", "" );
keyToNewValue.put( "messaging-type", "idle" );
out = Utils.updateProperties( afterTxt, keyToNewValue );
Assert.assertEquals( beforeTxt, out );
}
代码示例来源:origin: roboconf/roboconf-platform
File f = new File( url.toURI());
String content = Utils.readFileContent( f );
Matcher m = Pattern.compile( TranslationBundle.ROW_PATTERN ).matcher( content );
while( m.find()) {
代码示例来源:origin: roboconf/roboconf-platform
@Test
public void testAppendStringInto() throws Exception {
File f = this.folder.newFile();
Assert.assertTrue( f.delete());
String content = "whatever\n\thop ";
Assert.assertFalse( f.exists());
Utils.appendStringInto( content, f );
Assert.assertTrue( f.exists());
Assert.assertEquals( content, Utils.readFileContent( f ));
Utils.appendStringInto( "\npop", f );
Assert.assertEquals( content + "\npop", Utils.readFileContent( f ));
}
代码示例来源:origin: roboconf/roboconf-platform
/**
* @param f
*/
private static void testLoadingAndWritingOfValidFile( File f ) throws Exception {
// Preserving comments
FileDefinition rel = ParsingModelIo.readConfigurationFile( f, false );
Assert.assertTrue( f.getName() + ": parsing errors were found.", rel.getParsingErrors().isEmpty());
String fileContent = Utils.readFileContent( f );
fileContent = fileContent.replaceAll( "\r?\n", System.getProperty( "line.separator" ));
String s = ParsingModelIo.writeConfigurationFile( rel, true, null );
Assert.assertEquals( f.getName() + ": serialized model is different from the source.", fileContent, s );
// The same, but without writing comments
s = ParsingModelIo.writeConfigurationFile( rel, false, null );
Assert.assertFalse( f.getName() + ": serialized model should not contain a comment delimiter.", s.contains( ParsingConstants.COMMENT_DELIMITER ));
// Ignore comments at parsing time
rel = ParsingModelIo.readConfigurationFile( f, true );
Assert.assertTrue( f.getName() + ": parsing errors were found.", rel.getParsingErrors().isEmpty());
s = ParsingModelIo.writeConfigurationFile( rel, true, null );
Assert.assertFalse( f.getName() + ": serialized model should not contain a comment delimiter.", s.contains( ParsingConstants.COMMENT_DELIMITER ));
s = ParsingModelIo.writeConfigurationFile( rel, false, null );
Assert.assertFalse( f.getName() + ": serialized model should not contain a comment delimiter.", s.contains( ParsingConstants.COMMENT_DELIMITER ));
}
代码示例来源:origin: roboconf/roboconf-platform
@Test
public void testWriteStringInto() throws Exception {
File f = this.folder.newFile();
String content = "whatever\n\thop ";
Utils.writeStringInto( content, f );
Assert.assertEquals( content, Utils.readFileContent( f ));
}
代码示例来源:origin: roboconf/roboconf-platform
@Test
public void testParsingWithInvalidAutonomicRules() throws Exception {
// Copy the application and update a command file
File dir = TestUtils.findApplicationDirectory( "lamp" );
Assert.assertTrue( dir.isDirectory());
File newDir = this.folder.newFolder();
Utils.copyDirectory( dir, newDir );
File ruleFile = new File( newDir, Constants.PROJECT_DIR_RULES_AUTONOMIC + "/sample.drl" );
Assert.assertTrue( ruleFile.isFile());
File ruleFileCopy = new File( newDir, Constants.PROJECT_DIR_RULES_AUTONOMIC + "/sample.drl-invalid-extension" );
Utils.copyStream( ruleFile, ruleFileCopy );
String s = Utils.readFileContent( ruleFile );
s = s.replace( "scale", "inexisting-command" );
Utils.writeStringInto( s, ruleFile );
// Load it and verify it contains errors
ApplicationLoadResult alr = RuntimeModelIo.loadApplication( newDir );
List<RoboconfError> criticalErrors = new ArrayList<> ();
for( RoboconfError error : alr.getLoadErrors()) {
if( error.getErrorCode().getLevel() == ErrorLevel.SEVERE )
criticalErrors.add( error );
}
Assert.assertEquals( 2, criticalErrors.size());
Assert.assertEquals( ErrorCode.RULE_UNKNOWN_COMMAND, criticalErrors.get( 0 ).getErrorCode());
Assert.assertEquals( ErrorCode.PROJ_INVALID_RULE_EXT, criticalErrors.get( 1 ).getErrorCode());
}
代码示例来源:origin: roboconf/roboconf-platform
Assert.assertTrue( f.exists());
String fContent = Utils.readFileContent( f );
fContent = fContent.replaceAll( "(?i)application-version\\s*=.*", "application-version = @inval!d" );
Utils.writeStringInto( fContent, f );
内容来源于网络,如有侵权,请联系作者删除!