Skip to content

Instantly share code, notes, and snippets.

@sansuke05
Created September 1, 2017 00:17
Show Gist options
  • Select an option

  • Save sansuke05/dd7f6389d38041999664232f41e11e53 to your computer and use it in GitHub Desktop.

Select an option

Save sansuke05/dd7f6389d38041999664232f41e11e53 to your computer and use it in GitHub Desktop.
BufferedReaderを使ったFileの読み込みと文字列比較
import java.io.*;
public class ComparingStrings {
public static final String KEY = "hoge";
static void fuga(){ System.out.println("fuga called!"); }
public static void main(String[] args) {
//例としてファイルからBufferedReaderを使って読み込む方法
try{
File file = new File("hoge.txt"); //ファイル名、PATHも指定可能
FileReader fr = new FileReader(file);
BufferedReader bf = new BufferedReader(fr);
String str;
while ((str = bf.readLine()) != null) { //reallineで1行ごとに文字列を抽出
if (str.equals(KEY)) { //文字列の比較はequalsメソッドで判定
fuga();
}
}
bf.close();
} catch (FileNotFoundException e){
System.out.println("hoge.txt is not found!");
} catch (IOException e) {
System.out.println(e);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment