简介版
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
String string = "004-034556";
String[] parts = string.split("-");
String filePath = "D:/A.txt";
FileInputStream fin = new FileInputStream(filePath);
InputStreamReader reader = new InputStreamReader(fin);
BufferedReader buffReader = new BufferedReader(reader);
String strTmp = "";
while((strTmp = buffReader.readLine())!=null){
System.out.println(strTmp);
}
buffReader.close();
}
优化版
StringBuilder result = new StringBuilder();
try{
BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
String s = null;
while((s = br.readLine())!=null){//使用readLine方法,一次读一行
result.append(System.lineSeparator()+s);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}
return result.toString();
}
public static void main(String[] args){
File file = new File("D:/course.txt");
System.out.println(txt2String(file));
}
}