新建个工具类,并标记成静态的,方便调用。
package util;
import java.io.File;
import java.io.FileOutputStream;
public class FileUtil {
public static boolean inputFile(String strResponse){
boolean bl = false;
File file = new File("e:\\test.txt");
//输入流
FileOutputStream fop;
try {
fop = new FileOutputStream(file);
if(!file.exists()){
file.createNewFile();
}
//将字符串转成字节
byte[] contentInBytes = strResponse.toString().getBytes();
//写入文件
fop.write(contentInBytes);
fop.flush();
//释放
fop.close();
bl = true;
} catch (Exception e) {
e.printStackTrace();
}
return bl;
}
}