I have a static method that creates an xml file and I would like it to return the raw xml file as a string. After creating the xml file I would like to read from the file and convert it to a string. How do I go about doing so?
http://guava-libraries.googlecode.com/svn/trunk/javadoc/com/google/common/io/Files.html
Joiner.on('').join(Files.readLines(file, CharSet.fromName("UTF-8")))
you could turn to, for the file handling, to the apache.commons.io library.
This one has build in convenience functions for reading and storing files.
So for reading
org.apache.commons.io.FileUtils#readFileToString(File file)
and for writing
org.apache.commons.io.FileUtils#writeStringToFile(File file, String data)
See here for javadoc
http://commons.apache.org/io/
Here is a variety of ways of doing it:
How to create a Java String from the contents of a file
Related
I am trying to get information from many xml files in a directory.
How can I get specific information from each one and send it to an excel file, in java?
file 1.xml
file 2.xml
file 3.xml
*********
**file.csv** or .**xls** with the information of the 'n' files XML
there are several libraries on Java that can help you to do so.
For instance, for getting information from XML you can use dom4j and extract the specific information make use of the query language XPATH, supported by the library (examples). And to read all the XML files form a directory, Java 8 has an easy way of achieving that.
Files.list(Paths.get("/path/to/xml/files"))
.map(YourXMLParser::parse)
.forEach(XLSExporter::export);
where parse method would have the signature:
public MyDataBean parse(Path path) {
InputStream inputStream = Files.newInputStream(Path);
SAXBuilder saxBuilder = new SAXBuilder(inputStream);
... <-- Making use of SAX for instance and return the read data in a custom Bean (MyDataBean)
}
As Files.list() method return Stream you can take advantage of that to use map and forEach.
Once you have the information from each XML files to you can export to XLS using the most used library in Java for it: Apache POI
I hope it can help.
I have a long JSON file and i want to copy a specific element from it(i know its name) to an excel file.
eg :: Suppose i want to make an excel file having "Product" (Baleno, i20, Ford Figo etc) imported from a JSON file, how to do it using GET POST or without AJAX.
So, obviously there are ways to write this yourself. What I recommend, however, is using a library (or two. I'd recommend JSON Simple and/or Apache POI) Software engineering is about efficiency, and that includes for the engineer. Using libraries is not shameful. I'd recommend doing that first. Try out using librarys, okay?
-Batista
One simple method I have used, when you only require the content you have in the JSON and if the output needs no formatting!
Create/Construct/Return a CSV File containing the content.
Product,Q1Sales,Q2Sales,Q3SalesQ4Sales
"Baleno",6000,5000,7000,5500
Return the Mimetype Filename as "BalenoSales.xls"
Make the Suffix of the Servlet URL ".xls" as well so Excel/IE likes it.
Dear brothers Hope you all right?
I'm designing a document program, however, rather to save file .text extension or using any other MS-Office API in java, i want to create my custom file format such as ".sad" extension so that this sort of file can only be read by my programs, how this can be possible?
Your requirement seems ambiguous. Are you looking to make a program that creates MS Office Word documents or plain text files with a custom file extension?
In the case of the former, you can't have a custom extension as MS Word documents, by definition, have a .doc / .docx extension.
However, if you are looking to create a program that produces text files then you can easily have a custom extension. Just look at this tutorial: How to create a file in Java
I already stated why this is a bad idea. Yet I have a solution for you (more like a how-not-to-do-it)
Take your plain text you want to save, convert it to bytes and apply this "highly enthusiastic encryption nobody will ever be able to break" on it:
string plainText = "yadayada";
bytes[] bytesFromText = toBytes(plainText);
bytes[] encrypted = new Array(sizeof(bytesFromText)*2);
for(int i = 0; i < sizeof(bytesFromText); i++){
if((i modulo 2) == 0){
encrypted.push(toByte(Math.random modulo 255));
}
encrypted.push(bytesFromText[i]);
}
I let it up to you to figure out why this is a bad idea and how to decrypt it. ;)
You can create file with any extension
For example,
File f = new File("confidential.sad");
Hope this will work for you :)
Working with custom files in Java
Here is the tutorial that will help you in getting the concept about how to create your own files with custom extension such as .doc or .sad with some information embedded in it and after saving the file you want to read that information form the file.
ZIP
Similar applications often use archives to store data. Consider MS-Word and its documents >with the .docx file extension. If you change the extension of any .docx file to .zip, you >will find that the document is actually a zip archive, with only a different extension.
https://www.ict.social/java/files/working-with-custom-files-in-java-zip-archive
I have published a library that saves files, and handles everything with one line of code only, you can find it here along with its documentation
Github repository
and the answer to your question is so easy
String path = FileSaver
.get()
.save(file,"file.custom");
I am trying to load some data from an internal .txt file. After some efforts with FileHandle the only I thing I've accomplished is to put this .txt file into a string variable. Instead of this string I need the integers that are stored inside:
FileHandle handle = Gdx.files.internal("txt/questions.txt");
String lines = handle.readString(); `
Part of txt file:0
#a)1! b)0,350,190,185! c)180,1247,180,153! d)710,970,124,101! e)615,1105,175,120! //sheep
#a)2! b)208,344,248,191! c)403,957,142,127! d)655,1250,142,130! e)0,1075,263,150! // elafi
#a)3! b)460,344,164,200! c)10,1232,165,155! d)245,915,150,133! e)268,1083,235,145! //elephant
#a)4! b)624,344,234,190! c)835,260,150,55! d)500,1228,155,172! e)800,1117,185,108! //horse
#a)5! b)858,330,167,203! c)10,890,220,174! d)822,1235,178,145! e)575,943,128,141! //rabbit
You need to "parse" your text file. You could write a simple parser for your text file format (there is nothing special in Libgdx to support parsing text files, so any standard Java features like Java - Parsing Text File OR http://pages.cs.wisc.edu/~hasti/cs302/examples/Parsing/parseString.html might help).
Alternatively, it might be simpler put your text file in a format that is easy for existing Libgdx code to parse. That generally means "JSON". JSON is not a Libgdx file format, so there are lots of tools and tutorials explaining JSON. (This format makes more sense if your file is generated by a tool and isn't maintained by a human directly.)
How would I go about converting an abstract File path (of File type) into a String type?
File.getPath() will give you the path as a String.
If you want the contents of the file, use either of
IOUtils.toString(InputStream,Charset) from Apache Commons-IO
Files.toString(File,Charset) from Google Guava.
Use File.getAbsolutePath().
check this link Abstarct path to String as path
check the method getPath()
The simplest way to get file path as a string is as below code:
String FilepathAsString = fileobject.getAbsolutePath();
Import required : java.io.File;
If you want to load the contents of the file into a String, using google guava it's a case of
String st = Files.toString(file, Charsets.UTF-8);
see the Javadoc.
Of course this does require you to know the character set of the file. Files contain bytes, strings are characters, and there always end up being bugs when converting between the two if you don't actually know what the bytes in your file mean.