1 | import java.io.*; |
2 | |
3 | public class main { |
4 | public static void main(String[] args) throws IOException { |
5 | String s = loadTextFile("input/main.java", ""); |
6 | s = s.replaceAll("for i=1, 10 \\{", "for (int i = 1; i <= 10; i++) {"); |
7 | saveTextFile("output/main.java", s); |
8 | } |
9 | |
10 | static String charsetForTextFiles = "UTF-8"; |
11 | |
12 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
13 | if (!new File(fileName).exists()) |
14 | return defaultContents; |
15 | |
16 | FileInputStream fileInputStream = new FileInputStream(fileName); |
17 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, charsetForTextFiles); |
18 | return loadTextFile(inputStreamReader); |
19 | } |
20 | |
21 | public static String loadTextFile(Reader reader) throws IOException { |
22 | StringBuilder builder = new StringBuilder(); |
23 | try { |
24 | BufferedReader bufferedReader = new BufferedReader(reader); |
25 | String line; |
26 | while ((line = bufferedReader.readLine()) != null) |
27 | builder.append(line).append('\n'); |
28 | } finally { |
29 | reader.close(); |
30 | } |
31 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
32 | } |
33 | |
34 | /** writes safely (to temp file, then rename) */ |
35 | public static void saveTextFile(String fileName, String contents) throws IOException { |
36 | File file = new File(fileName); |
37 | File parentFile = file.getParentFile(); |
38 | if (parentFile != null) |
39 | parentFile.mkdirs(); |
40 | String tempFileName = fileName + "_temp"; |
41 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
42 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, charsetForTextFiles); |
43 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
44 | printWriter.print(contents); |
45 | printWriter.close(); |
46 | if (file.exists() && !file.delete()) |
47 | throw new IOException("Can't delete " + fileName); |
48 | |
49 | if (!new File(tempFileName).renameTo(file)) |
50 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
51 | } |
52 | } |
Began life as a copy of #584 Don't change, we use this :) http://tinybrain.blog.de/2015/04/21/i-can-now-use-my-own-extended-java-20247887/
download show line numbers debug dex old transpilations
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #587 |
Snippet name: | JavaX Lua for loop translator - runnable! |
Eternal ID of this version: | #587/1 |
Text MD5: | 0fd249660172f557e41822f8edba8370 |
Author: | stefan |
Category: | javax |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-21 21:36:21 |
Source code size: | 2079 bytes / 52 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 992 / 781 |
Referenced in: | [show references] |