文件的读:
synchronized static void writeFile(String filePath, String sets) {
try {
FileWriter fw = new FileWriter(filePath, false);
PrintWriter out = new PrintWriter(fw);
out.write(sets);
out.println();
fw.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
文件的写入:
static String readFile(String path) {
File file = new File(path);
BufferedReader reader = null;
String laststr = "";
try {
reader = new BufferedReader(new FileReader(file));
String tempString = null;
while ((tempString = reader.readLine()) != null) {
laststr = laststr + tempString;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
return laststr.equals("") ? "{}" : laststr;
}
流向字符串的转换:
static String convertStreamToString(InputStream inputStream) { //流转为字符串
String convertString = "";
try {
convertString = IOUtils.toString(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
return convertString;
}