Javaで音声ファイルを変換するスクリプトで途中で止まってしまう

このQ&Aのポイント
  • Javaで作成した音声ファイル変換スクリプトが途中で止まってしまい、ファイルがコピーされない状態になっています。
  • ファイルのコピー処理が正常に行われず、変換工程に移行できない状態です。
  • 数々の書き直しや試行錯誤を行いましたが、問題の解決に至っていません。お手伝いをお願いします。
回答を見る
  • ベストアンサー

Javaが途中で止まってしまいます

Javaで音声ファイルを変換するスクリプトなのですが、音声ファイル自体は認識して「ファイルコピー終了」と出るのですが、実際にはコピーされておらず、そこで終わってしまい変換工程まで辿り着かない状態です。 ============================== import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class OnseiFileConvert { /** * @param args */ public static void main(String[] args) { OnseiFileConvert exe = new OnseiFileConvert(); exe.execute(); } /** m = 0x80; for( i=0; i<fsize; i++, m++ ) { if( m == 0xFF ) { m = 0x80; } fread( &t, 1, 1, fp ); t = t^m; fwrite( &t, 1, 1, fp_out ); } */ public void execute(){ String voice_dir_in = "C:\\java\\onsei"; String voice_dir_out = "C:\\output\\onsei"; try { convVoice(voice_dir_in,voice_dir_out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 音声変換処理 * @throws IOException * @throws InterruptedException */ private void convVoice(String voice_dir_in,String voice_dir_out) throws IOException, InterruptedException{ System.out.println("ディレクトリーコピー開始"); System.out.println("xcopy /T /E \"" + voice_dir_in + "\" \"" + voice_dir_out +"\""); Process proc = Runtime.getRuntime().exec( "xcopy /T \"" + voice_dir_in + "\" \"" + voice_dir_out +"\""); proc.waitFor(); proc.destroy(); System.out.println("ディレクトリーコピー終了"); System.out.println("ファイルコピー開始"); convData(voice_dir_in, voice_dir_out); System.out.println("ファイルコピー終了"); } /** * @param voice_dir_in * @param voice_dir_out * @throws IOException * @throws InterruptedException */ private void convData(String voice_dir_in, String voice_dir_out) throws IOException, InterruptedException { System.out.println(voice_dir_in + "/" + voice_dir_out); File in_file = new File(voice_dir_in); File[] in_files_list = in_file.listFiles(); for(int i = 0; i < in_files_list.length ; i++ ){ File chFile = in_files_list[i]; if(chFile.isDirectory()){ convData(voice_dir_in + "\\" + chFile.getName() ,voice_dir_out + "\\" + chFile.getName() ); } else { String file_name = chFile.getName(); System.out.println(file_name); if(".".equals(file_name.substring(0, 1))){ file_name = file_name.substring(1, file_name.length()-4); File ofile = new File(voice_dir_out + "\\" + file_name); if( ofile.length() == 0 ){ convFile(voice_dir_in + "\\" + chFile.getName(),voice_dir_out + "\\" + file_name); } } } } } /** * ファイル単位に変換を実施する処理 */ private void convFile(String input_file_name,String out_file_name) { //convert FileInputStream fip = null; FileOutputStream fop = null; try { System.out.println("変換開始"); fip = new FileInputStream(input_file_name); fop = new FileOutputStream(out_file_name); long fsize = (new File(input_file_name)).length(); int m = 0x80; for( int i=0; i< fsize; i++, m++ ) { if( m == 0xFF ) { m = 0x80; } int t = fip.read(); t = t^m; fop.write(t); } System.out.println("変換完了" + fsize + "Byte"); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ try { if(fip != null){ fip.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { if(fop != null){ fop.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } ============================== 長時間悩んで書き直したりしたのですが、やはり結果が変わらずお手上げ状態です。 どうぞご助力お願いします。

  • Java
  • 回答数1
  • ありがとう数14

質問者が選んだベストアンサー

  • ベストアンサー
  • salsberry
  • ベストアンサー率69% (495/711)
回答No.1

convData()からconvFile()を呼ぶ条件は合っていますか? file_nameが"."で始まるファイルの場合だけconvFile()を呼んでいるようです。 if(".".equals(file_name.substring(0, 1))){

rmta9044
質問者

お礼

回答ありがとうございました。 ご指摘通り書き換えたら無事動作いたしました。 満足のいく結果が得られ大変助かりました。 この場を借りてお礼申し上げます。

関連するQ&A

  • javaについて

    このプログラムは文字を入力して、検索をかけてその検索にかかった秒数を表示するものですが、検索数を表示するのにはどうしたらいいでしょうか? import java.io.*; import java.net.*; public class Sample { public static void main(String[] args) { System.out.println("入力してください"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); double sec = getSearchSec(htmlSrc); if (sec > 0) { System.out.println("検索秒数は" + sec + "秒"); } else { System.out.println("検索結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static double getSearchSec(String htmlSrc) { double sec = 0; String tmp = htmlSrc.replaceAll("<.+?>| ", ""); tmp = tmp.replaceAll(".*件-", ""); tmp = tmp.replaceAll("秒.*", ""); try { sec = Double.parseDouble(tmp); } catch (NumberFormatException e) { } return sec; } }

  • javaについて

    このプログラムは文字を入力して、検索をかけてその検索にかかった秒数を表示するものですが、検索数を表示するのにはどう書き換えますでしょうか? import java.io.*; import java.net.*; public class Sample { public static void main(String[] args) { System.out.println("入力してください"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); double sec = getSearchSec(htmlSrc); if (sec > 0) { System.out.println("検索秒数は" + sec + "秒"); } else { System.out.println("検索結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static double getSearchSec(String htmlSrc) { double sec = 0; String tmp = htmlSrc.replaceAll("<.+?>| ", ""); tmp = tmp.replaceAll(".*件-", ""); tmp = tmp.replaceAll("秒.*", ""); try { sec = Double.parseDouble(tmp); } catch (NumberFormatException e) { } return sec; } }

    • ベストアンサー
    • Java
  • java 検索数表示ができるようにしたいのですが

    補足へ import java.io.*; import java.net.*; import java.lang.*; public class secc3 { public static void main(String[] args) { System.out.println("入力"); String keyword = getKeyword(); String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=" + keyword, "UTF-8"); String sec = getNumSearchHits(htmlSrc); sec = sec.replace(",",""); int ken = Integer.parseInt(sec); System.out.println(ken); if (ken > 0) { System.out.println("Hit数約" + ken + "件"); } else { System.out.println("結果0件"); } } private static String getKeyword() { String keyword = ""; BufferedReader br = null; try { br = new BufferedReader(new InputStreamReader(System.in)); keyword = br.readLine(); } catch (IOException e) { e.printStackTrace(); } finally { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } keyword = keyword.replaceAll(" ", "+"); return keyword; } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } private static String getNumSearchHits(String htmlSrc) { java.util.regex.Pattern p = java.util.regex.Pattern.compile(" / 約([,0-9]+)件 - "); java.util.regex.Matcher m = p.matcher(htmlSrc); return m.find() ? htmlSrc.substring(m.start(1), m.end(1)) : ""; } }

  • Java ApachePOIについて

    サーブレットとApachePOIについて質問です。 下記のソースで書き込んだセルをすべて黄色で塗りつぶしをしたいのですが、 ソースの書き方を教えてください。 以上、お願いします。 「ソース」 // 業務名前 String[] name4 = request.getParameterValues("gyoumuname"); for (int i = 0; i < name4.length; i++) { System.out.println(i + " " + name4[i]); name4[i] = new String(name4[i].getBytes("8859_1"), "UTF-8"); List outList=new ArrayList(); for (int i = 0; i < name4.length; i++) { outList.add(name4[i]); } for (int i = 0; i < outList.size(); i++) { Row row5 = sheet.getRow(8 + i); row5.getCell(3).setCellValue(new HSSFRichTextString(outList.get(i).toString())); } // 値を書き込んだエクセルを出力する FileOutputStream out = null; try { out = new FileOutputStream( "C:\\Users\\satou\\Desktop\\weekreport.xls"); workbook.write(out); } catch (IOException e) { System.out.println(e.toString()); } finally { try { out.close(); } catch (IOException e) { System.out.println(e.toString()); } } 以下省略。

  • javaについて

    こういうプログラムを組んだんですが、うまく実行できません。 どんな改善をしたらよいでしょうか? import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class Sample { public static void main(String[] args) { String htmlSrc = getHTMLSrc("http://search.yahoo.co.jp/search?p=java", "UTF-8"); htmlSrc = htmlSrc.replaceAll("<.+?>| ", ""); htmlSrc = htmlSrc.replaceAll(".*件-", ""); htmlSrc = htmlSrc.replaceAll("秒.*", "秒"); System.out.println(htmlSrc); } private static String getHTMLSrc(String strURL, String charSet) { StringBuffer sb = new StringBuffer(); HttpURLConnection conn = null; BufferedReader br = null; try { URL url = new URL(strURL); conn = (HttpURLConnection)url.openConnection(); InputStreamReader isr = new InputStreamReader(conn.getInputStream(), charSet); br = new BufferedReader(isr); String tmp = ""; while ((tmp = br.readLine()) != null) { sb.append(tmp); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (br != null) { br.close(); } if (conn != null) { conn.disconnect(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } } 以下のエラーが表示されるんですが、どうしたらよいでしょうか?? 環境が悪いのでしょうか?? java.net.ConnectException: Operation timed out

    • ベストアンサー
    • Java
  • Javaのネットワークに関して

    一つのサーバから複数のクライアントにメッセージを送るようにしなさい。いくつのクライアントと接続するか は入力により動的に決めるようにすること。複数のクライアントとの通信にはそのソケットを配列やコンテナを使って実装すればよい。サ ーバを「No23ex04Server」としクライアントを「No23ex04Client」とすること。 とあります。 以下のコードでクライアント側のプログラムは一切変えなくてよいそうなので、サーバー側のプログラムを以下のようにしたのですが、acceptが呼ばれている時点で、ループが止まってしまうので、入力待機状態になりません。スレッドを使わなくてもできるそうなのですが、私にはさっぱりです。 public class No23ex04Client { /** * @param args */ public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ String cIpAddress = null; String hostName = null; String sIpAddress = null; int portNo = 65535; try { cIpAddress = InetAddress.getLocalHost().getHostAddress(); hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e) { } System.out.println("Client IP Address : " + cIpAddress); System.out.println("Host Name : " + hostName); Scanner scn = new Scanner(System.in); System.out.print("Server IP Address? : "); sIpAddress = scn.next(); System.out.print("Server Port Number? : "); portNo = scn.nextInt(); System.out.println("■Start■"); try { Socket sc = new Socket(sIpAddress, portNo); BufferedReader br = new BufferedReader(new InputStreamReader(sc.getInputStream())); while (true) { System.out.print("From Server Message : "); String fromServerMessage = br.readLine(); System.out.println(fromServerMessage); if (fromServerMessage.contains("さようなら")) break; } sc.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("■Shutdown■"); } } public class No23ex04Server { /** * @param args */ public static void main(String[] args) { // TODO 自動生成されたメソッド・スタブ String ipAddress = null; String hostName = null; int portNo = 65535; try { ipAddress = InetAddress.getLocalHost().getHostAddress(); hostName = InetAddress.getLocalHost().getHostName(); } catch (UnknownHostException e){ } System.out.println("IP Address : " + ipAddress); System.out.println("Host Name : " + hostName); Scanner scn = new Scanner(System.in); System.out.print("Port Number? : "); portNo = scn.nextInt(); System.out.print("Socket Number? : "); int num = scn.nextInt(); System.out.println("■Start■"); try { ServerSocket ssc = new ServerSocket(portNo); System.out.println("Serverが起動(Port Number : " + ssc.getLocalPort() + ")"); Socket[] sc = new Socket[num]; BufferedWriter[] bw = new BufferedWriter[num]; for (int i = 0; i < num; i++) { sc[i] = ssc.accept(); System.out.println("接続" + i + " : " + sc[i].getRemoteSocketAddress()); bw[i] = new BufferedWriter(new OutputStreamWriter(sc[i].getOutputStream())); } while (true) { System.out.print("To Client Message : "); String toClientMessage = scn.next(); for (BufferedWriter b : bw) { b.write(toClientMessage); b.newLine(); b.flush(); } if (toClientMessage.contains("さようなら")) break; } for (Socket s : sc) s.close(); } catch (IOException e) { e.printStackTrace(); } System.out.println("■Shutdown■"); } }

    • ベストアンサー
    • Java
  • java わからない。

    今、卒業研究でJAVAを扱っています。 しかし、思った動作をするプログラムが作れません。 時間もなくなってきたので、okwaveに投稿しました。 お願いします。 パソコンはwindowsです。 プログラムを実行すると, (1)コマンドプロセッサでdirを実行してくれる。 (2)実行した後にファイルにする。 実行するときは[dir > dir.txt]のようにしたいです。 今まで書いてきた例をいかに描きます。 import java.io.*; public class TestExec1 { public static void main(String[] args) { try { String s1 = "dir"; Process process = Runtime.getRuntime().exec(s1); InputStream is = process.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (Exception e) { e.printStackTrace(); } } } どこが間違っているかわかりません。 よろしくお願いします。

  • javaのfile書込みについて

    毎度お世話になります。 Q1)下記のコードに於きまして、16コラム毎にデーターの書込みを行なう様に 変更できますか? 即ち、書込みデーターが長ければ、その分だけブランク数を減らしまして、フォーマット を見やすくする。 ============================ void file_out(){ try{ File file = new File("c:\\TRSX700\\test.txt"); if (checkBeforeWritefile(file)){ FileWriter fw = new FileWriter(file); for(int j=0; j<8; j++){ for(int i=0; i<11; i++){ fw.write(String.valueOf(cndMSRdata[j][i])); fw.write(" "); } fw.write("\r\n"); } fw.close(); } else{ System.out.println("ファイルに書き込めません"); } } catch(IOException e){ System.out.println("IOException: "+e); } //====================================== } 以上、宜しくお願いします。

    • ベストアンサー
    • Java
  • Javaでクイズ作成途中もエラー

    あの、以前こちらで質問した者です。→http://okwave.jp/qa/q6482901.html 今、英単語が表示され日本語でそれを答えるクイズを作ろうとしています。 まだまだ作りかけですが、どうしてもコンパイル時にエラーになってしまいます。 どこから直せば良さそうですか?自己研究ということで周りに質問する人がいません。 すみませんがヒントを頂けたらと質問しました。よろしくお願いします。 import java.io.*; public class EnglishToJapaneseQuiz { public int MAX_QUESTION = 5; public int GOODANSWER = 0; public static void main(String[] args) { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { System.out.println("Welcome! This quiz helps you study Japanese in English"); System.out.println("'n' normal"); System.out.println("'h' hard"); System.out.println("'e' expert"); System.out.println("'z' exit"); System.out.println("Please select the level"); String line = reader.readLine(); char c = line.charAt(0); switch (c) { case 'n': System.out.println("You selected normal"); for (int i=0; i<MAX_QUESTION; i++) { int x = (int)(Math.random() * 10) + 1; switch (x) { case 1: System.out.println("School"); String Answer = "学校"; BufferedReader responce = new BufferedReader(new InputStreamReader(System.in)); String line = responce.readLine(); If (line.equals(Answer)) { System.out.println("correct"); GOODANSWER = GOODANSWER + 1; } else { System.out.println("incorrect"); } break; case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: } } break; case 'h': System.out.println("You selected hard"); break; case 'e': System.out.println("You selected expert"); break; case 'z': break; default: System.out.println("you must indicate valid character"); break; } } catch (IOException e) { System.out.println(e); System.out.println("invalid value"); } System.out.println("the program was successfully ended"); } }

    • ベストアンサー
    • Java
  • JAVAのプログラムについて

    独学でJAVAを勉強中なのですが、 import java.io.*; public class ExserciseD5L3_2{ public static void main(String args[]){ BufferedReader br = new BufferedReader(newInputStreamReader(System.in),1); try{ System.out.println("■■■計算クイズ■■■"); System.out.println("計算してください。"); String Que[] = {"10×50=?","21-7=?","360÷6=?"}; int Ans[] = {500,14,60}; int counter; for(counter = 0; counter <=2; counter++ ){ System.out.println(Que[counter]); System.out.println("答えは?"); String str = br.readLine(); int i = Integer.parseInt(str); if(i == Ans[counter]){ System.out.println("おめでとう!大当たりです。"); } else{ System.out.println("残念!答えは"+Ans[counter]+"です。"); } } } catch(IOException e){ System.out.println("IOエラーが発生しました。"); } catch(NumberFormatException ne){ System.out.println("入力された数値が正しくないようです。"); } } } これを実行すると ■■■計算クイズ■■■ 計算してください。 10×50=? 答えは? 500 おめでとう!大当たりです。 21-7=? 答えは? 14 おめでとう!大当たりです。 360÷6=? 答えは? 60 おめでとう!大当たりです。 となるのですが、これに おめでとう!正解数は3つです。とか正解数は2つですなどのように 正解数も出るようにするにはどのようにしたらいいのでしょうか?

    • ベストアンサー
    • Java

専門家に質問してみよう