Read a text file with Java [duplicate] - java

This question already has answers here:
Reading text file always returns 0 - Java
(2 answers)
Closed 7 years ago.
I have a game client jar that has an updater. Its client.jar and it downloads a cache which has all the game files. There is no need to update the client.jar just the cache, but the problem is the version number is hardcoded into the client.
What I've been trying to do ALL day is change the way the version number is obtained, and I just can't get it.
http://pastebin.com/Z5urTUVw
Line 31 is the version number, I need that to read a version number from a dropbox link. Found here - https://dl.dropboxusercontent.com/u/87363031/version.txt
Just 1 simple number, I've been trying to do this for the better part of 6 hours now and I'm bout to lose my cool. Can someone please help me with this?

If you can use Apache Commons IO, there's a very simple way to achieve this using IOUtils:
InputStream in = new URL("https://dl.dropboxusercontent.com/u/87363031/version.txt").openStream();
try {
String version = IOUtils.toString(in));
System.out.println(version);
} finally {
IOUtils.closeQuietly(in);
}

Related

Get values froma a Dataset<Row> into a .txt file (using Java) [duplicate]

This question already has answers here:
Write/store dataframe in text file
(5 answers)
Closed 4 years ago.
Im new here so i hope to help you and be helped if could be possible.
I made a Apache Spark project using Spark SQL and ML Spark in Java. I've finished this project but i have some problems with the output.
I've got a Dataset<Row> final (final is the name of the dataset) with some information. When i use show() with this dataset ( finals.show(); ) I get the next information:
[2018026,1,9.93,127.66,5.16,245.8,4.426875,6.91]
[2018026,1,9.97,127.89,5.36,244.8,4.426875,6.91]
[2018026,1,6.76,113.54,6.42,228.8,4.426875,6.91]
[2018026,1,6.92,114.2,6.81,224.0,4.426875,6.91]
[2018026,1,6.86,113.98,6.65,226.0,4.426875,6.91]
[2018026,1,6.81,113.76,6.58,227.4,4.426875,6.91]
[2018026,1,6.97,113.49,6.58,225.8,4.426875,6.91]
[2018026,1,6.97,114.42,6.67,221.6,4.426875,6.91]
(The "," is the separator between the fields).
Well, I'm trying to get this output in text file, for example projectSpark.txt but is impossible to me. How can I get this info on a text file?
Should I iterate over the Dataset<Row> or are there some methods to do this?
Thank so much guys.
Regards.
In Java, you can do something like it:
finals.javaRDD().map(x -> x.toString()).saveAsTextFile("your/path");
It will save your dataset in a single text file.

Read yaml file in java [duplicate]

This question already has an answer here:
How do you actually parse values in YAML in Java? [duplicate]
(1 answer)
Closed 6 years ago.
I want to write java program to read set of data from YAML file in cloud foundry.
#Define CDN domains
---
domains:
name : CDN1
quality : 200..300
cost : low
location: http://
name: CDN2
quality: 400..500
cost: high
location: http://
Then, in the program based on the name and quality it should redirect the first request to new location.
Can anyone help me for this?
I`m entirely new in YAML!
Based on my search, I can use snacked YAML or bean YAML, but I don't know what difference is.
You can use Jackson to read YAML, which is widely supported in Spring: https://github.com/FasterXML/jackson-dataformat-yaml

Decode HTML Escape Characters [duplicate]

This question already has answers here:
How to do URL decoding in Java?
(11 answers)
Closed 7 years ago.
I'm downloading a JSON from the Google Directions API. For the HTML_Instructions field, representing the actual instruction needed for navigation, here is the format:
"Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e",
Is there a way to decode/remove the escape characters from the String that is downloaded in Java/an Android application.
Thanks for the help.
In Java use
String result = java.net.URLDecoder.decode(url, "UTF-8");
In JS use decodeURIComponent
document.write(decodeURIComponent("Head \u003cb\u003esoutheast\u003c/b\u003e on \u003cb\u003eMinor Ave\u003c/b\u003e toward \u003cb\u003eMadison St\u003c/b\u003e"))

Writing to a File - Java [duplicate]

This question already has answers here:
How do I create a file and write to it?
(35 answers)
Closed 7 years ago.
I'm trying to write a program that writes a listing of things to a *.txt file, and everything appears to be all good and well but nothing is written in the txt file. Any help is appreciated. I won't post my code on here because it's only a small fraction, but I'm using a formatter in the form Formatter fileMaker = new Formatter("file.txt"); and then I would do something like fileMaker.format(%s, String str); but nothing turns up in the file? Thanks for the help.
Hopefully this little bit might help, maybe I'm doing something wrong that I don't see, but I declared it as private static Formatter fileMaker; in the class and then I implement it like below.
fileMaker.format("%s%d\n", "#flightCount", flights2.size());
for(int i=0; i<flights2.size(); i++){
fileMaker.format("%s %s%s%d%d %d ", "#newFlight", flights2.get(i).getSourceAirport(), flights2.get(i).getDestinationAirport(),
flights2.get(i).getTakeoffTime(), flights2.get(i).getLandingTime(), flights2.get(i).getCapacity());
}
PrintWriter fileWriter= new PrintWriter("my-file.txt");
fileWriter.println("Content");
fileWriter.close();

how to read a config file with sections using Java [duplicate]

This question already has answers here:
What is the easiest way to parse an INI file in Java?
(13 answers)
Closed 7 years ago.
Given a file containing this:
[upper]
a = A
b = B
[words]
1 = one
2 = two
How do I access these key/values with reference to their headers? Java's Properties() class only handles section-less files.
Use the ini4j library (tutorial linked): http://ini4j.sourceforge.net/tutorial/
If you are a fan of the Apache Commons offerings, they have a library just for you: Commons Configuration. Commons Configuration reads many more formats other than just the .INI style files.

Categories