public static String readFile(String storagePath){ //文件的读取 File newFile = new File(storagePath); if(newFile.exists()){ String res = ""; try{ File file = new File(storagePath); FileInputStream fin = new FileInputStream(file); //用这个就不行了,必须用FileInputStream int length = fin.available(); byte [] buffer = new byte[length]; fin.read(buffer); res = EncodingUtils.getString(buffer, "UTF-8"); fin.close(); }catch(Exception e){ e.printStackTrace(); } return res; }else { return null; } }
带换行符读取 Android 带换行符文本读取_xiaokakajk的博客-优快云博客
FileUtils.writeFile(path, new Gson().toJson(openDoorBean), false)
String 转 bean HumitureData humitureData = new Gson().fromJson(FileUtils.readFile(getFilePath()), HumitureData.class);
Type listType = new TypeToken<List<WeekTimetableBean.DataBean>>() {}.getType();
String path=ResManager.getInstance().getResRootPath(mContext)+File.separator+"newtimetable/json";
String timetableList = FileUtil.readFile(path);
try {
list = new Gson().fromJson(timetableList, listType );
}catch (IllegalStateException e){
e.printStackTrace();
}
FileUtils.writeFile(filepath, new Gson().toJson(weekTimetableBean.getData()), false)
public static boolean writeFile(String filename, String content, boolean append) {
boolean isSuccess = false;
BufferedWriter bufferedWriter = null;
try {
bufferedWriter = new BufferedWriter(new FileWriter(filename, append));
bufferedWriter.write(content);
isSuccess = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
closeIO(bufferedWriter);
}
return isSuccess;
}
/*try {
String fileName = "/touch_qr.txt";
String path = Environment.getExternalStorageDirectory()+"/touch";
ZipFile zf = new ZipFile(path+fileName);
for (Enumeration<?> entries = zf.entries(); entries.hasMoreElements(); ) {
ZipEntry entry = ((ZipEntry) entries.nextElement());
Log.e("==zip==",entry.getName());
File desFile = new File(path+"/json");
upZipFile(zf,entry,desFile);
String value = FileUtils.readUTF8FromFile(path+"/json");
Log.e("==zip==",value);
Intent intentqr = new Intent();
intentqr.setAction("net.bunnytouch.bunnyad.service.senttemperature_qr");
intentqr.putExtra("value", value + "");
QRService.this.sendBroadcast(intentqr);
QRData QR = new Gson().fromJson(value, QRData.class);
Log.e("==zip==",QR.getCredentialSubject().getAge());
}
}catch (IOException E){
}*/
return super.onStartCommand(intent, flags, startId);
}
//解压缩一个文件
private void upZipFile(ZipFile zf, ZipEntry entry, File desFile) throws IOException {
InputStream in = zf.getInputStream(entry);
if (!desFile.exists()) {
File fileParentDir = desFile.getParentFile();
if (!fileParentDir.exists()) {
fileParentDir.mkdirs();
}
if (!desFile.createNewFile()) {
Log.e("==zip==", "Create " + desFile.getAbsolutePath() + " failed!");
}
}
OutputStream out = new FileOutputStream(desFile);
byte buffer[] = new byte[1024 * 1024];
int realLength;
while ((realLength = in.read(buffer)) > 0) {
out.write(buffer, 0, realLength);
}
in.close();
out.close();
}