• ベストアンサー
※ ChatGPTを利用し、要約された質問です(原文:ファイル検索の方法)

ファイル検索方法と「*」の使い方

このQ&Aのポイント
  • フォルダ内にある、最後が「D00」で終わるファイルを表示させる方法が分からない
  • ファイル検索のために作成したコードで、「*D00」の「*」の使い方に問題があるのか
  • JavaのFileクラスを使用して、指定したフォルダ内のファイルを取得しているがうまくいかない

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

  • ベストアンサー
  • UKY
  • ベストアンサー率50% (604/1207)
回答No.1

> if(list1[i].equals(str01)) equals メソッドではだめです……。 これだと、"*D00" そのものにしかヒットしません。 正規表現を扱う matches メソッドなら、ワイルドカードが使えます。 if (list1[i].matches(".*D00")) // 正規表現に関してはちょっと自信なし。 // ↑これで合ってる? ただし、特定の文字で終わる文字列かどうかを判断するには、endsWith メソッドの方が速いです。 if (list1[i].endsWith("D00")) // 逆に startsWith なんてメソッドもあります。 // 文字列の先頭が "D00" であるかどうか調べる if (list1[i].startsWith("D00"))

brunhild
質問者

お礼

endsWithなんてあるんですね!matchesも教えて頂いたとおりでうまく出力できました。 かなり悩んでいたので本当に助かりました!ありがとうございました。

全文を見る
すると、全ての回答が全文表示されます。

関連するQ&A

  • 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
  • ファイル管理ツールを作りたいのですが

    ファイル管理ツールを作る際に、ディレクトリ内のファイル名を取得し、それをアプレット画面のテキストエリアに表示させたいのですが全くやり方がわかりません。 初心者なりに頑張って作ってみたのですが、 import java.io.File; public class Filekanri{ public static void main(String args[]){ File dir = new File("C:\\MyDir"); File[] files = dir.listFiles(); // 全ファイルの名前を取得 String fnames[] = new String[files.length]; for( int i = 0; i < files.length; i++ ) { fnames[i] = files[i].getName(); } これで良いのでしょうか?このあとどうすれば良いのかどうかどうか教えてください。宜しくお願いします。

  • コマンドプロンプトでひらがなの入力が出来ません。

    普段グーグル日本語入力を使っています。 OS XPsp2 import java.io.*; class Mojiretuate { public static void main(String[] args) throws IOException { String str = "こんにちは"; String str2; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("文字列当てゲームです。"); System.out.println("答えは何かのあいさつです。"); for(;;){ System.out.println("\n入力"); str2 = br.readLine(); System.out.println("入力文字列の長さ:" + str2.length()); if(str.equals(str2)){ System.out.println("正解です"); break; } else if(str2.length() > str.length()){ System.out.println("長すぎます。"); } else if(str2.length() == str.length()){ System.out.println("長さはあっています。"); } else{ System.out.println("短すぎます。"); } } System.out.println("終了"); } }

    • ベストアンサー
    • Java
  • コマンドラインで配列0~1番目入力したときは?

    使う言語はJava 例えばこんな感じで入力した場合、コマンドラインの配列0~1番目を入力したときコマンドラインでの配列1番目の文字列を値を出力するにはどうしたらいいのでしょうか? コマンドラインを使ってのサンプルソースがあんまり無かった為質問します。 まだまだ初心者脱却していないのでご指導ご鞭撻のほどお願いします。 お早い回答お待ちしています。 java Sample 2012年 1月 1月 考えたサンプルソースはこうです。 public class Ensyuu158 { /** * @param args */ public static void main(String[] args) { for(int i=0; i<args.length; i++){ if(args[i].equals(args[0])){ methodA(args[0]); }else if(args[i].equals(args[1])){ methodB(args[1]); } } }     //コマンドライン配列0~1番目が入力されたら1番目だけの文字列の値を出力する private static void methodB(String str) { System.out.println(str); }     //コマンドラインでの配列0番目が入力されたら0番目だけの文字列の値と文字列を出力したい private static void methodA(String str) { System.out.println(str+"月"); } } }

  • Javaで配列を定義する方法

    こんにちは、片岡といいます。 Java言語で配列を定義する場合、以下の二つの書式に違いはありますか。 違いがあるとすれば、どのような違いですか。 また、違いがないのならば、どちらの記述がよいのですか。 ご存知の方はいらっしゃいませんか。 (1) public class Main1 { public static void main(String[] args) { String str[] = new String[] { new String("itiban"), new String("niban"), new String("sanban"), }; for (int i = 0; i < str.length; i++ ) { System.out.println(str[i]); } } } (2) public class Main2 { public static void main(String[] args) { String str[] = { new String("itiban"), new String("niban"), new String("sanban"), }; for (int i = 0; i < str.length; i++ ) { System.out.println(str[i]); } } } なお、私の環境は以下の通りです。 OS: Microsoft Windows XP Professional SP2 java: java version "1.4.2_13" 検索エンジンで"配列 オブジェクト java"を検索しましたが、 解決に役立つ情報は見つかりませんでした。

    • ベストアンサー
    • Java
  • javaの表示方法がわかりません。

    javaを勉強しているのですがもう少しと言うところで悩んでいます。 配列の中身を見て同じコードがある場合にはその個数をカウントし表示するというようなことをしたいのですが表示部分でうまくいきません。 初歩的な質問ですみませんが、どうやって重複しないで表示させることができるのでしょうか。 **表示させたい結果** 001は2個 003は2個 004は1個 005は4個 ********* **表示される結果** 001は2個 001は2個 003は2個 003は2個 004は1個 005は4個 005は4個 005は4個 005は4個 ********* **ソース** class str{ public static void main(String args[]){ int i = 0; int j = 0; int S = 0; String test[] ={"001","001","003","003","004","005","005","005","005"}; String str1 = ""; String str2 = ""; for(i=0;i<9;i++){ str1 = test[i]; for(j=0;j<9;j++){ str2 = test[j]; if(str1.equals(str2)){ S++; } } System.out.println(str1 + "は" + S + "個"); S = 0; } } } *********

  • javaについて

    以下のプログラムで[\\D]+はどういう働きをしているのか分からないので教えていただけますでしょうか。 実行結果は 1990 と表示されます。 よろしくお願いいたします。 public class split{ public static void main (String[]args){ String s = "明日は1990年です。"; String [] str = s.split("[\\D]+"); for( String st: str ){ if( !st.equals("") ){ System.out.println( st ); } } } }

    • ベストアンサー
    • Java
  • ファイル読み込みを配列に入れる方法

    ファイルから数字を読み込んで 並び替えて出力するプログラムを作成しているのですが ファイルから一度に配列に取り込んで、その配列で並び変えたいのですが取り込みができないのです、int型の配列に一度に取り込むのは可能なのでしょうか? import java.io.*; class Sort { public static void main(String[] args) { String path = args[0]; File file = new File(path); FileReader fr = null; int[] buf = new int[(int)file.length()]; try { fr = new FileReader(file); fr.read(buf); for (int i=0; i<buf.length-1;i++) { int mini=buf[i]; int miniIndex = i; for(int j = i+1; j<buf.length;j++){ if(buf[j] < mini){ mini = buf[j]; miniIndex = j; } } buf[miniIndex] = buf[i]; buf[i] = mini; } for(int i =0; i<buf.length;i++) { System.out.print( buf[i]+","); } } catch (IOException e) { System.out.println("ファイル入力エラー"); } } } と作ってみたのですが‥ ご教授お願いいたします。

    • ベストアンサー
    • Java
  • クラスのフィールドの比較方法

    2つのクラスのフィールドを比較して、その結果を以下のように表示させたいのですが、どのようにプログラミングをすればいいのでしょうか? 例 class Car { String carname; int height; int width; int length; } class Garage { String garagename; int height; int width; int depth; } public class example { public static void main (String[] args) throws IOException { Car car[]; car = new Car[5]; System.out.println("車の情報を入力してください。"); BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in); for(int i=0; i<5; i++){ car[i] = new Car(); System.out.println(i+1 + "台目の車の名前を入力してください。"); String str1 = br1.readLine(); System.out.println("車の高さを入力してください。"); String str2 = br1.readLine(); System.out.println("車の幅を入力してください。"); String str3 = br1.readLine(); System.out.println("車の長さを入力してください。"); String str4 = br1.readLine(); int h1 = Integer.parseInt(str2); int w1 = Integer.parseInt(str3); int l1 = Integer.parseInt(str4); car[i].carname = str1; car[i].height = h1; car[i].weith =w1; car[i].length =l1; } for(int i=0; i<5; i++){ garage[i] = new Garage(); System.out.println(i+1 + "つ目の車庫の名前を入力してください。"); String str5 = br2.readLine(); System.out.println("車庫の高さを入力してください。"); String str6 = br2.readLine(); System.out.println("車庫の幅を入力してください。"); String str7 = br2.readLine(); System.out.println("車庫の長さを入力してください。"); String str8 = br2.readLine(); int h2 = Integer.parseInt(str6); int w2 = Integer.parseInt(str7); int d2 = Integer.parseInt(str8); garage[i].garagename = str2; garage[i].height = h2; garage[i].weith =w2; garage[i].depth =d2; } } 出力例 garage1 car3 garage2 car5 garage3 car4 garage4 car2 garage5 car1 よろしくお願いします。

    • ベストアンサー
    • Java
  • 文字列の比較について

    BufferedReaderでファイルを読んだのちに、見出しの文字列をカンマで分割して 文字列を比較しようとしています。 ですが、分割すると、文字列が別な文字コードによる表記に変わってしまいます。 なぜでしょうか? また、解決方法などのヒントなどあれば教えてください。 BufferedReader buffReader = new BufferedReader( new FileReader("/home/masa/Desktop/Sample.csv")); String s; int ini=0; int wamei=0; int i=0; while((s = buffReader.readLine())!= null){ String[] str = s.split(",", -1); //System.out.println(s); if (i == 0) { for(int j=0; j<str.length;j++){ System.out.println(String.toString(str)); if(str.equals("五十音")){ini = j;} System.out.println("五十音"+ini); if(str.equals("a")){wamei = j;} System.out.println("和名"+wamei); //System.out.println(j); } } i++;

    • ベストアンサー
    • Java