IO流

来源:互联网 发布:数据分析工作内容 编辑:程序博客网 时间:2024/06/05 22:50

IO流

输入输出流

输入流FileInputStream

向程序中输入数据,且数据的单位为字节

int read() throws IOException

读取⼀个字节,并以整数的形式返回,如果返回值是-1则表⽰已经到输⼊流的末尾

int read(byte[] buffer)throws IOException

读取⼀系列字节,返回实际读取的字节数,如果返回值是-1则表⽰已经到输⼊流的末尾

int read(byte[] buffer, intoffset, int length) throws IOException

从offset位置开始,读取length个字节,如果返回值是-1则表⽰已经到输⼊流的末尾

void close() throwsIOException

关闭流并释放内存空间

读取文件里面的内容,输出到控制台

 

 

@Test

public void test(){

/***************创建输⼊入流InputStream-begin********************/

FileInputStream in = null;

try {

in = new FileInputStream("C:\\ hello.txt");

} catch (FileNotFoundException e) {

System.out.println("找不不到指定⽂文件");

System.exit(0);

}

/***************创建输⼊入流InputStream-end********************/

long num = 0; //⽤用于计数的变量量,作⽤用是计算⼀一共读了了多少个字节

int b = 0; //⽤用于记录读取位置的变量量

try {

/***************读取⽂文件-begin********************/

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

System.out.println((char)b);

num ++;

}

in.close();

System.out.println("共读取了了"+num+"个字节");

/***************读取⽂文件-end********************/

} catch (IOException e) {

System.out.println("⽂文件读取错误");

System.out.println(0);

}

}

输出流FileOutputStream

向程序中输出数据,数据的单位也是字节

void write(int b) throwsIOException

向输出流缓冲区中写⼊⼀个字节数据

void write(byte[] b) throwsIOException

向输出流缓冲区中写⼊⼀系列数据

void write(byte[] b, intoffset, int length) throws IOException

向输出流缓冲区中写⼊⼀系列数据,offset表⽰b的下标,length表⽰要写⼊到缓冲区的长度,offset+length要⼩

于等于b的长度

close() throws Exception

关闭流释放内存空间

void flush() throws Exception

将缓冲区的数据全部写出到⽬的地⽂件

@Test

   public voidtest2() throws IOException{

     FileOutputStream fos=newFileOutputStream("D:\\test.txt");

     fos.write("Helloworld".getBytes());

     fos.close();

   }

 

拷贝文件

@Test

   public voidtest1() throws IOException{

      FileInputStream fis=newFileInputStream("D:/test.txt");

      FileOutputStream fos=newFileOutputStream("D:/test1.txt");

      int b=0;

      while((b=fis.read())!=-1){

         fos.write(b);

      }

      fos.close();

      fis.close();

   }

 

 

这适合流媒体,因为读取是按字节读取和输出,但是文本中有可能含有汉字,汉字占用两个字节。但是流媒体都是字节形式的,比如:MP3,视频,图片

 

文件操作流

字符流Reader的基本方法

向程序中输入数据,且数据的单位为字符(一个字符占两个字节)

int read() throws IOException

读取⼀个字符并以整数形式返回,如果返回-1则表⽰已经到输⼊流的末尾

int read(char[] buff) throwsIOException

读取⼀系列字符,返回实际读取的字符数,如果返回-1则表⽰已经到输⼊流的末尾

int read(char[] buff, intoffset, int length) throws IOException

从offset位置读取length个字符,并返回实际读取的字符数,如果返回-1则表⽰已经到输⼊流的末尾

void close() throwsIOException

关闭流释放内存空间

 

 

读取⽂件内容,输出到控制台

@Test

public void test(){

FileReader fr = null;

try {

fr = new FileReader("C:\\ hello.txt");

int c = 0;

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

System.out.println((char)c);

}

fr.close();

} catch (FileNotFoundException e) {

System.out.println("找不不到指定⽂文件");

System.exit(0);

} catch (IOException e) {

System.out.println("⽂文件读取错误");

System.exit(0);

}

}

 

字符流Writer的基本方法

向程序中输入数据,且数据的单位是字符

void write(int c) throwsIOException

向输出流缓冲区中写⼊⼀个字符数据

void write(char[] buff)throws IOException

向输出流缓冲区中写⼊⼀系列的数据

void write(String str) throwsIOException

向输出流缓冲区中写⼊⼀个字符序列

void write(String str, intoffset, int length) throws IOException

向输出流缓冲区中写⼊⼀个字符序列,offset表⽰b的下标,length表⽰要写⼊到缓冲区的长度,offset+length要

⼩于等于b的长度

void close() throwsIOException

关闭流释放内存资源

void flush() throwsIOException

将输出流缓冲区中的数据全部写出到⽬的地⽂件

 

向硬盘中的⽂件写⼊内容

@Test

public void test(){

FileWriter fw = null;

try {

fw = new FileWriter("C:\\ hello2.txt");

String str = "HelloWorld我的Java世界";

fw.write(str);

fw.close();

} catch (IOException e) {

System.out.println("⽂文件写⼊入错误");

System.exit(0);

}

}

 

缓冲流

缓冲流的作用就是对数据的读写过程中提供缓存功能,提高读写的效率。

BufferedInputStream(FileInputStream in)

BufferedInputStream(FileInputStream in, int sz)

@Test

public void testBufferedInputStream(){

try {

FileInputStream in = new FileInputStream("C:\\ hello.txt");

BufferedInputStream bis = new BufferedInputStream(in,1024);

int b;

while((b = bis.read()) != -1){

System.out.println((char)b);

}

bis.close();

in.close();

} catch (FileNotFoundException e) {

System.out.println("⽂文件没有找到");

} catch (IOException e) {

System.out.println("⽂文件操作错误");

}

}

BufferedOutputStream(FileOutputStream out)

BufferedOutputStream(FileOutputStream out, int sz)

@Test

public void testBufferedOutputStream(){

try {

FileOutputStream out = new FileOutputStream("C:\\ hello1.txt");

BufferedOutputStream bos = newBufferedOutputStream(out, 1024);

bos.write("helloMyJava".getBytes());

bos.close();

out.close();

} catch (FileNotFoundException e) {

System.out.println("⽂文件没有找到");

} catch (IOException e) {

System.out.println("⽂文件操作错误");

}

}

BufferedReader(Reader in)

BufferedReader(Reader in, int sz)

@Test

public void testBufferedReader(){

try {

FileReader fr = new FileReader("C:\\ hello.txt");

BufferedReader br = new BufferedReader(fr, 1024);

String str;

while((str = br.readLine()) != null){

System.out.println(str);

}

br.close();

fr.close();

} catch (FileNotFoundException e) {

System.out.println("⽂文件没有找到");

} catch (IOException e) {

System.out.println("⽂文件操作错误");

}

}

BufferedWriter(Writer out)

BufferedWriter(Writer out, int sz)

@Test

public void testBufferedWriter(){

try {

FileWriter fw = new FileWriter("C:\\ hello2.txt");

BufferedWriter br = new BufferedWriter(fw);

br.write("来呀,相互伤害呀");

br.newLine();

br.write("来呀,相互伤害呀");

br.close();

fw.close();

} catch (FileNotFoundException e) {

System.out.println("⽂文件没有找到");

} catch (IOException e) {

System.out.println("⽂文件操作错误");

}

}

 

打印流

PringtStream(OutputStream out)

打印输出流,针对于字节

@Test

public void test(){

try {

FileOutputStream out = new FileOutputStream("C:\\ hello1.txt");

PrintStream ps = new PrintStream(out);

ps.print(“来呀,相互伤害呀".getBytes());

ps.close();

} catch (Exception e) {

e.printStackTrace();

}

}

PrintWriter(Writer out)

打印输出流,针对于字符

public void test1(){

try {

FileWriter fw = newFileWriter("C:\\Users\\qinzhicong\\Documents\\hello1.txt");

PrintWriter pw = new PrintWriter(fw);

pw.print(“来呀,相互伤害呀");

pw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

 

对象流

直接将对象写入和输出,被操作的对象必须序列化

对象序列化

序列化

序列化是指把Java对象转换成字节序列的过程

反序列化

反序列化是指把字节序列恢复成Java对象的过程

如何实现对象序列化?

只有实现了Serializable或Externalizable接⼜的类的对象才能被序列化,否则抛出异常。

 

将User对象输出到磁盘

package testio;

import java.io.Serializable;

public class User implements Serializable {

private String name = "张三";

private int age = 18;

 

public User(String name, int age) {

this.name = name;

this.age = age;

}

public String getName() {

return name;

}

public int getAge() {

return age;

}

}

@Test

public void test(){

User user = new User("张三", 18);

try {

FileOutputStream out = new FileOutputStream("C:\\hello1.dat");

ObjectOutputStream oos = new ObjectOutputStream(out);

oos.writeObject(user);

oos.flush();

oos.close();

out.close();

} catch (Exception e) {

e.printStackTrace();

}

}

 

 

ObjectInputStream(InputStream in)

对象输⼊流,配合FileInputStream使⽤,将对象输⼊到程序

将对象输⼊到程序

@Test

public void test1(){

try {

FileInputStream in = newFileInputStream("C:\\Users\\qinzhicong\\Documents\\hello1.dat");

ObjectInputStream ois = new ObjectInputStream(in);

User user = (User) ois.readObject();

System.out.println(user.getName()+"\t"+user.getAge());

} catch (Exception e) {

e.printStackTrace();

}

}

原创粉丝点击