一、IO流之前
①System类:System.getProperties();
△Properties是一个与IO流相结合的集合
Properties props = System.getProperties();
props.list(System.out);
②Runtime类:Runtime r = Runtime.getRuntime();Process p = r.exec("notepad.exe L1IOOther.java");p.destroy();
③Date类:SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");System.out.println(sdf.format(new Date()));
④Calendar类:
Calendar c = Calendar.getInstance();
System.out.println(MessageFormat.format("{0}-{1}-{2}", c.get(Calendar.YEAR),c.get(Calendar.MONTH),c.get(Calendar.DAY_OF_MONTH)));
//练习一:获取指定年月的天数
public static int getAnyMonthDays(int year,int month){
Calendar c = Calendar.getInstance();
c.set(year, month, 1);
c.add(Calendar.DAY_OF_MONTH, -1);
return c.get(Calendar.DAY_OF_MONTH);
}
//练习二:计算工作日天数【周一至周五】
public static int getWorkDay(Date dfrom,Date dto){
Calendar from = Calendar.getInstance();
from.setTime(dfrom);
Calendar to = Calendar.getInstance();
to.setTime(dto);
int sum = 0;
while(!from.equals(to)){
int weekDate = from.get(Calendar.DAY_OF_WEEK);
if(weekDate!=Calendar.SUNDAY&&weekDate!=Calendar.SATURDAY){
sum++;
System.out.println(MessageFormat.format("{0}-{1}-{2}", from.get(Calendar.YEAR),from.get(Calendar.MONTH)+1,from.get(Calendar.DAY_OF_MONTH)));
}
from.add(Calendar.DAY_OF_MONTH, 1);
}
return sum;
}
------------------------------------孤独的分割线------------------------------------
二、IO流概述
1、字符流【Reader/Writer】
2、字节流【InputStream/OutputStream】
3、文件类【File】
4、其他流及字符编码
三、IO之字符流
【1】FileReader/writer
△读取文件,并将字节按sun.jnu.encoding设定的编码转换为字符
【2】缓冲字符流BufferedReader/Writer
△一次可以读写一行
△IO流运用了装饰模式,简化了继承体系,增强了扩展性
【3】字符转换流InputStreamReader/Writer
△可以自定义字符转换编码
★读写文本文件
/**
* 指定字符集读写文本文件
* @param ori
* @param dest
* @param charset
* @throws IOException
*/
public static void copyFile_3(File ori, File dest,String charset) throws IOException {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(new FileInputStream(ori),charset));
bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest),charset));
String sl = null;
while((sl = br.readLine())!=null){
bw.write(sl);
bw.newLine();
}
} catch (IOException e) { throw e; }
finally {
try {
if (bw != null) {bw.close();}
} catch (IOException e) {throw new RuntimeException(e);}
try {
if (br != null) {br.close();}
} catch (IOException e) {throw new RuntimeException(e);}
}
}
★自定义缓冲字符流
package com.lfd.io.learn;
import java.io.IOException;
import java.io.Reader;
public class L3MyBufferedReader extends Reader{
private Reader r;
public L3MyBufferedReader(Reader r) {
this.r = r;
}
public String readLine() throws IOException{
StringBuilder sb = new StringBuilder();
int ch = 0;
while((ch=r.read())!=-1){
if(ch == '\r'){continue;}
if(ch == '\n'){return sb.toString();}
sb.append((char)ch);
}
if(sb.length()!=0){return sb.toString();}
return null;
}
@Override
public void close() throws IOException{
r.close();
}
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return r.read(cbuf, off, len);
}
}
四、IO之字节流
【1】FileInputStream/OutputStream
【2】BufferedInputStream/OutputStream
△内部实现了缓冲区
★实现复制文件
/**
* 复制文件
* @param source
* @param dest
* @throws IOException
*/
public static void copyFile(File source,File dest) throws IOException{
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(source));
bos = new BufferedOutputStream(new FileOutputStream(dest));
int i = 0;
while((i = bis.read())!=-1){bos.write(i);}
} catch(IOException e) {
throw e;
}finally{
if(bos!=null){try {bos.close();} catch (IOException e) {
e.printStackTrace();
}}
if(bis!=null){try {bis.close();} catch (IOException e) {
e.printStackTrace();
}}
}
}
★模拟缓冲字节流
package com.lfd.io.learn;
import java.io.IOException;
import java.io.InputStream;
/**
* 模拟缓冲字节输入流
*/
public class L5MyBufferedInputStream extends InputStream{
private InputStream is;
private byte[] buffer = new byte[1024];
private int count = 0;
private int pos = 0;
public L5MyBufferedInputStream(InputStream is){
this.is = is;
}
@Override
public int read() throws IOException {
if(count == 0){
count = is.read(buffer);
pos = 0;
}
if(count<0){
return -1;
}else{
byte b = buffer[pos];
count--;
pos++;
return b&255;
}
}
@Override
public void close() throws IOException {
is.close();
}
}
★使用转换流读取控制台输入并将输入打印至控制台
*System.setIn()和System.setOut()可以改变标准输入输出设备
/**
* 读取转换流在控制台读取输入与控制打印
* @throws IOException
*/
public static void readWriteConsole() throws IOException{
BufferedReader br = null;
BufferedWriter bw = null;
try {
String sl = null;
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
while(!"over".equals((sl=br.readLine()))){
bw.write(sl);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
throw e;
}finally{
try{if(bw!=null)bw.close();}catch(IOException ex){ex.printStackTrace();}
try{if(br!=null)br.close();}catch(IOException ex){ex.printStackTrace();}
}
}