Java-io流

文件字输入流读取文件操作步骤∶

1.建立一个流对象,将已存在的一个文件加载进流。FileInputStream fr= new FileInputStream("Test.txt");

2.创建一个临时存放数据的数组。byte[] b = new byte[1024];

3.调用流对象的读取方法将流中的数据读入到数组中。fr.read(b);

文件字节输入流

public static void main(String[] args){

try{



FileInputStream in new FileInputStream("Text.txt");

byte[] b = new byte[1024];

int len = 0;



while((len = in.read(b))!= -1){

System.out.println (new String (b,0,len));

}



in.close ();



} catah (Exception e){

e.printStackTrace ();

}

}

len = in.read(b)

该行代码的作用是将in流中的数据读取出来,并依次存储到数组b中,返回值为实际读取的有效数据的个数。

int n = in.read();

该方法的作用是每次读取流中的一个字节,当到达流的末尾时,read方法的返回值是-1

文件字写入文件操作步骤∶

1.建立一个流对象,将已存在的一个文件加载进流。

FileOutputStream out = new FileOutputStzeam("Text.txt");

2.out.write (str.getBytes ()); out.flush ();

文件字节输出流

public static void main (String[] azgs){

try {



FileOutputStream out = new FileOutputStzeam("Text.txt");

String str = "zbcsssssss";



out.write (str.getBytes ());

out.flush ();

out.close ();



} catch(Exception e){

e.printStackTrace ();

}

}

文件复制

public static void copyFile(){

try {

FileInputStream in - new F11eInputStream("Text.txe");//读取的源文件

FileOutputStream out- new FileOutputStream("Text.txt");//复制到哪里



byte[] b = new byte[100];

int len = 0;



while((len = in.read(b))!= -1){

out.write(b,0,len);

//参数1是写的缓冲数组,参数2是从数组的那个位置开始,参数3是获取的数组的总长度;

}



out.flush();//把写到内存的数据刷到硬盘



out.close();

in.close();



} catch(Exception e){

e.printStackTzace();

}

}

注意:

文件字节流非常通用,可以用来操作字符的文档,还可以操作任何其它类型文(图片,压缩包等等),引用字节流直接使用二进制

文件字符输入流读取文件操作步骤∶

1.建立一个流对象,将已存在的一个文件加载进流。FileReader fr= new FileReader("Test.txt");

2.创建一个临时存放数据的数组。char[] ch = new char[1024];

3.调用流对象的读取方法将流中的数据读入到数组中。fr.read(ch);

文件字输出流

public static void testFileReader(StrAng inPath){

try{



FileReader fr = new FileReader(inPath);//创建文件字符输入流的对象



char[] c = new char[10];//创建临时存数据的字符数组

int 1en = 0;/定义一个输入流的读取长度



while((len = fr.read(c))!= -1){

System.out.println(new String(c,0,len));

}



fr.lose();//关闭流



} catch(Exception e)(

e.printStackTrace();

}

}

文件字符输出流

/**

* @eparam text 输出的内容

* @eparam outPath 输出的文件

*/



publia static void testFileWriter(String text,String outPath){

try {



FileWziter fw = new FileWriter(outPath);



fw.write(text)∶//写到内存中

fw.flush();//把内存的数据刷到硬盘



fw.close ();//关闭流



} catch (Exception e) {

e.printStackTrace ();

}

}

注 意

定义文件路径时,注意∶可以用"/"或者"\\"。

在写入一个文件时,如果目录下有同名文件将被覆盖。

在读取文件时,必须保证该文件已存在,否则出异常。

缓冲流

FilelnputStream,FileOutPutStream

FileReader,FileWriter

这些都计算机与硬盘之间发生的io操作,基于硬

对是比较慢,这个操作的速度受到硬盘的读写速度的制约,为了能够提供读写速度. 一定程度上绕过硬盘的限制,java 提供一种缓冲流来实现

public static void testBufferedInputStream() throws Exception(

//文件字节输入流对象

FileInputStream in=new FlleInputStream(“Text.txt”);

//把文件字节输入流放到缓冲字节输入流对象

BufferedInputStream br = new BufferedInputStream (in);



byte[] b = new byte[10];

int len = 0;



while((len = br,read(b))!- -1){

System.out.println (new String (b,0,len));

}



//关闭流的时候,本着一个最晚开的最早关,依次关

br.close():

in.close();

}

publio static void testBufferedOutputStream()throws Exception{

//创建字节输出流对象

FileOutputStream out=new FileOutputStream(“Text.txt”);

//把字节输出流对象放到缓冲字节输出流中

BufferedOutputStream bo = new BufferedOutputStream(out);



String s = "hello world";



bo.write(s.getBytes());//写到内存中,转化成流可接受的byte型

bo.flush ();//刷到硬盘上



//关闭流的时候,最晚开的最早关,依次关

bo.close();

out.close ();

}

转换流

转换流提供了在字节流和字符流之间的转换Java API提供了两个转换流∶

InputStreamReader和OutputStreamWriter

字节流中的数据都是字符时,转成字符流操作更高效。

InputStreamReader

用于将字节流中读取到的字节按指定字符集解码成字符。需要和InputStream"套接"。

FlleInputStream fs = new FileInpuqStream("Text.txt");

InputStreamReader in = new InputStreamReader(fs."GBK");

/**

*转换输入流

*注意,在转换字符流的时候,设置的字符集编码要与读职的文件的数据的编码一致

*不然就会出现乱码

*InputStreamReader

*/



publia statia void testInputStreamReader ()throws Exception{



FlleInputStream fs = new FileInpuqStream("Text.txt");

//把字书节流转换为字符流

InputStreamReader in = new InputStreamReader(fs."GBK");

//参数是字节流,参数2是编码





char[] c = new char[100];

int len = 0;



while((len = in.read(c))!=-1){

Syatem.out.println (new String (c, 0,len));

}



in.close ();

fs.clos3e ();

}





/**

*转换字节输出流为字符输出流

* OutputStreamrAter

*/

public static void testOutputStreamiriter () throws Exception{



FileOutputStream out = new FileOutputStream("Text.txt");

OutputStreamWriter os = new OutputStreamWriter (out,"GBK");



os.write("你好你好");

os.flugh();

os.close():

}

标准输入输出流(System.in,System.out)

public void static text(){



InputStreamReader is = new InputStreamReader (System,in);



//把输入流放到缓冲流里

BufferedReader br = new BufferedReader (is);

Bufferedriter out = new Bufferedriter(new FileWriter("Text.txt"));



String line = "":



while((line = br.readLine ())!= nall) {

if (1ine,equals("over")){

break;

}



//读取的每一行都写到指定的rxT文件

out.write (line);

}



out.flueh();

out.close ();

br.close();

is.close():

}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值