文件File封装(读、写)

package com.exam.file;
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.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import android.os.Environment;
/**
 * 文件基本操作
 * 
 * 读取,写入
 * 
 * @author yxx
 *
 * 2015-10-8
 */
public class FileUtil {
/**
 * 判断设备是否有SD卡。
 * 有true,没有false。
 */
public static boolean existSDCard() {
return Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState());
} 
/**
 * fileNameSD卡目录下文件全路径名
 * 以行为单位读取文件,常用于读面向行的格式化文件
 * 这里读取按照GB2312的编码格式,防止中文乱码
 */
public static void readFileByLines(String fileName) {
File file = new File(Environment.getExternalStorageDirectory(), fileName);
BufferedReader reader = null;
try {
System.out.println("以行为单位读取文件内容,一次读一整行:");
//中文乱码处理
InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "GB2312");
reader = new BufferedReader(isr);
String tempString = null;
int line = 1;
// 一次读入一行,直到读入null为文件结束
while ((tempString = reader.readLine()) != null) {
// 显示行号
System.out.println("line " + line + ": " + tempString);
line++;
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e1) {
}
}
}
}
/**
 * 直接写入到SD卡根目录
 * 
 * @param data
 */
public static void writeToFile(String data) {
File file = new File(Environment.getExternalStorageDirectory(), "file.txt");//根目录下
BufferedWriter bw = null;
try {
FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile(),true);//true 为添加 不覆盖
bw = new BufferedWriter(new OutputStreamWriter (fos, "GB2312"));
bw.write(data);
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
 * 判断文件夹是否存在,
 * 不存在,自动创建
 * @param data
 */
public static void writeToFolderFile(String data){
File folder = new File(Environment.getExternalStorageDirectory() + "/alarm");
BufferedWriter bw = null;
if(!folder.exists()){// 如果文件夹不存在,创建一个
folder.mkdirs(); //这里要用.mkdirs()方法,父类文件夹不存在时,可以自动创建
}                  //而如果用.mkdir()方法则不会自动创建
File file = new File(folder.getAbsolutePath(), "file.txt");
try {
FileOutputStream fos = new FileOutputStream(file.getAbsoluteFile(),true);//true 为添加 不覆盖
bw = new BufferedWriter(new OutputStreamWriter (fos, "GB2312"));
//os = new FileOutputStream(file,false);//true 为添加 不覆盖
bw.write(data);
bw.flush();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

//调用范例
FileUtil.readFileByLines("GXDB.txt");
FileUtil.writeToFile("阿拉斯加发绿色wefwef\n");
FileUtil.writeToFolderFile("阿拉斯加发绿色wefwef\n");

//mainfest.xml 别忘了添加SD卡操作权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

分享文章:文件File封装(读、写)
转载源于:https://www.cdcxhl.com/article28/jcjpjp.html

成都网站建设公司_创新互联,为您提供网站维护全网营销推广品牌网站制作Google营销型网站建设建站公司

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联

小程序开发