From c009693a07276f8d66cd68637431f0a3f0e9ef15 Mon Sep 17 00:00:00 2001 From: Julian T Date: Tue, 8 Sep 2020 13:43:21 +0200 Subject: Added more java assignments --- sem5/oop/m2/Opg1/src/Main.java | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 sem5/oop/m2/Opg1/src/Main.java (limited to 'sem5/oop/m2/Opg1/src') diff --git a/sem5/oop/m2/Opg1/src/Main.java b/sem5/oop/m2/Opg1/src/Main.java new file mode 100644 index 0000000..179e678 --- /dev/null +++ b/sem5/oop/m2/Opg1/src/Main.java @@ -0,0 +1,38 @@ +import java.io.FileWriter; +import java.io.IOException; + +public class Main { + + static void writeLines(String name) throws IOException{ + FileWriter f = null; + try { + f = new FileWriter(name); + + for (int i = 1; i <= 100; i++) { + f.write(String.format("%-3d ", i)); + if (i % 10 == 0) { + f.write(System.lineSeparator()); + } + } + } finally { + if (f != null) { + f.close(); + } + } + } + + public static void main(String[] args) { + String name = "hej.txt"; + if (args.length > 0) { + name = args[0]; + } + + System.out.printf("Writing to %s%n", name); + try { + writeLines(name); + } catch(IOException e) { + System.err.printf("Failed with: %s%n", e); + } + } + +} -- cgit v1.2.3