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

58
LINES

< > BotCompany Repo | #567 // Lua print to Java print

JavaX source code - run with: x30.jar

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

Author comment

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: #567
Snippet name: Lua print to Java print
Eternal ID of this version: #567/1
Text MD5: 6a00274a8988ad3b5b3d13ae07c8dd0a
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-04-11 05:14:45
Source code size: 2231 bytes / 58 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 685 / 590
Referenced in: [show references]