Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

59
LINES

< > BotCompany Repo | #1000252 // mainAdder, silenced

JavaX source code - run with: x30.jar

1  
import java.io.*;
2  
3  
public class main {
4  
  static String prelude =
5  
    "public class main {\n" +
6  
    "  public static void main(String[] args) {\n";
7  
  static String postlude =
8  
    "  }\n" +
9  
    "}\n";
10  
11  
  public static void main(String[] args) throws IOException {
12  
    String code = loadTextFile("input/main.java", null);
13  
    if (code == null)
14  
      throw new RuntimeException("Nothing to do (no input/main.java)");
15  
    saveTextFile("output/main.java", prelude + code + postlude);
16  
    if (false) System.out.println("output/main.java saved!");
17  
  }
18  
19  
  /** writes safely (to temp file, then rename) */
20  
  public static void saveTextFile(String fileName, String contents) throws IOException {
21  
    File file = new File(fileName);
22  
    File parentFile = file.getParentFile();
23  
    if (parentFile != null)
24  
      parentFile.mkdirs();
25  
    String tempFileName = fileName + "_temp";
26  
    FileOutputStream fileOutputStream = new FileOutputStream(tempFileName);
27  
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream, "UTF-8");
28  
    PrintWriter printWriter = new PrintWriter(outputStreamWriter);
29  
    printWriter.print(contents);
30  
    printWriter.close();
31  
    if (file.exists() && !file.delete())
32  
      throw new IOException("Can't delete " + fileName);
33  
34  
    if (!new File(tempFileName).renameTo(file))
35  
      throw new IOException("Can't rename " + tempFileName + " to " + fileName);
36  
  }
37  
38  
  public static String loadTextFile(String fileName, String defaultContents) throws IOException {
39  
    if (!new File(fileName).exists())
40  
      return defaultContents;
41  
42  
    FileInputStream fileInputStream = new FileInputStream(fileName);
43  
    InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
44  
    return loadTextFile(inputStreamReader);
45  
  }
46  
47  
  public static String loadTextFile(Reader reader) throws IOException {
48  
    StringBuilder builder = new StringBuilder();
49  
    try {
50  
      BufferedReader bufferedReader = new BufferedReader(reader);
51  
      String line;
52  
      while ((line = bufferedReader.readLine()) != null)
53  
        builder.append(line).append('\n');
54  
    } finally {
55  
      reader.close();
56  
    }
57  
    return builder.length() == 0 ? "" : builder.substring(0, builder.length()-1);
58  
  }
59  
}

Author comment

Began life as a copy of #2000317

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: #1000252
Snippet name: mainAdder, silenced
Eternal ID of this version: #1000252/1
Text MD5: c407432766b2b600ee89b176f0b41062
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-11 04:43:20
Source code size: 2276 bytes / 59 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 653 / 524
Referenced in: [show references]