java中的IO

1.I/O系统操作的目标是为了对数据进行读写操作

输入:从数据源当中读取数据

//导入类

import java.io.*;

 

class Test{

         public static void main(String args[]){

         //声明输入流引用

                   FileInputStream fis = null;

                   //声明输出流引用

                   FileOutputStream fos = null;

                   try{

                   //生成代表输入流的对象

                            fis = new FileInputStream("D:/from.txt");

                   //生成代表输出流的对象

                   fos = new FileOutputStream("D:/to.txt");

                            //生成一个字节数组

                            byte [] buffer = new byte[100];

                            //调用输入流的read的方法,输入数据

                            fis.read(buffer,0,buffer.length);

                            String s = new String(buffer);

                            //调用一个String对象的trim方法,将会去除掉这个字符串的首位空格和空字符

                            s = s.trim();

                            System.out.println(s);

                   }

                   catch(Exception e){

                            System.out.println(e);

                   }

         }

}输出:将数据写入到数据目的地当中

//导入类

import java.io.*;

 

class Test{

         public static void main(String args[]){

         //声明输入流引用

                   FileInputStream fis = null;

                   //声明输出流引用

                   FileOutputStream fos = null;

                   try{

                   //生成代表输入流的对象

                            fis = new FileInputStream("D:/from.txt");

                   //生成代表输出流的对象

                   fos = new FileOutputStream("D:/to.txt");

                            //生成一个字节数组

                            byte [] buffer = new byte[100];

                            //调用输入流的read的方法,输入数据

                            int temp = fis.read(buffer,0,buffer.length);//输入流的返回值为写入的数据长度

                            fos.write(buffer,0,temp);

                   }

                   catch(Exception e){

                            System.out.println(e);

                   }

         }

}

 

2.IO的分类

输入流和输出流、字节流和字符流、节点流和处理流

3.核心类的核心方法

 InputStream://读取数据

 intread(byte[]b,int off,int len)//读进来的数据从b的第off(offside:偏移量)为开始,读取一次最多读取len个数据,返回值为读取数据的字节数

 OutputStream://数据写到文件中

 voidwrite(byte[]b,int off,int len)//要往文件中写入的数据要先变成比特数据,从b的第off(offside:偏移量)为开始写,一共写入len个数据

转载于:https://www.cnblogs.com/java4android/archive/2012/05/01/2477817.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值