一 、将字符串写入文本
方法一
/**
* 使用FileWriter
* @param str
* 问题说明 : 每次写入内容都会覆盖 原本文件中的内容 适合一次写入
* 格式一 : fileWriter = new FileWriter(filePath); 文件内容会覆盖
* 格式一 : fileWriter = new FileWriter(filePath,true); 可以追加写入文件 追加内容不换行
*/
public static void strWrite1(String str,String filePath){
FileWriter fileWriter;
try {
// 文件无需创建 也能写入成功
fileWriter = new FileWriter(filePath);
// fileWriter.write(""); // 清空源文件内容
fileWriter.write(str);
fileWriter.close();
System.out.println("写入成功");
} catch (IOException e) {
e.printStackTrace();
System.out.println("写入失败");
}
}
方法二
/**
* 使用FileOutPutStream
* 说明 :
* 格式一 : FileOutputStream fos = new FileOutputStream("文件名"); 文件内容会覆盖
* 格式一 : FileOutputStream fos = new FileOutputStream("文件名",true); 可以追加写入文件 追加内容不换行
*/
public static void strWrite2(String str,String filePath){
try {
// 文件无需创建 也能写入成功 自动创建文件
FileOutputStream fos = new FileOutputStream(filePath);
byte[] bytes = str.getBytes();
fos.write(bytes,0,bytes.length);
// fos.write(str.getBytes(),0,str.getBytes().length);
System.out.println("写入成功");
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("写入失败");
} catch (IOException e) {
e.printStackTrace();
System.out.println("写入失败");
}
}
方法三
/**
* 打印输出流
* @param str
* @param filePath
* 说明 : 可以追加写入文件 追加内容可以换行
*/
public static void strWrite3(String str,String filePath) {
try {
File file = new File(filePath);
PrintStream ps = new PrintStream(new FileOutputStream(file));
ps.println(str);// 往文件里写入字符串
// ps.append("http://www.jb51.net");// 在已有的基础上添加字符串
System.out.println("写入成功");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("写入失败");
}
}
二 、将文件内容写入字符串
方法一
/**
* 以字节为单位读取文件,常用于读二进制文件,如图片、声音、影像等文件。
* 当然也是可以读字符串的。
*
*/
public static String strRead1(String filePath){
//FileInputStream 用于读取诸如图像数据之类的原始字节流。要读取字符流,请考虑使用 FileReader。
FileInputStream inputStream = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
inputStream = new FileInputStream(filePath);
byte[] bytes = new byte[1024];
int length = -1;
while((length = inputStream.read(bytes)) != -1){
bos.write(bytes,0,length);
}
bos.close();
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bos.toString();
}
方法二
/**
* FileReader 一个字一个字的读
* @param filePath
* @return
*/
public static String strRead2(String filePath){
StringBuffer stringBuffer = new StringBuffer("");
File file = new File(filePath);
try {
FileReader fileReader = new FileReader(file);
int ch = 0;
while ((ch = fileReader.read())!=-1){
stringBuffer.append((char)ch + "");
}
fileReader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return stringBuffer.toString();
}
方法三
/**
* FileReader 设置字节数组的长度 一次性读取
* @param filePath
* @return
*/
public static String strRead3(String filePath){
String result = "";
File file = new File(filePath);
try {
FileInputStream fileInputStream = new FileInputStream(file);
int size = fileInputStream.available(); // size 为字串的长度 ,这里一次性读完
byte[] bytes = new byte[size];
fileInputStream.read(bytes);
fileInputStream.close();
result = new String(bytes);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
方法四
/**
* InputStreamReader+BufferedReader读取字符串 , InputStreamReader类是从字节流到字符流的桥梁
* @param filePath
* @return
* 按行读对于要处理的格式化数据是一种读取的好方式
*/
public static String strRead4(String filePath) {
int len=0;
StringBuffer str=new StringBuffer("");
File file=new File(filePath);
try {
FileInputStream is=new FileInputStream(file);
InputStreamReader isr= new InputStreamReader(is);
BufferedReader in= new BufferedReader(isr);
String line=null;
while( (line=in.readLine())!=null )
{
if(len != 0) // 处理换行符的问题
{
str.append("\r\n"+line);
}
else
{
str.append(line);
}
len++;
}
in.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return str.toString();
}