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

59
LINES

< > BotCompany Repo | #2000317 //

New Tinybrain snippet

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  
}

download  show line numbers   

Snippet is not live.

Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #2000317
Snippet name:
Eternal ID of this version: #2000317/1
Text MD5: ce1e89785facd87ea64a6d9ad16a7aec
Author: someone
Category:
Type: New Tinybrain snippet
Gummipassword: gummi
Uploaded from IP: 85.176.104.171
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-11 04:42:50
Source code size: 2218 bytes / 59 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 711 / 161
Referenced in: [show references]