Java文件操作(二)

来源:互联网 发布:厦门雅迅网络很烂吗 编辑:程序博客网 时间:2024/05/29 16:25

3.3、内存流(掌握)

一个子类决定父类的具体操作行为,那么对于整个IO操作来说,具体的输入、输出会根据子类的不同而有所不同。

内存流:指的是所有的输入、输出都是以内存为目的地。

内存的输出流:ByteArrayOutputStream,以内存为源目的地

内存输入流:ByteArrayInputStream,是指把内容向内存中输入。

ByteArrayInputStream方法:

构造:public ByteArrayInputStream(byte[] buf),表示把内容输入到内存里去

ByteArrayOutputstream方法:

构造:public ByteArrayOutputStream()

例如:以下代码完成了一个大-小写的转换功能

import java.io.* ;

public class IODemo22{

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

ByteArrayInputStream bis = null ;

ByteArrayOutputStream bos = null ;

bos = new ByteArrayOutputStream() ;

String str = "helloworld" ;

// 把内容输入到内存里去

bis = new ByteArrayInputStream(str.getBytes()) ;

// 通过bos可以把内容读取出来

int c = 0 ;

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

   char x = Character.toUpperCase((char)c) ;

   bos.write(x) ;

}

System.out.println(bos) ;

bis.close() ;

bos.close() ;

}

}

3.4、打印流(重点)

思考:

之前如果想向一个文件中保存一些内容,需要把内容变为byte数组,很麻烦,那么对于OutptuStream本身而言,只是具备了保存的功能,但是其功能并不完善。所以后来人们为了操作IO方便(输出方便)为OutptuStream增加了一个子类 —— PrintStream。

PrintStram之中提供了比OutputStram中更好的输出方法。

打印流实际上也分为两种:PrintStream、PrintWriter

PrintStream的使用:

构造:public PrintStream(OutputStream out)

根据传入的OutputStream来决定输出的位置。

使用:print()、println()

例如:观察PrintStream使用

import java.io.* ;

public class IODemo23{

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

PrintStream ps = null ;

ps = new PrintStream(new FileOutputStream(new File("e:\\h.txt"))) ;

ps.print("hello ") ;

ps.print("world") ;

ps.println("\r\nHELLO") ;

ps.close() ;

}

}

如果需要在文件之后对内容进行追加的话,则直接编写以下代码即可:

import java.io.* ;

public class IODemo23{

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

PrintStream ps = null ;

ps = new PrintStream(new FileOutputStream(new File("e:\\h.txt"),true)) ;

ps.print("hello ") ;

ps.print("world") ;

ps.println("\r\nHELLO") ;

ps.println("\r\nABC") ;

ps.close() ;

}

}

可以发现,使用打印流很方便的完成数据的输出。

3.5、System对IO的支持

回顾:System.out.println() ;

System中有以下两个静态属性:

System.out:对应的是标准输出,为显示器

System.in:对应的是标准输入,为键盘

3.6.1、System.out

System.out是PrintStream的类型,问:能否通过此对象为OutputStream实例化?

import java.io.* ;

public class IODemo24{

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

OutputStream out = System.out ;

out.write("HELLO WORLD".getBytes()) ;

out.close() ;

}

}

以上代码进一步验证了面向对象的多态性,那个子类为父类实例化,那么父类就具备那个子类的功能。所有的目的地由子类决定。父类只是规定出了标准。

3.6.2、System.in

System.in对应着键盘的输入,是InputStream类型的对象。

那么既然是InputStream类型的对象,那么下面的代码实验了由键盘输入数据:

import java.io.* ;

public class IODemo25{

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

// 现在代码从键盘中读取信息

InputStream input = System.in ;

byte b[] = new byte[1024] ;

System.out.print("请输入内容:") ;

int len = input.read(b) ;

System.out.println("输入的内容为:" + new String(b,0,len)) ;

}

}

以上确实完成了键盘的输入信息功能,但是否存在问题?

开辟的空间问题。

之前学过一种方式,不开辟一个空间,有多少读多少?

那么如果现在使用此种方式呢?

import java.io.* ;

public class IODemo27{

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

// 现在代码从键盘中读取信息

InputStream input = System.in ;

StringBuffer buf = new StringBuffer() ;

System.out.print("请输入内容:") ;

int c = 0 ;

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

   buf.append((char)c) ;

}

System.out.println("输入的内容为:" + buf) ;

}

}

以上代码没有指定出具体的空间大小,而是输入多少就保存多少,如果现在输入的是英文字母可以正确保存,如果是中文呢,因为是半个半个字保存的,所以是乱码。

3.6、BufferedReader(缓冲读取,重点)

之前出现乱码的根本原因在于是分着读的。

BufferedReader是一个字符流的子类,构造方法:

public BufferedReader(Reader in)

System.in是一个字节流的对象。

字节流-字符流的转换类:

InputStreamReader:把输入的字节流变为字符流

OutputStreamWriter:把输出的字符流变为字节流

观察InputStreamReader,是Reader的子类,构造方法:

   public InputStreamReader(InputStream in)

BufferedReader中读取:public String readLine() throws IOException

键盘输入数据的标准格式:

import java.io.* ;

public class IODemo28{

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

BufferedReader buf = null ;

buf = new BufferedReader(new InputStreamReader(System.in)) ;

System.out.print("请输入内容:") ;

String str = null ;

str = buf.readLine() ;

System.out.println("输入的内容为:" + str) ;

}

}

如果现在要想输入多个数据的话,则直接重复readLine()即可。

import java.io.* ;

public class IODemo29{

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

BufferedReader buf = null ;

buf = new BufferedReader(new InputStreamReader(System.in)) ;

System.out.print("请输入内容1:") ;

String str = null ;

str = buf.readLine() ;

System.out.println("输入的内容1为:" + str) ;

System.out.print("请输入内容2:") ;

str = buf.readLine() ;

System.out.println("输入的内容2为:" + str) ;

}

};

0 0
原创粉丝点击