1 | import java.io.*; |
2 | |
3 | public class main { |
4 | public static void main(String[] args) throws IOException { |
5 | String code = loadTextFile("input/main.java", null); |
6 | if (code == null) |
7 | throw new RuntimeException("Nothing to do (no input/main.java)"); |
8 | String modified = modifyCode(code); |
9 | saveTextFile("output/main.java", modified); |
10 | System.out.println("output/main.java saved!"); |
11 | } |
12 | |
13 | public static String modifyCode(String code) { |
14 | return code.replaceAll("System.out.println\\(", "if (false) System.out.println("); |
15 | } |
16 | |
17 | /** writes safely (to temp file, then rename) */ |
18 | public static void saveTextFile(String fileName, String contents) throws IOException { |
19 | File file = new File(fileName); |
20 | File parentFile = file.getParentFile(); |
21 | if (parentFile != null) |
22 | parentFile.mkdirs(); |
23 | String tempFileName = fileName + "_temp"; |
24 | FileOutputStream fileOutputStream = new FileOutputStream(tempFileName); |
25 | OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8"); |
26 | PrintWriter printWriter = new PrintWriter(outputStreamWriter); |
27 | printWriter.print(contents); |
28 | printWriter.close(); |
29 | if (file.exists() && !file.delete()) |
30 | throw new IOException("Can't delete " + fileName); |
31 | |
32 | if (!new File(tempFileName).renameTo(file)) |
33 | throw new IOException("Can't rename " + tempFileName + " to " + fileName); |
34 | } |
35 | |
36 | public static String loadTextFile(String fileName, String defaultContents) throws IOException { |
37 | if (!new File(fileName).exists()) |
38 | return defaultContents; |
39 | |
40 | FileInputStream fileInputStream = new FileInputStream(fileName); |
41 | InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); |
42 | return loadTextFile(inputStreamReader); |
43 | } |
44 | |
45 | public static String loadTextFile(Reader reader) throws IOException { |
46 | StringBuilder builder = new StringBuilder(); |
47 | try { |
48 | BufferedReader bufferedReader = new BufferedReader(reader); |
49 | String line; |
50 | while ((line = bufferedReader.readLine()) != null) |
51 | builder.append(line).append('\n'); |
52 | } finally { |
53 | reader.close(); |
54 | } |
55 | return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1); |
56 | } |
57 | } |
Began life as a copy of #563
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: | #566 |
Snippet name: | Silencer (JavaX) - disables all System.out.println statements |
Eternal ID of this version: | #566/1 |
Text MD5: | ccf4844b89a09e203b0bfa831551248e |
Author: | stefan |
Category: | |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-04-11 04:07:48 |
Source code size: | 2270 bytes / 57 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 669 / 600 |
Referenced in: | [show references] |