From what I know you can find the path of a file in the internal storage with java simply by using:
String path = context.getFilesDir().getAbsolutePath();
File file = new File(path + "/filename");
But how do I do this using kotlin?
In Kotlin, when you see a getter method from a Java class, you can omit the get, change the first letter to lower-case, and use it like a Kotlin property:
val path = context.filesDir.absolutePath
val file = File("$path/filename")
Related
So I have a program that turns a .txt file into a string to then send it via bluetooth to a printer, the problem is that right now I'm doing it using the file name but I wanted to do it only using the path of the file, this has to do with the fact that I need to search on the folder for any existing txt files and if there are any I need to print the first one and then delete it, so I can't be doing it by using the file's name. This is my code so far:
private fun readFile() String {
val file = File(storage/emulated/0/IS4-PDF-RDP/00233116695912019091310005913BLUETOOTH.txt)
var ins InputStream = file.inputStream()
read contents of IntputStream to String
var content = ins.readBytes().toString(Charset.defaultCharset())
return content
}
You can find the first file in the folder read it and then delete it as per your requirements
File("/storage/emulated/0/IS4-PDF-RDP/").walk().find {
it.extension == "txt"
}?.apply {
inputStream().readBytes().toString(Charset.defaultCharset())
delete()
}
Suppose I'm cleaning some html using HtmlCleaner (v2.18) and I want to set the property invalidAttributeNamePrefix (see section Cleaner parameters) to some value, i.e.: data-.
This way an attribute my-custom-attr="my-value" in the HTML will be transformed to data-my-custom-attr="my-value".
How can I do that? I wasn't able to find any example for the Java usage.
You can take as reference this piece of code:
HtmlCleaner cleaner = new HtmlCleaner();
CleanerProperties properties = cleaner.getProperties();
properties.setOmitComments(true);
// properties.setInvalidAttributeNamePrefix("data-"); there is no such method
// html is a declared variable which contains some html content
TagNode rootTagNode = cleaner.clean(html);
XmlSerializer xmlSerializer = new PrettyXmlSerializer(properties);
String cleanedHtml = xmlSerializer.getAsString(rootTagNode);
Upgrading to version 2.22 solves this.
Now it can be done
// ...
properties.setInvalidXmlAttributeNamePrefix("data-");
//...
I am trying to write a groovy script which loads the custom properties for a test suite using information from a properties file.
The properties file has around 6 different attributes
I have had a look at quite a few different methods i.e Loading from Properties test step and trying to expand the properties with groovy, but have not been successful.
If anyone could advise on how to achieve this, it would be much appreciated.
Thanks in advance.
Here is the groovy script which reads a property file and set them at test suite level:
def props = new Properties()
//replace the path with your file name below. use / instead of \ as path separator even on windows platform.
new File("/absolute/path/of/test.properties").withInputStream { s ->
props.load(s)
}
props.each {
context.testCase.testSuite.setPropertyValue(it.key, it.value)
}
The above script load test suite level for the current suite where the groovy script is present.
Unfortunately, in my case I want to have the properties in the same order as the input file, ie. sorted, and this methode does not work.
I wanted to load a 'Project properties' file containing sorted properties and each time I used this method it stored them unsorted.
I had to use a more straightforward method (see below). If anyone knows about a more elegant/practical way to do it, I'm interested
def filename = context.expand( '${#TestCase#filename}' )
def propertiesFile = new File(filename)
assert propertiesFile.exists(), "$filename does not exist"
project = testRunner.testCase.testSuite.project
//Remove properties
project.propertyNames.collect{project.removeProperty(it)}
//load the properties of external file
propertiesFile.eachLine {
line->
firstIndexOf = line.indexOf('=') // properties as set as key=value in the file
key = line.substring(0, firstIndexOf)
value = line.substring(firstIndexOf+1)
project.setPropertyValue(key, value)
}
I have a directory OUTPUT where I have the output files from a Map Reduce job. The output files are Text files written with a TextOutputFormat.
Now I want to read the key value pairs from the output file. How can I do so using some existing classes in hadoop. One way I could do it was as follows
FileSystem fs = FileSystem.get(conf);
FileStatus[] files = fs.globStatus(new Path(OUTPUT + "/part-*"));
for(FileStatus file:files){
if(file.getLen() > 0){
FSDataInputStream in = fs.open(file.getPath());
BufferedReader bin = new BufferedReader(new InputStreamReader(
in));
String s = bin.readLine();
while(s!=null){
System.out.println(s);
s = bin.readLine();
}
in.close();
}
}
This approach would work but increases my task to a great deal as I now need to manually parse the key value pairs out of each individual line. I am looking for something more handy that directly lets me read key and value in some variables.
Are you forced to use TextOutputFormat as your output format in the previous job?
If not then consider using SequenceFileOutputFormat, then you can use a SequenceFile.Reader to read back the file in Key / Value pairs. You can also still 'view' the file using hadoop fs -text path/to/output/part-r-00000
EDIT: You can also use the KeyValueLineRecordReader class, you'll just need to pass in a FileSplit to teh constructor.
Using log4net 1.2.11 on .net framework 3.5, this works:
var fileAppender = new log4net.Appender.FileAppender(layout, "check.log", true);
log4net.Config.BasicConfigurator.Configure(fileAppender);
log4net.LogManager.GetLogger(typeof(Program)).Info("constructor");
Except that it gives the warning:
Warning 1 'log4net.Appender.FileAppender.FileAppender(log4net.Layout.ILayout,
string, bool)' is obsolete: 'Instead use the default constructor and
set the Layout, File & AppendToFile properties
But if I use the properties it does not work
var fileAppender = new log4net.Appender.FileAppender()
{ Layout = layout, File = "check.log", AppendToFile = true };
log4net.Config.BasicConfigurator.Configure(fileAppender);
log4net.LogManager.GetLogger(typeof(Program)).Info("prop1");
That is, nothing gets written to the file. I have also tried a full path ("c:\check.log") and assigning to properties after creating the appender with default constructor.
What am I doing wrong?
I did not try it but looking at the log4net source code I can only assume that you need to call ActivateOptions on the file appender in order to make things work.