——Java培训、Android培训、iOS培训、.Net培训、期待与您交流! ——
自学视频io第二部分最后
这一讲重点部分,明白io流中这么多体系,怎么快速判断用哪种流。。。
1
1.1:明白源:InputStream Reader
1.2:明确目的:OutputStream Writer
2 是否是文本文件
是: 用Reader Writer
不是:用 InputStream OotputStream
3 是否要提高读写效率
是:BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(“luo.txt”)));
4 流的转换
字节流转换成字符流
InputStreamReader OutputStreamWriter
package com.itheima.www;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Scanner;
/**
* 接受键盘的录入
*
* @author myluo
*
*/
public class ScananerInput {
public static void main(String[] args) {
//方法一 录入打印在控制台上
// methodScananer1();
//方法二 键盘录入写入指定文件
//methodScannaner2();
//方法三 读取文件打印在控制台上
// methodScananer3();
//练习 三个明确 把一个图片文件copy到另一个磁盘
// methodCopyImger();
//方法五 转换流的由来
// methodInputStreamReader();
// methodInputStreamReader2();
//方法六 保存log信息
// methodInfo();
//方法七 程序运行时打印系统信息
methodSystemInfo();
}
//方法七 程序运行时打印系统信息
private static void methodSystemInfo() {
// TODO Auto-generated method stub
Properties pro=System.getProperties();
try {
pro.list(new PrintStream("x5.txt"));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void methodInfo() {
// TODO Auto-generated method stub
Scanner input=new Scanner(System.in);
int num=0;
System.out.print("请输入:");
try {
num=input.nextInt();
} catch (Exception e) {
// TODO: handle exception
try {
PrintStream ps=new PrintStream("luo.txt");
Date dt=new Date();
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String date=sdf.format(dt);
ps.println(date);
e.printStackTrace(ps);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
//指定读写的文件的编码格式,默认是GBK,用到了转换流
private static void methodInputStreamReader2() {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/a/luo5551.txt"),"utf-8"));
String len=null;
try {
while((len=br.readLine())!=null){
bw.write(len);
bw.flush();
}
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//指定读写的文件的编码格式,默认是GBK,用到了转换流
private static void methodInputStreamReader() {
// TODO Auto-generated method stub
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw=null;
try {
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("E:/a/luo555.txt")));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String len=null;
try {
while((len=br.readLine())!=null){
bw.write(len);
bw.flush();
}
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//图片的copy
private static void methodCopyImger() {
/**
* 1 ,明确源和目的
* 源:InputStream Reader
* 目的:OutputStream Writer
*
* 2:是否是文本文件
* 是:Reader Writer
* 不是:InputStream OutputStream
*
* 3:(Reader 和 Writer 体系中)是否要提高效率
* BufferedReader br=new BufferedReader();
* BufferedWriter bw=new BufferedWriter();
*
*/
try {
//读取图片文件
FileInputStream fs=new FileInputStream("C:/Users/Administrator/Desktop/psb.jpg");
//提高读取效率
//BufferedReader br=new BufferedReader(new InputStreamReader(fs));
//提高效率
BufferedInputStream bis=new BufferedInputStream(fs);
//写入图片文件
FileOutputStream fos=new FileOutputStream("E:/a/xx2.jpg");
//提高写入效率
//BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedOutputStream bos=new BufferedOutputStream(fos);
//String len=null;
int len=-1;
byte[] by=new byte[1024];
try {
while((len=bis.read(by))!=-1){
bos.write(by,0,len);
}
bos.flush();
bos.close();
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void methodScananer3() {
try {
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("C:/Users/Administrator/Workspaces/MyEclipse 8.5/System1/luo.tex")));
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
String len=null;
try {
while((len=br.readLine())!=null){
bw.write(len);
bw.flush();
}
bw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void methodScannaner2() {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
try {
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("luo.tex")));
String len=null;
try {
while((len=br.readLine())!=null){
if("a".equals(len)){
break;
}
bw.write(len.toUpperCase());
bw.flush();
}
bw.close();
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//键盘录入与打印1
private static void methodScananer1() {
/**
* 通过字符转换流
* 把字节转换成字符
*
*/
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
//把读出来对象打印在控制台
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
//读取字符流
String len=null;
try {
while((len=br.readLine())!=null){
bw.write(len.toUpperCase());
//换行
bw.newLine();
bw.flush();
}
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我想要的,我一定会要得到。
原文地址:http://blog.csdn.net/myadmini/article/details/45845319