Java IO流

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

字节流(图片)

FileInputStream fis=new FileInputStream("F:\\test.rar");

//FileInputStream fis = System.in; //控制台键盘录入InputStream

FileOutputStream fos = new FileOutputStream("F:\\Copy.rar");

//FileOutputStream fos = System.out; //目的地:控制台PrintStream

 

//BufferedInputStream bis = new BufferedInputStream(fis);

//BufferedOutputStream bos = new BufferedOutputStream(fos);

 

字符流(文本,只能是gbk格式)

FileReader fr = new FileReader("Demo.java"); //输入

FileWriter fw = new FileWriter("Copy.java"); //输出

 

//BufferedReader br = new BufferedReader(fr); //缓冲流

//BufferedWriter bw = new BufferedWriter(fw); //高效

 

转换流(字节流 指定编码转 字符流)

FileInputStream fis = new FileInputStream("F:\\utf-8.txt");

//FileInputStream fis = System.in; //控制台键盘录入

InputStreamReader isr = new InputStreamReader(fis,"utf-8");//默认gbk

//FileReader fr = new InputStreamReader(fis,"utf-8"); //多态

 

FileOutputStream fos = new FileOutputStream("F:\\utf-8.txt",true);

OutputStreamWriter osw=new OutputStreamWriter(fos,"utf-8");//转换流

//FileWriter fw =new OutputStreamWriter(fos,"utf-8"); //多态

 

//一次读写一个字节 //字符

int ch;

while((ch=br.read())!=-1) {

bw.write(ch);    //fw.flush();

//System.out.print((char)ch); // 转字符

}

 

//一次读写一个字符数组           // 字节数组

char[] chs = new char[1024]; //byte[] bytes=new byte[1024];

int len;

while((len=br.read(chs))!=-1) { //(bytes)

bw.write(chs,0,len);

//System.out.print(new String(chs,0,len));//字节数组转字符串

}

 

//一次读写一行(字符串)字符缓冲流特有

String line;

while((line=br.readLine())!=null){  //br.readLine()一次读一行

bw.write(line);//System.out.println(line);

bw.newLine();//写入系统换行符

bw.flush();//刷新

}

//释放资源 用什么读写 用什么关

bw.close(); //bw>fw  bos>fos  osw>fos

br.close();

}

 

posted @ 2019-01-10 10:50  博客张C  阅读(165)  评论(0编辑  收藏  举报