---------------------------------------------------------
》保存到外部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();
}
}