首页 > 解决方案 > 如何使用 Java 程序在 CodeBlocks 中打开特定的 C++ 文件?

问题描述

我创建了一个 Java 程序来创建一个以文件名作为用户输入的 C++ 文件,然后在文件创建完成后,程序应该会自动在 CodeBlocks 中打开创建的 C++ 文件。

 System.out.println("Enter the Name of the file to be created ---");
         name =sc.next();
         File f = new File(name+".cpp");
         if(f.exists())
         {
             System.out.println("File already exists");
         }
         else
         {

             try{ 
                 f.createNewFile();
                 FileWriter fw = new FileWriter(name+".cpp");
                 fw.write("#include<bits/stdc++.h>\r\nusing namespace std;\r\nint main()\r\n{\r\n"
                 + "ios_base::sync_with_stdio(false);\r\ncin.tie(NULL);\r\n\r\nreturn 0;\r\n}");
                 fw.close();
                }
             catch(Exception e){System.out.println(e);} 
             System.out.println("!! File Sucessfully Created !!");
             try {
                  Runtime r = Runtime.getRuntime();
                  r.exec("CodeBlocks C:\\Users\\ADITYA\\Documents\\codeblocks progs\\"+name+".cpp");
                 } catch (IOException e) {
                 System.out.println(e.printStackTrace());                
                 }

         }

标签: java

解决方案


就像是

Process p = Runtime.getRuntime().exec("path/to/codeblock/executable --file="+filePath);

命令行参数


推荐阅读