JAVA/JAVA(MEC)

filestream 2번이상사용시 주의

kdy_tistory 2020. 8. 24. 16:46

개발을 하다 try catch구문을 연속으로 2번사용할 때가 있었다.

try {

FileOutputStream output = new FileOutputStream(project_path + "\\aa.bat");

String str = "pushd %~dp0\r\n" + "cd root\r\n" + "git init\r\n" + "git status\r\n"

+ "git add *\r\n" + "git commit -m \"d\"\r\n" + "git remote add origin " + txt + "\r\n"

+ "git push origin master";

byte[] by = str.getBytes();

output.write(by);

output.close();

} catch (Exception e) {

e.getStackTrace();

}

  //Runtime rt = Runtime.getRuntime();

  try {

  

  String exeFile = project_path + "\\aa.bat"; 

  Process p = Runtime.getRuntime().exec(exeFile);

  //p = rt.exec(exeFile); 

  //p.waitFor(); 

  

  BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));

    String line = null;

   

    while ((line = br.readLine()) != null) {

      System.out.println(line);

    }


    int waitFor = p.waitFor();

    int result = p.exitValue();


  } 

  catch (Exception e2) {

  e2.printStackTrace(); 

  }


주의할점은 filestream 구문을 2번이상 사용시에 먼저번에 사용한 filestream구문에서 close로 닫아주지 않으면 32번에러(이미 프로그램이 사용중이라 엑세스 할 수가 없다.)가 발생하므로 항상 close하는것을 잊지말아야한다.!