android 把字符串内容保存到指定路径
来源:互联网 发布:格格广场舞服装淘宝店 编辑:程序博客网 时间:2024/06/07 14:18
---------------------------------------------------------
》保存到外部SD卡中:
- public static void saveFile(String str) {
- String filePath = null;
- boolean hasSDCard = Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
- if (hasSDCard) {
- filePath = Environment.getExternalStorageDirectory().toString() + File.separator + "hello.txt";
- } else
- filePath = Environment.getDownloadCacheDirectory().toString() + File.separator + "hello.txt";
- try {
- File file = new File(filePath);
- if (!file.exists()) {
- File dir = new File(file.getParent());
- dir.mkdirs();
- file.createNewFile();
- }
- FileOutputStream outStream = new FileOutputStream(file);
- outStream.write(str.getBytes());
- outStream.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
-------------------------------------------------------------------------------
》保存到App内部路径:
/*** 从缓存中读取工作平台数据
*/
public static String getWorktableBeanFromCache(Context context){
try {
File file = new File(context.getCacheDir().getAbsolutePath()+File.pathSeparator+"workPlatform");
if(!file.exists() || !file.canRead())return null;
long lastModified = file.lastModified();
if((System.currentTimeMillis() - lastModified)>Constant.FILE_CACHE_EXPIRES_HOUR*3600*1000){
file.delete();
return null;
}
FileInputStream fin = new FileInputStream(file);
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
StringBuilder sb = new StringBuilder("");
String temp ;
while((temp=br.readLine())!=null){
sb.append(temp);
}
br.close();
fin.close();
LogManagerControl.ShowLog("tssc",sb.toString(), "i");
// sb=null;
return sb.toString();
} catch (Exception e) {
}
return null;
}
/**
* 向缓存中写入工作平台数据
*/
public static void writeWorktableBeanToCache(Context context,String json){
try {
if(TextUtils.isEmpty(json)){
return;
}
File file = new File(context.getCacheDir().getAbsolutePath()+File.pathSeparator+"workPlatform");
if(file.exists()){
file.delete();
}
if(file.createNewFile()){
FileOutputStream out = new FileOutputStream(file);
out.write(json.getBytes());
out.flush();
out.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
------------------------------------------------------------------------------------------------
》Android默认的文件保存
String FILENAME = "hello_file"; String string = "hello world!"; FileOutputStream fos; try { fos = openFileOutput(FILENAME, Context.MODE_PRIVATE); fos.write(string.getBytes()); fos.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } FileInputStream in = null; try { in = openFileInput("hello_file.txt"); StringBuffer fileContent = new StringBuffer(""); byte[] buffer = new byte[1024]; while(in.read(buffer) != -1) { fileContent.append(new String(buffer)); } finall = fileContent.toString(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
》》》》/**
* 从文件缓存中读取工作平台数据
*/
public static String getPlatformsData(Context context) {
FileInputStream in = null;
StringBuffer fileContent;
try {
in = context.openFileInput(mFileName);
fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
fileContent.append(new String(buffer));
}
in.close();
return fileContent.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return "";
}
return "";
}
private static String mFileName = "executive_platform_data";
/**
* 向缓存中写入工作平台数据
*/
public static void savePlatformsData(Context context, String json) {
if (StringUtils.isEmpty(json)) {
return;
}
File file = new File(context.getFilesDir(), mFileName);
if (!file.exists()) {
file.mkdir();
}
// 如果没有指定访问的模式 ,文件的模式 默认是私有的权限.
// 只有当前的应用程序可以读写这个文件 ,别的应用程序是不可以操作这个文件.
try {
// FileOutputStream fos = new FileOutputStream(file);
FileOutputStream fos;
fos = context.openFileOutput(mFileName, Context.MODE_PRIVATE);
LogManagerControl.ShowLog("FileOperatUtils", "数据被写入了!!", "V");
fos.write(json.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
* 从文件缓存中读取工作平台数据
*/
public static String getPlatformsData(Context context) {
FileInputStream in = null;
StringBuffer fileContent;
try {
in = context.openFileInput(mFileName);
fileContent = new StringBuffer("");
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
fileContent.append(new String(buffer));
}
in.close();
return fileContent.toString();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
return "";
}
return "";
}
private static String mFileName = "executive_platform_data";
/**
* 向缓存中写入工作平台数据
*/
public static void savePlatformsData(Context context, String json) {
if (StringUtils.isEmpty(json)) {
return;
}
File file = new File(context.getFilesDir(), mFileName);
if (!file.exists()) {
file.mkdir();
}
// 如果没有指定访问的模式 ,文件的模式 默认是私有的权限.
// 只有当前的应用程序可以读写这个文件 ,别的应用程序是不可以操作这个文件.
try {
// FileOutputStream fos = new FileOutputStream(file);
FileOutputStream fos;
fos = context.openFileOutput(mFileName, Context.MODE_PRIVATE);
LogManagerControl.ShowLog("FileOperatUtils", "数据被写入了!!", "V");
fos.write(json.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
0 0
- android把字符串内容保存到指定路径
- android 把字符串内容保存到指定路径
- Android保存图片到指定路径
- 上传图片把缩略图保存到指定的虚拟路径
- 怎样把图像保存到指定的路径里
- 把图片保存到SD卡指定的路径并返回改路径
- C++怎样把内容写入指定路径?
- 简单的C# 保存内容到指定路径下的XML文件中方法
- 把某一路径内文件夹里的内容复制到指定位置
- 保存Log4j日志文件到指定路径
- 图片保存到指定的路径
- 获取Bitmap保存到指定文件夹路径
- C语言把内存的文件保存到硬盘时如何把文件名由用户通过屏幕自己设置,并且保存到指定的路径
- iOS 保存图片到相册, 保存到指定的路径
- linux 把文件安装到指定路径
- java获取当前类的路径并且把本类的内容全部输出到指定位置
- 将字符串内容保存到本地文件中
- php上传图片到指定位置路径保存到数据库
- 八大排序算法
- (四)、解决"复制出来的虚拟机不能启动 DataNode"
- Java Json数据解析
- vim 复制、删除多行
- VMware Fusion For Mac v5.0.1中文版
- android 把字符串内容保存到指定路径
- 关于finish() onDestroy() 和System.exit()的区别
- i7OS在真机调试无法全屏显示,模拟器正常
- 蜂鸣器驱动电路
- (五)、HDFS 简介
- HDU【2602】Bone
- Android 版本AGC调节
- BezierPath
- iOS地图 很多坐标点 怎样显示所有坐标点 并使屏幕居中