天天看点

java IO流(字节流与字符流对文件的处理)

import java.io.*;

public class FileWriterDemo {

public static void main(String[] args) throws IOException {

//writerDemo();

//FileOutputStreamDemo();

}

//字节流对文件进行读写

public static void FileOutputStreamDemo()

{

//定义字节数组

byte[] b1={'b','c','a'};

//定义字节流对象

FileOutputStream fos=null;

try{

//给字节流对象创建要操作的文本

//如果该文件已存在,将会将其覆盖。

//如果在构造函数里传递一true参数,代表不覆盖已有的文件。并在已有的文件的末尾,续写文件。

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

//调用write方法将字节数组里的字符写入字节文件对象里。

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

}

catch (IOException e){

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

}

finally{

try {

//关闭流

fos.close();

} catch (Exception e2) {

// TODO: handle exception

}

}

}

//字符文件操作流

  public static void writerDemo()

{

//1.创建一个FileWriter对象,对象已初始化必须确定要操作的文件(创建文本),

//如果该文件已存在,将会将其覆盖。

//如果在构造函数里传递一true参数,代表不覆盖已有的文件。并在已有的文件的末尾,续写文件。

FileWriter fw=null;

try{

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

//2.调用流操作对象的write方法将字符串写入流中。

//在windos文本下,不能识别\n为换行符,只能识别\r\n为换行符。

fw.write("ljwer\r\noiusdjfldsklollolololol");

//3.刷新流对象中缓冲的数据,将数据刷到目的文件中。

fw.flush();

//4.刷新流对象中缓冲的数据,将数据刷到目的文件中,并且关闭流。

}

catch(IOException e)

{

System.out.println(e);

}

try {

if(fw!=null)

fw.close();

} catch (IOException e1) {

System.out.println(e1);

}

//表示流中不能再写入数据,会报出异常。

//fw.write("收到两份洪武偶额我家附近");

//fw.flush();

}

}

import java.io.*;

public class FileReaderDemo {

public static void main(String[] args) throws IOException {

//FileOutPutStreamDemo();

//FileReaderDemo()

}

//字节流对文件的读取。

public static void FileOutPutStreamDemo() throws IOException

{

//定义一个字节数组。

byte[] b1=new byte[1024];

//定义字节流读取对象,并且确定要操作的文件,要保证该文件是已经存在的,如果不存在,会发生异常。

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

int num=0;

//调用read方法对fis进行读取,返回文件对象里字节数的总合。

while((num=fis.read(b1))!=-1)

{

//不断的对文件里的字节数进行读取,当读取完之后,将其转换成字符串进行打印,最后进行最后一轮读取

//读取空字节,返回-1,跳出循环。

sop(num);

sop(new String(b1,0,num));

}

}

public static void FileReaderDemo() throws IOException

{

//创建一个文件读取流对象,和指定名称的文件相关。

//要保证该文件是已经存在的,如果不存在,会发生异常。

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

//第二种方式:通过字符数组进行读取。

//定义一个字符数组,用于存储读到字符。

//该read(char[])返回的是读到的字符个数。

char[] buf=new  char[1024];

int num=0;

while((num=fr.read(buf))!=-1)

{

sop(num);

sop(new String(buf,0,num));

}

//第一种读取方式

}

public static<T> void sop(T t)

{

System.out.println(t);

}

}

import java.io.*;

public class FileWriterDemo2 {

public static void main(String[] args){

//定义写入字节流对象

FileWriter fw=null;

try{

//给字节流对象创建要操作的文本

//如果该文件已存在,将会将其覆盖。

fw=new FileWriter("c:\\demo.txt");

  fw.write("rwerwe");

}

catch(IOException e)

{

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

}

finally{

try {

if(fw!=null)

fw.close();

} catch (IOException e2) {

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

}

}

}

}

//练习:文本复制。

import java.io.*;

public class CopyText {

public static void main(String[] args) throws IOException {

//copy();

copy1();

}

public static void copy1()

{

FileWriter fw=null;

FileReader fr=null;

try{

//

fw=new FileWriter("c:\\demo.txt");

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

char buf[]=new char[1024];

int length=0;

while ((length=fr.read(buf))!=-1)

{

fw.write(buf,0,length);

}

}

catch(IOException e){

throw new  RuntimeException("读写失败");

}

finally{

try{

if(fw!=null)

fw.close();

}

catch(IOException e)

{}

finally{

if(fr!=null)

try{

fr.close();

}

catch(IOException e)

{}

}

}

}

public static void copy() throws IOException 

{

FileWriter fw=new FileWriter("c:\\demo.txt");

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

int num=0;

while ((num=fr.read())!=-1)

{

fw.write(num);

}

fw.close();

fr.close();

}

public static<T> void sop(T t)

{

System.out.print(t);

}

}

继续阅读