热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

文件操作工具类和字符串判断是否为空

importandroid.app.Activity;importandroid.content.Context;importandroid.graphics.Bitmap;importand
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.os.Environment;
import android.support.annotation.Nullable;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;

/**
* Created by yanchengmeng on 16/12/9.
*/
public class FileUtil {

/**
* 保存图片到本地
*
* @param bitmap
* @param path
* @param fileName
* @return 保存的路径,如果为null表示保存失败
*/
public static String saveBitMap(Bitmap bitmap, String path, String fileName) {

if (bitmap == null || path == null || fileName == null) {
return null;
}

File storePath = new File(path);

String result = saveBitMap(bitmap, storePath, fileName);

return result;
}

/**
* 保存图片到本地
*
* @param bitmap
* @param path
* @param fileName
* @return
*/
public static String saveBitMap(Bitmap bitmap, File path, String fileName) {

if (bitmap == null || path == null || fileName == null) {
return null;
}

if (!path.exists()) {
if (!path.mkdirs()) {
return null;
}
}

File destFile = new File(path, fileName);
String result = destFile.getAbsolutePath();
OutputStream os = null;
try {
os = new FileOutputStream(destFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (IOException e) {
result = null;
} finally {
if (os != null) {
try {
os.close();
} catch (IOException e) {
}
}
}
return result;
}

/**
* 刷新图片到相册中
*
* @param context
* @param path
*/
public static void mediaRefresh(Activity context, File path) {
MediaScannerConnection.scanFile(context, new String[]{path.getAbsolutePath()}, null, null);
}

/**
* 刷新图片到相册中
*
* @param context
* @param path
*/
public static void mediaRefresh(Activity context, String path) {
MediaScannerConnection.scanFile(context, new String[]{path}, null, null);
}

/**
* 从指定文件中读取String
*
* @param fileName
* @return
*/
public static String readFromFile(File fileName) {

StringBuilder sb = new StringBuilder();
BufferedReader reader = null;

try {
reader = new BufferedReader(new FileReader(fileName));

String tempString;

while ((tempString = reader.readLine()) != null) {
sb.append(tempString);
}
} catch (IOException e) {
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
}
}

return sb.toString();
}

/**
* 把String写入指定文件
*
* @param fileName
* @param text
*/
public static void writeToFile(File fileName, String text) {

BufferedWriter writer = null;

try {
writer = new BufferedWriter(new FileWriter(fileName));

writer.write(text);
writer.flush();

} catch (IOException e) {
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
}
}

}

/**
* 复制单个文件
*
* @param oldPath String 原文件路径 如:c:/fqf.txt
* @param newPath String 复制后路径 如:f:/fqf.txt
* @return boolean
*/
public static void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在时
InputStream inStream = new FileInputStream(oldPath); //读入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字节数 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
} catch (Exception e) {
System.out.println("复制单个文件操作出错");
e.printStackTrace();
}
}

/**
* 使用文件通道的方式复制文件
*
* @param s 源文件
* @param t 复制到的新文件
*/

public static void copyFile(File s, File t) {

FileInputStream fi = null;

FileOutputStream fo = null;

FileChannel in = null;

FileChannel out = null;

try {

fi = new FileInputStream(s);

fo = new FileOutputStream(t);

in = fi.getChannel();//得到对应的文件通道

out = fo.getChannel();//得到对应的文件通道

in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道

} catch (IOException e) {

e.printStackTrace();

} finally {

try {
fi.close();

in.close();

fo.close();

out.close();

} catch (IOException e) {
e.printStackTrace();
}
}
}

public static String getCacheDir(Context context) {
String cacheDir = null;
try {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| !Environment.isExternalStorageRemovable()) {
cacheDir = context.getExternalCacheDir().getPath();
if (StringUtil.isEmpty(cacheDir)) {
cacheDir = context.getCacheDir().getPath();
}
} else {
cacheDir = context.getCacheDir().getPath();
}
} catch (Exception e) {

}
if (StringUtil.isEmpty(cacheDir)){
return context.getCacheDir().getPath();
}
return cacheDir;
}

@Nullable
public static File getExternalCacheDir(Context context) {
try {
return context.getExternalFilesDir(null);
} catch (Exception e){
return null;
}
}


public static boolean saveBitmap(Bitmap bm, String picName) {
try {
File f = new File(picName);
if (f.exists()) {
f.delete();
}
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
return true;
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}

public static boolean fileExist(String path, String fileName) {
File file = new File(path);
if (!file.exists()) {
return false;
}
if (!file.isDirectory()) {
return false;
}

String[] tempList = file.list();
for (int i = 0; i if (fileName.equals(tempList[i])) {
return true;
}
}
return false;

}


public static void delAllFile(String path) {
File file = new File(path);
if (!file.exists()) {
return;
}
if (!file.isDirectory()) {
return;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i if (path.endsWith(File.separator)) {
temp = new File(path + tempList[i]);
} else {
temp = new File(path + File.separator + tempList[i]);
}
if (temp.isFile()) {
temp.delete();
}
if (temp.isDirectory()) {
delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
delFolder(path + "/" + tempList[i]);//再删除空文件夹
}
}
}

//删除文件夹
public static void delFolder(String folderPath) {
try {
delAllFile(folderPath); //删除完里面所有内容
String filePath = folderPath;
filePath = filePath.toString();
File myFilePath = new File(filePath);
myFilePath.delete(); //删除空文件夹
} catch (Exception e) {
e.printStackTrace();
}
}

public static boolean isEmpty(String str) {
if (null == str || str.trim().equals("") || str.trim().equalsIgnoreCase("null")) {
return true;
}
return false;
}

}
   

推荐阅读
  • 本文介绍了在Android开发中使用软引用和弱引用的应用。如果一个对象只具有软引用,那么只有在内存不够的情况下才会被回收,可以用来实现内存敏感的高速缓存;而如果一个对象只具有弱引用,不管内存是否足够,都会被垃圾回收器回收。软引用和弱引用还可以与引用队列联合使用,当被引用的对象被回收时,会将引用加入到关联的引用队列中。软引用和弱引用的根本区别在于生命周期的长短,弱引用的对象可能随时被回收,而软引用的对象只有在内存不够时才会被回收。 ... [详细]
  • 本文讨论了如何优化解决hdu 1003 java题目的动态规划方法,通过分析加法规则和最大和的性质,提出了一种优化的思路。具体方法是,当从1加到n为负时,即sum(1,n)sum(n,s),可以继续加法计算。同时,还考虑了两种特殊情况:都是负数的情况和有0的情况。最后,通过使用Scanner类来获取输入数据。 ... [详细]
  • 本文主要解析了Open judge C16H问题中涉及到的Magical Balls的快速幂和逆元算法,并给出了问题的解析和解决方法。详细介绍了问题的背景和规则,并给出了相应的算法解析和实现步骤。通过本文的解析,读者可以更好地理解和解决Open judge C16H问题中的Magical Balls部分。 ... [详细]
  • Android工程师面试准备及设计模式使用场景
    本文介绍了Android工程师面试准备的经验,包括面试流程和重点准备内容。同时,还介绍了建造者模式的使用场景,以及在Android开发中的具体应用。 ... [详细]
  • 本文讨论了在VMWARE5.1的虚拟服务器Windows Server 2008R2上安装oracle 10g客户端时出现的问题,并提供了解决方法。错误日志显示了异常访问违例,通过分析日志中的问题帧,找到了解决问题的线索。文章详细介绍了解决方法,帮助读者顺利安装oracle 10g客户端。 ... [详细]
  • Android自定义控件绘图篇之Paint函数大汇总
    本文介绍了Android自定义控件绘图篇中的Paint函数大汇总,包括重置画笔、设置颜色、设置透明度、设置样式、设置宽度、设置抗锯齿等功能。通过学习这些函数,可以更好地掌握Paint的用法。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 本文介绍了一种解析GRE报文长度的方法,通过分析GRE报文头中的标志位来计算报文长度。具体实现步骤包括获取GRE报文头指针、提取标志位、计算报文长度等。该方法可以帮助用户准确地获取GRE报文的长度信息。 ... [详细]
  • 开发笔记:实验7的文件读写操作
    本文介绍了使用C++的ofstream和ifstream类进行文件读写操作的方法,包括创建文件、写入文件和读取文件的过程。同时还介绍了如何判断文件是否成功打开和关闭文件的方法。通过本文的学习,读者可以了解如何在C++中进行文件读写操作。 ... [详细]
  • 本文讨论了在手机移动端如何使用HTML5和JavaScript实现视频上传并压缩视频质量,或者降低手机摄像头拍摄质量的问题。作者指出HTML5和JavaScript无法直接压缩视频,只能通过将视频传送到服务器端由后端进行压缩。对于控制相机拍摄质量,只有使用JAVA编写Android客户端才能实现压缩。此外,作者还解释了在交作业时使用zip格式压缩包导致CSS文件和图片音乐丢失的原因,并提供了解决方法。最后,作者还介绍了一个用于处理图片的类,可以实现图片剪裁处理和生成缩略图的功能。 ... [详细]
  • 先看官方文档TheJavaTutorialshavebeenwrittenforJDK8.Examplesandpracticesdescribedinthispagedontta ... [详细]
  • 本文介绍了Swing组件的用法,重点讲解了图标接口的定义和创建方法。图标接口用来将图标与各种组件相关联,可以是简单的绘画或使用磁盘上的GIF格式图像。文章详细介绍了图标接口的属性和绘制方法,并给出了一个菱形图标的实现示例。该示例可以配置图标的尺寸、颜色和填充状态。 ... [详细]
  • 合并列值-合并为一列问题需求:createtabletab(Aint,Bint,Cint)inserttabselect1,2,3unionallsel ... [详细]
  • 本文介绍了在Ubuntu下制作deb安装包及离线安装包的方法,通过备份/var/cache/apt/archives文件夹中的安装包,并建立包列表及依赖信息文件,添加本地源,更新源列表,可以在没有网络的情况下更新系统。同时提供了命令示例和资源下载链接。 ... [详细]
  • 上图是InnoDB存储引擎的结构。1、缓冲池InnoDB存储引擎是基于磁盘存储的,并将其中的记录按照页的方式进行管理。因此可以看作是基于磁盘的数据库系统。在数据库系统中,由于CPU速度 ... [详细]
author-avatar
cb
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有