Writing to a File - Java [duplicate] - java

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();

Related

How to find Encoding of a file using Java [duplicate]

This question already has answers here:
Java : How to determine the correct charset encoding of a stream
(16 answers)
Closed 3 years ago.
I am trying to find the encoding of a file using the java program. But it always providing the UTF-8 as the output. Even though it is an ANSI file.
import java.io.InputStream
import java.io.FileInputStream
import java.io.BufferedInputStream
import java.io.InputStreamReader
new InputStreamReader(new FileInputStream("FILE_NAME")).getEncoding
The library is old and looks no proper support for that.
https://code.google.com/archive/p/juniversalchardet/
Some are so many answers, that say we can find the encoding of the file like
Java : How to determine the correct charset encoding of a stream
These solutions doesnt look good. According to # Jörg W Mittag We cannot find the encoding of a file for sure.
In scala I don't have sure, but have you tried alread this lib?
public static Charset guessCharset2(File file) throws IOException {
return CharsetToolkit.guessEncoding(file, 4096, StandardCharsets.UTF_8);
}

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.

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"))

Read a text file with Java [duplicate]

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);
}

java.* package for encoding decoding [duplicate]

This question already has answers here:
Decode Base64 data in Java
(21 answers)
Closed 9 years ago.
I have used com.sun.org.apache.xerces.internal.impl.dv.util.Base64 package for the purpose of encoding decoding of strings. But I want to use java.* package for encoding and decoding instead of com.sun.apache.* package.
Can you please suggest an appropriate java.* package?
If you can wait until Java 8 is released - there will be a java.util.Base64 class.
In the meantime you should use the solution from Joachim Sauer's comment. (See Decode Base64 data in Java - second answer)
Use the java Packages:
java.net.URLDecoder
java.net.URLEncoder
And use it like this:
public static String decodeString(final String string) {
try {
return URLDecoder.decode(string, "UTF-8");
} catch (final UnsupportedEncodingException e) {
TLog.d(LOG, "Decoding Not Supported");
}
return string;
}
What you mean?
You want to use:
import java.*;
instead of using:
import com.sun.apache.*;
?
Seems a lit bit hard. I have one way to do this:
Download the source code of com.sun.org.apache.xerces.internal.impl.dv.util.Base64 packege.
Update the package name.
Re-package the source code.
Import the jar file again.
I don't think you should do this, it might be some license issue.

Categories