InputString()和OutputString()的使用

InputString()和OutputString()的使用

InputString()
(1)FileInputstream: 子类,读取数据的通道

  使用步骤:

    1.获取目标文件:new File()

    2.建立通道:new FileInputString()

    3.读取数据:read()

    4.释放资源:close()

//一些默认要导入的包
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//分别调用方法查看效果
test1();
System.out.println(“——————————————-“);
test2();
System.out.println(“——————————————-“);
test3();
System.out.println(“——————————————-“);
test4();
}
(2)读取数据的三种方式

    1.直接读取 (一次只能一个字节)

int date = fileInputStream.read();
      char date3 = (char)fileInputStream.read();

//方式一 直接打印
public static void test1() throws IOException{
//(1)获取目标文件路径
File file = new File(“C:\Users\joke\Desktop\Demo1.java”);
//(2)根据目标文件路径 建立通道: new FileInputStream(file)
FileInputStream fileInputStream = new FileInputStream(file);
//(3)读取数据 :read();
int date = fileInputStream.read();//这里是int类型
int date2 = fileInputStream.read();//
char date3 = (char)fileInputStream.read(); //以char类型显示
System.out.println(date+”\”+date2+”\”+date3);
//(4)释放资源
fileInputStream.close();
}
 2.单独使用for循环(效率低)     

for(int i = 0; i < file.length();i++){
        System.out.print((char)fileInputStream.read());
      }

//方式二 循环遍历
public static void test2() throws IOException{
//通过时间测试效率
long startTime = System.currentTimeMillis();
File file = new File(“C:\Users\joke\Desktop\Demo1.java”);
FileInputStream fileInputStream = new FileInputStream(file);
//for循环
for(int i = 0; i < file.length();i++){
System.out.print((char)fileInputStream.read());
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(“读取文件所花时间:”+(endTime-startTime));
}
3.Byte[ ] 缓冲区(只能读取指定的字节数不能读取一个完整的文件)   

byte[] bt = new byte[1024];
      int count = fileInputStream.read(bt);
      System.out.println(new String (bt,0,count));

//方式三 创建缓冲区(只能读取制定的大小,不能读取一个完整的文件)
public static void test3() throws IOException{
File file = new File(“C:\Users\joke\Desktop\Demo1.java”);
FileInputStream fileInputStream = new FileInputStream(file);
//创建缓冲区,加快读取数据,确定要读取的字节大小
byte[] bt = new byte[1024];
//read() 读取字节
int count = fileInputStream.read(bt);
System.out.println(count); //显示读取到的字节数
System.out.println(new String (bt,0,count));//将字节转为字符串显示
fileInputStream.close();
}
4.缓冲区和循环结合。缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高  

byte[] bt = new byte[1024];
      int count = 0;
      while((count = fileInputStream.read(bt)) != -1){
        System.out.println(new String (bt,0,count));
      }

//方式四 循环与缓冲区结合(效率高)
public static void test4() throws IOException{
//通过时间测试效率
long startTime = System.currentTimeMillis();
File file = new File(“C:\Users\joke\Desktop\Demo1.java”);
FileInputStream fileInputStream = new FileInputStream(file);
//缓冲区一般设置为1024的倍数。理论上设置的缓冲区越大,读取效率越高
byte[] bt = new byte[1024];
int count = 0;
//read返回 -1 时,证明已经遍历完
while((count = fileInputStream.read(bt)) != -1){
//字符串型显示(从bt中的第0个字节开始遍历count个长度)
System.out.println(new String (bt,0,count));
}
fileInputStream.close();
long endTime = System.currentTimeMillis();
System.out.println(“读取文件所花时间:”+(endTime-startTime));
}

  在以上,对比第二个和第四个方法,会发现方法四的效率是比较高的,所以推荐使用的四个方法

  在这里我们是直接抛出异常,除了抛出之外我们还可以使用

    try{  }cater{  }finally{  }

  的方式来处理异常

OutputString()的使用
  FileOutPutStream:子类,写出数据的通道

  步骤:

    1.获取目标文件

    2.创建通道(如果原来没有目标文件,则会自动创建一个)

    3.写入数据 write()

    4.释放资源

  注意:

    (1)如果目标文件不存在,那么会自己创建一个目标文件

    (2)如果目标文件存在,先将里面的数据清空,再写入数据

    (3)想在原有的数据上写入数据,则在创建通道的时候使用 构造方法:

       OutPutStream(File file,Boolean append),boolean值为true则可以

    (4)用 write(int a)方法写入数据,虽然接收的是int,但实际上只有一个字节的数据

      (操作的是低八位的,其他的全部丢掉)

//会自动导入一些包
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

//方式一
public static void writeData() throws IOException{
//1.找目标文件
File file = new File(“C:\Users\bigerf\Desktop\文件夹\writeTest.java”);
//2.创建一个通道
FileOutputStream outputStream = new FileOutputStream(file);
//3.开始写入数据,
int a = 10; // int 型 4个字节
outputStream.write(a); //注意这里每次只能输出一个字节
outputStream.write(‘b’); // char 类型
outputStream.write(5);
// 0000-0000 0000-0000 0000-0001 1111-1111 == 511
int b = 511 ; //大于八位(9位)
outputStream.write(b); //实际结果 255,但没有显示
int c = 63; //小于八位(6位)
outputStream.write(c); //乱码
//4.关闭资源
outputStream.close();
}

//方式二
public static void writeData2() throws IOException{
//1.找目标文件
File file = new File(“C:\Users\bigerf\Desktop\文件夹\writeTest2.java”);
//2.创建一个通道,(如果不存在路径中的文件,则会在这一步创建的文件)
//new FileOutputStream(file,true); /true表示在原来文本的基础上写入文本(反之则会先清空再写入)
FileOutputStream outputStream = new FileOutputStream(file,true);
//3.创键一个字节数组
String str = “hello word”;
//将字符串变为字节数组
byte[] b = str.getBytes();
//4.写入数据
outputStream.write(b); //hello word
//5.关闭资源
outputStream.close();
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值