java程序中读取文件操作
/**
* 创建本地文件
* */
public File CreateFile(String path) throws IOException {
File tokenFile = new File(path);
if (!tokenFile.exists()){
tokenFile.createNewFile();
}
return tokenFile;
}
InputStreamReader reader = new InputStreamReader(new FileInputStream(path));
BufferedReader bufferedReader = new BufferedReader(reader);
/*StringBuffer可变*/
StringBuffer token = new StringBuffer();
String line = "";
while ( (line = bufferedReader.readLine())!=null) {
token.append(line);
}
System.out.println("获取的token:"+token.toString());
- 本地的文件可以直接访问,但是放到tomact上就找不到文件,我的解决方法是,将文件放到resource下面并用classloder去读取
String path = CaptchaUtils.class.getClassLoader().getResource("xxx.txt").getPath();