-------android培训、java培训、期待与您交流! ----------
1.字节流,字符流
2.输入流,输出流
字节流 的抽象基类 :
inputStream, OutputStream;
字符流的抽象基类 :
Reader Writer
Writer 就是要在内存中创建地址url 创建文件,好让
数据可以写入文件中
IO异常
class FileWriterDemo
{
public static void main(String args[])
{
try
{
FileWriter fw = new FileWriter("aa.txt");
fw.write("ssss");
fw.flush();
}
catch(IOException e)
{
System.out.println(e.toString());
}
finally
{
fw.close();
}
}
}
// 演示对已有文件的续写!
class FileWriterDemo
{
public static void main(String args[]) throws IOException
{
FileWriter fe = null;
fe = new FileWriter("a.txt",true);//在结尾加true是续写
fe.write("aaa");
fe.close();
System.out.println("");
}
}
//文本文件读取方式
class FileReadDemo
{
public static void main(String args[])
{
FileRead fr = new FileRead("a.txt");
fr.write("aaa");
while()
}
}
// 通过字符数组进行读取
class FileReadDemo
{
public static void main(String args[])
{
FileRead fr = new FileRead("a.txt");
char a[] = new char[4];
int num = fr.Read(a);
}
}
// 文本文件读取,读取java文件
/*
在D盘创建文件,用于存储C盘文件的内容
*/
class CopyDemo
{
public static void main(String args[])
{
}
public static copy_2()
{
try
{
FileWriter fw = new FileWriter("System_copy.txt");
FileReader fr = new FileReader("System。java");
char buf[] = new char[1024];
int sum =0;
while((sun=fr.read(buf))!-1)
{
fw.write();
}
}
catch(IOException e)
{
throw new RuntimeException("");
}
finally
{
if(fr!=null)
try
{
fr.close();
}
catch(IOException e)
{
}
if(fw!=null)
try
{
fw.close();
}
catch(IOException e)
{
}
}
}
}
《========================缓冲区BufferWriter======================》
public class BufferWriterDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//创建一个字符写入流对象,在所在的盘符就会自动创建文件
FileWriter fw = new FileWriter("f:\\a.txt");
//将写入流对象以参数的形式放入缓冲区域
BufferedWriter bufw = new BufferedWriter(fw);
//
for(int x =1;x<2;x++){
bufw.write("春蟬到死絲方盡,蠟炬成灰淚始幹,晓镜但愁云鬓改,夜吟应觉月光寒");
bufw.newLine();
bufw.flush(); //刷新缓冲
}
bufw.close(); // 记住要关闭缓冲流
fw.close(); //关闭写入流
System.out.println("aaa");
}
}
《=========================创建文件,读取文件==================》
public class BufferedReaderDemo {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileWriter wf = new FileWriter("f://wangbin.txt"); //创建文件向文件中写入字符;
//wf.write("前程似锦,富甲一方,名垂青史");
BufferedWriter buw = new BufferedWriter(wf); //记住一定要刷新,否则字符不会写入文件中
buw.write("前程似锦,富甲一方,名垂青史");
buw.newLine();
buw.flush();
FileReader fr = new FileReader("f://wangbin.txt");//要知道读取文件的路径,加入缓冲区,读取文件
BufferedReader bur = new BufferedReader(fr);//加入缓冲区
String s = bur.readLine(); //读取一行
System.out.println(s);
}
}
《=======================复制MP3 文件 并求得复制所用的时间========================》
public class Mp3CopyBuffer {
/**
* @param args
* copy MP3 BuffeedOutputStream
* BufferedInputStream
*
* 复制MP3 文件 并求得复制所用的时间
*/
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
long start = System.currentTimeMillis();
copy_1();
long end = System.currentTimeMillis();
System.out.print("复制时候所需要的时间time="+(end-start));
}
//通过字节流的缓冲区完成复制
public static void copy_1()throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("f://1.mp3"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("f://2.mp3"));
int by = 0;
while((by=bis.read())!=-1){
bos.write(by);
}
bos.flush();
bos.close();
}
}
《======================================CopyRW=========================》
public class CopyRW {
/**
* @param args
* @throws IOException
* @throws IOException
* 读取a.java文件中的字符写入到b.txt文件中,其中通过缓冲流进行读取
*/
public static void main(String[] args) throws IOException {
//FileReader 用于读取字符流,
FileReader fr = new FileReader("f://a.java");
FileWriter fw = new FileWriter("f://b.txt");
BufferedReader bur = new BufferedReader(fr);
//BufferedReader bur = new BufferedReader(new FileWriter("f://b.txt")); 效果是相同的
BufferedWriter buw = new BufferedWriter(fw);
String a =null;
String b = null;
while( (a=(bur.readLine()))!=null){
System.out.println(b=a.toString().replaceAll(",", "\n"));
buw.write(b);
buw.flush();
buw.close();
}
}
}
《==============================CopyPicture--复制图片======================》
-------android培训、java培训、期待与您交流! ----------
详细请查看:http://edu.youkuaiyun.com/heima/