四套读写文件方案

                                                                                                                           四套读写文件方案 :实例

一:使用字节流读取文本文件

 //字节输入流练习:从文本文件读取各种数据(字母,字符串都支持)

   //声明流对象

try {

FileInputStream fis=new FileInputStream("c:\ming.txt");

int data;

System.out.println("可读取的字节数:"+fis.available());

System.out.println("文件内容为:");

//循环读取数据

byte[] buffer=new byte[1024];

StringBuilder sb=new StringBuilder();

while ((data=fis.read(buffer))!=-1) {

//byte[]转为字符串

String str=new String(buffer,0,data,"gb2312");

sb.append(str);

}

System.out.println(sb.toString());

} catch (Exception e) {

}

一:使用字节流写文本文件

 // 通过字节流将内存中的数据写入到硬盘上

FileOutputStream fos=null;

try {

String str="你是人间的四月天";

byte[] words=str.getBytes("gb2312");

//创建流对象,一追加方式写入文件

fos=new FileOutputStream("c:\ming.txt",true);

//写入文件

fos.write(words,0,words.length);

System.out.println("文件已更新");

} catch (Exception e) {

System.out.println("创建文件时出错!");

}finally{

try {

if (fos!=null) {

fos.close();

}

} catch (Exception e2) {

   e2.printStackTrace();

}

}

二:使用字符流读取文本文件

//使用字符流读取文本文件

Reader fr=null;

try {

 fr=new FileReader("c:\ming.txt");

char[] ch=new char[1024];//中转站,缓冲区

StringBuffer sbf=new StringBuffer();

int length=fr.read(ch);//将字符读入数组

//循环读取并追加字符

while (length!=-1) {

sbf.append(ch,0,length);

length=fr.read(ch);

}

System.out.print(sbf);

} catch (Exception e) {

   e.printStackTrace();

}finally{

try {

if (fr!=null) {

fr.close();

}

} catch (Exception e2) {

// TODO: handle exception

}

}

二:使用字符流写文本文件

//使用字符流写文本文件

FileWriter fw=null;

try {

fw=new FileWriter("c:\ming.txt",true);

//写入信息

String words="叶丽仪-上海滩";

fw.write(words);

fw.flush();

System.out.println("写入成功");

} catch (Exception e) {

System.out.println("文件不存在");

}finally{

try {

if (fw!=null) {

fw.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

三:字符输入流BufferedReader读取文件

FileReader fr=null;

BufferedReader br=null;

try {

//创建一个FileReader对象

fr=new FileReader("c:\ming.txt");

//创建一个BufferedReader对象

br=new BufferedReader(fr);

//读取一行数据

String line=br.readLine();

while (line!=null) {

System.out.println(line);

line=br.readLine();

}

} catch (Exception e) {

e.printStackTrace();

}finally{

try {

if (br!=null) {

br.close();

}if (fr!=null) {

fr.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

三: 使用MyBufferedWriter来写入文本文件

FileWriter fw=null;

BufferedWriter bw=null;

try {

fw=new FileWriter("c:\ming.txt",true);

bw=new BufferedWriter(fw);

//写入信息

bw.write("故乡的原风景");

bw.newLine();

bw.write("城里的月光-许美静");

bw.flush();

fw.close(); 

//读取文件内容

FileReader fr=new FileReader("c:\ming.txt");

BufferedReader br=new BufferedReader(fr);

String line=br.readLine();

while (line!=null) {

System.out.println(line);

line=br.readLine();

}

} catch (Exception e) {

System.out.println("文件不存在");

e.printStackTrace();

}finally{

try {

if (bw!=null) {

bw.close();

}

if (fw!=null) {

fw.close();

}

} catch (Exception e2) {

// TODO: handle exception

}

}

四:// 使用字节流类DataOutputStream写二进制文件

DataOutputStream out=null;

DataInputStream dis=null;

try {

//创建输入流对象

FileInputStream fis=new FileInputStream("c:\范宁.jpg");

dis=new DataInputStream(fis);

//创建输出流对象

FileOutputStream outFile=new FileOutputStream("c:\范宁小美女33.jpg");

out=new DataOutputStream(outFile);

int temp=dis.read();

while (temp!=-1) {

out.write(temp);

temp=dis.read();

}

System.out.println("复制成功");

fis.close();

outFile.close();

} catch (Exception e) {

System.out.println("文件不存在");

}finally{

try {

if (dis!=null) {

dis.close();

}

if (out!=null) {

out.close();

}

} catch (Exception e2) {

e2.printStackTrace();

}

}

原文地址:https://www.cnblogs.com/yangronglin/p/5564097.html