Mit dem folgendem Code kann ein beliebiger Inhalt in eine Textdatei geschrieben werden. Sollte die Datei bereits existieren, wird der Text am Ende der Datei angefügt:
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; public class SimpleFileWriter { static final Logger logger = Logger.getLogger(SimpleFileWriter.class.getName()); public static void writeFile(String pathName, String content) throws IOException { File file = new File(pathName); try (FileWriter writer = new FileWriter(file, true)) { writer.write(content); writer.flush(); } } public static void main(String[] args) { try { writeFile("C:/Temp/test.txt", "Hello World!" + System.getProperty("line.separator", "\r\n")); System.out.println("Data was successfully written."); } catch (IOException ex) { logger.log(Level.WARNING, ex.getLocalizedMessage()); } } }
For some Java applications it might be important to access the console to print some information from a console command. That’s why I wrote a little program which reads the output from the Windows command-line interface (cmd) and shows it on the screen. Note: This script also works on a Linux shell! …weiterlesen
With this post I just want to show how to convert a Java collection into an array and how to convert a wrapper class array into an array of the corresponding primitive data type. As a bonus I will show how to iterate over an ArrayList.
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | public static void main(String[] args) { // Java collection (ArrayList) with Floats ArrayList<Float> someFloats = new ArrayList<Float>(); someFloats.add(3.72f); someFloats.add(28.07f); someFloats.add(2011f); // Convert ArrayList into an array Float[] floatWrapperArray = someFloats.toArray(new Float[someFloats.size()]); float[] floatArray = new float[floatWrapperArray.length]; // Transfer array of Wrapper type (Float) into array of primitive data type (float) for (int i = 0; i < floatArray.length; i++) { floatArray[i] = floatWrapperArray[i].floatValue(); } // 1. Float ArrayList System.out.println("1. Float ArrayList"); for (Iterator iterator = someFloats.iterator(); iterator.hasNext();) { Float currentFloat = (Float) iterator.next(); System.out.println(currentFloat); } // 2. Float array System.out.println("2. Float array"); for (int i = 0; i < floatWrapperArray.length; i++) { System.out.println(floatWrapperArray[i]); } // 3. float array System.out.println("3. float array"); for (int i = 0; i < floatArray.length; i++) { System.out.println(floatArray[i]); } } |
Output
1 2 3 4 5 6 7 8 9 10 11 12 | 1. Float ArrayList 3.72 28.07 2011.0 2. Float array 3.72 28.07 2011.0 3. float array 3.72 28.07 2011.0 |
This code shows how to draw a simple triangle with OpenGL ES (OpenGL 1.0) on the screen of an Android mobile device. It is based on the tutorial OpenGL ES Tutorial for Android – Part I – Setting up the view from the Jaway Team Blog. To keep things simple I decided to implement only the necessary parts and to comment things that are not obvious. Please note that the class …weiterlesenGL10 stands for OpenGL 1.0.
Kurzer Code-Schnippsel, um eine properties-Datei, die in einem Paket aus einer JAR-Datei steckt, einzulesen:
1 2 3 4 5 6 7 8 9 10 | Properties properties = new Properties(); try { InputStream is = this.getClass().getClassLoader().getResourceAsStream("com/oberprima/upload/client/configuration/config.properties"); properties.load(is); } catch (IOException ex) { System.out.println(ex.getLocalizedMessage()); } |
Um lange Zeichenketten (Strings) mit Werten zu füllen, bietet sich das Java MessageFormat an:
1 2 3 4 5 6 7 8 9 10 11 12 | public static void main(String[] args) throws Exception { String template = "My name is {0} {1}." + System.getProperty("line.separator", "\r\n") + "I am {2} years old."; Object[] values = new Object[] { "Benny", "Neugebauer", 24 }; String sentence = MessageFormat.format(template, values); System.out.println(sentence); } |
Die Ausgabe wäre hierfür:
My name is Benny Neugebauer.
I am 24 years old.
Möglich wäre auch dieser Einzeiler:
String sentence = String.format("My Name is %s %s.%sI am %d yars old.", "Benny", "Neugebauer", System.getProperty("line.separator"), 24);
Man könnte ebenso String Templates verwenden, müsste dann aber Bibliotheken wie etwa ANTLR (Five minute Introduction in ANTLR String Templates) verwenden. Natürlich kann man Strings auch verketten (konkatenieren), was aber wenig übersichtlich ist:
1 2 3 4 5 6 7 8 9 10 | String sentence = "My Name is " +"Benny" +" " +"Neugebauer" +"." +System.getProperty("line.separator") +"I am " +24 +" years old."; System.out.println(sentence); |
Hinsichtlich der Performanz, sind String Builder besser geeignet als die Konkatenation von Zeichenketten:
1 2 3 4 | StringBuilder sb = new StringBuilder("My name is ").append("Benny "); sb.append("Neugebauer").append(".").append(System.getProperty("line.separator")); sb.append("I am").append(24).append(" years old."); System.out.println(sb.toString()); |
Wie man an diesen kurzen Beispielen sieht gibt es viele Möglichkeiten, die zum Ergebnis führen. Mir gefällt derzeit das Message Format am besten. Das Message Format ist für den Standard Java-Logger (java.util.logging.Logger) sogar das von NetBeans 7 bevorzugte Format für Log-Nachrichten.

0