导读
这些概念的东西可以在网上就可以找到,但我们需要通过例子来加深学习会更好。
字节字符转换流的代码如下:
package 类集;
import java.io.* ;
public class OutputStreamWriterDemo01{
public static void main(String args[]) throws Exception { // 所有异常抛出
File f = new File("d:" + File.separator + "test.txt") ;
Writer out = null ; // 字符输出流
out = new OutputStreamWriter(new FileOutputStream(f)) ; // 字节流变为字符流,同时向上转型。
out.write("hello world!!") ; // 使用字符流输出
out.close() ;
}
};
字节缓冲流的代码如下:
package com.iotest;
import java.io.ByteArrayInputStream;
import java.io.IOException;
public class ByteArryInputStreamDemo {
public static void main(String[] args) throws IOException {
String str = "abcdefghijk";
byte[] strBuf = str.getBytes(); //字符串转换成字节数组
ByteArrayInputStream bais = new ByteArrayInputStream(strBuf);
int data = bais.read(); //从字节数组输入流读取字节
while(data!=-1){
char upper = Character.toUpperCase((char)data);
System.out.print(upper+" ");
data = bais.read();
}
bais.close();
}
}
字符缓冲流的代码如下:
public String readLine()throws IOException:读取一个文本行。
import java.io.*;
public class BufferedIODemo2 {
public static void main(String[] args){
FileReader fr=null;
BufferedReader bw=null;
try {
fr=new FileReader("res/demo.txt");
bw=new BufferedReader(fr,255);
String str=null;
while((str=bw.readLine())!=null){
System.out.println(str);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(bw!=null)
try {
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
打印流的代码如下:
import java.io.* ;
public class PrintDemo01{
public static void main(String arg[]) throws Exception{
PrintStream ps = null ; // 声明打印流对象
// 如果现在是使用FileOuputStream实例化,意味着所有的输出是向文件之中
ps = new PrintStream(new FileOutputStream(new File("d:" + File.separator + "test.txt"))) ;
ps.print("hello ") ;
ps.println("world!!!") ;
ps.print("1 + 1 = " + 2) ;
ps.close() ;
}
};
对象流与序列化的代码如下:
package com.io;
import java.io.*;
public class listFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
//1 新建一个File类的实例
File file=new File("d:"+File.separator+"Xiaomi");
//2 进行文件目录的打印
print(file);
}
public static void print(File file) {
// TODO Auto-generated method stub
if(file!=null){//判断文件不为空
if(file.isDirectory()){//判断文件是不是目录
File[] fs=file.listFiles();//返回所有的子文件
if(fs!=null){
//开始遍历子文件
for (int i = 0; i < fs.length; i++) {
print(fs[i]);
}
}
}else{//如果不是目录就打印文件的路径
System.out.println(file);
}
}
}
}
字节数组流的代码如下:
import java.io.ByteArrayInputStream;
import java.io.IOException;
/**
* ByteArrayInputStream:在创建对象时传入数组即可,不需要传文件,也没有新增加的方法,close关闭无效。
* 流本身就是内存中的资源,流中的内容也是内存中的资源,所以不用手动关闭,内存会给他释放,所以不用关闭流
*
* 流的内容是内存中的资源,不访问磁盘。
* */
public class ByteArrayDemo01 {
public static void main(String[] args) throws IOException {
String str = "做人要脚踏实地,不能投机取巧";
byte[] bt = str.getBytes();
readArray(bt);
}
private static void readArray(byte[] bt){
/**
* ByteArrayInputStream声明时不抛出异常的原因:没有和外界产生关系
* */
ByteArrayInputStream bais = new ByteArrayInputStream(bt);
int hasRead = 0;
byte[] bt1 = new byte[1024];
StringBuilder sb = new StringBuilder();
try {
while((hasRead = bais.read(bt1))!=-1){
sb.append(new String(bt1, 0, hasRead));
}
System.out.println(sb);
} catch (IOException e) {
e.printStackTrace();
}
}
}