javaのテキストファイルとコマンドライン引数について質問です。
java Find lamb mary1.txt mary2.txt とインプットしたら、テキストから、
mary1.txt: Mary had a little lamb,
mary1.txt: little lamb, little lamb,
mary1.txt: Mary had a little lamb, its fleece was white as snow.
mary1.txt: and everywhere that Mary went, the lamb was sure to go.
mary2.txt: it made the children laugh and play to see a lamb at school.
とアウトプットがでるようにしたいのですがやり方がわかりません。
テキストの中身は下の画像で、
コマンドライン引数は
また、もしファイルが見つからない場合は"ファイルが見つかりません"とプリントしたいと思っています。
今の所、コマンドライン引数はできているようですが、テキストの中身をアウトプットできるようにするにはどうやればいいのでしょうか?
コードは下のようになってます。
よろしくお願いします。
public class Find { public static void main(String[] args) throws FileNotFoundException { if (args.length < 1) { System.out.println("エラー"); return; } String keyword = args[args.length - 1]; for (int i = 1; i < args.length; i++) { String filename = args[i]; System.out.println(filename); } } }
回答1件
あなたの回答
tips
プレビュー