1.流的分类
- 按操作数据单位不同分为:字节流(8bit),字符流(16 bit)
》对于文本文件(.txt, .java, .c, .cpp),使用字符流处理。
》对于非文本文件(.jpg, .mp3, .mp4, .avi, .doc, .ppt,…),使用字节流处理。 - 按数据流的流向不同分为:输入流,输出流
- 按流的角色的不同分为:节点流,处理流
- 1.Java的IO流共涉及40多个类,实际上非常规则,都是从如下4个抽象基类派生的。
- 2.由这四个派生出来的子类名称都是以其父类名作为子类名的后缀。
2.IO流体系
3.FileReader读入数据的基本操作
package com.rufy.java;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args){
FileReader fr = null;
try {
File file = new File("day01\\hello.txt");
System.out.println(file.getAbsolutePath());
fr = new FileReader(file);
int data;
while((data=fr.read())!=-1){
System.out.print((char)data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.rufy.java;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class Test {
public static void main(String[] args){
FileReader fr = null;
try {
File file = new File("day01\\hello.txt");
System.out.println(file.getAbsolutePath());
fr = new FileReader(file);
char[] cbuf=new char[5];
int len;
while((len=fr.read(cbuf))!=-1){
for (int i = 0; i <len; i++) {
System.out.print(cbuf[i]);
}
String str=new String(cbuf,0,len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4.FileWriter写出数据的操作
package com.rufy.java;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
FileWriter fw= null;
try {
File file=new File("helloTest.txt");
fw = new FileWriter(file,true);
fw.write("I hava a dream!\n");
fw.write("you need to hava a dream!");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5.使用FileReader和FileWriter实现文本文件的复制
package com.rufy.java;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
FileReader fr= null;
FileWriter fw= null;
try {
File srcFile=new File("day01\\hello.txt");
File destFile=new File("day01\\hello2.txt");
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);
char[] cbuf=new char[5];
int len;
while ((len=fr.read(cbuf))!=-1){
fw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(fw!=null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fr!=null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
6.使用FileInputStream和FileOutputStream读写非文本文件
package com.rufy.java;
import java.io.*;
public class Test {
public static void main(String[] args) {
FileInputStream fis=null;
FileOutputStream fos=null;
try {
File srcFile = new File("day01\\1.jpg");
File destFile = new File("day01\\tu.jpg");
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
byte[] buffer=new byte[5];
int len;
while ((len=fis.read(buffer))!=-1){
fos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(fis!=null){
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
7.缓冲流(字节型)实现非文本文件的复制
package com.rufy.java;
import java.io.*;
public class Test {
public static void main(String[] args) {
FileInputStream fis= null;
FileOutputStream fos = null;
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File srcFile=new File("day01\\1.jpg");
File destFile=new File("day01\\fast.jpg");
fis = new FileInputStream(srcFile);
fos = new FileOutputStream(destFile);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buffer=new byte[10];
int len;
while((len=bis.read(buffer))!=-1){
bos.write(buffer,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bos!=null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis!=null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
8.缓冲流(字符型)实现文本文件的复制
package com.rufy.java;
import com.sun.xml.internal.ws.util.xml.CDATA;
import java.io.*;
public class Test {
public static void main(String[] args) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new FileReader(new File("day01\\hello.txt")));
bw = new BufferedWriter(new FileWriter(new File("day01\\aaa.txt")));
String data;
while ((data=br.readLine())!=null){
bw.write(data);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bw!=null)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
if(br!=null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
8.转换流实现文件的读入和写出
package com.rufy.java;
import com.sun.xml.internal.ws.util.xml.CDATA;
import java.io.*;
public class Test {
public static void main(String[] args) {
InputStreamReader isr = null;
OutputStreamWriter osw = null;
try {
File file1 = new File("day01\\hello.txt");
File file2 = new File("day01\\hellowrite.txt");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
isr = new InputStreamReader(fis,"utf-8");
osw = new OutputStreamWriter(fos,"gbk");
char[] cbuf=new char[20];
int len;
while ((len=isr.read(cbuf))!=-1){
osw.write(cbuf,0,len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(isr!=null)
try {
isr.close();
} catch (IOException e) {
e.printStackTrace();
}
if(osw!=null)
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
8.标准的输入、输出流
- System.in : 标准的输入流,默认从键盘输入
- System.out: 标准的输出流,默认从控制台输出
package com.rufy.java;
import com.sun.xml.internal.ws.util.xml.CDATA;
import java.io.*;
import java.sql.SQLOutput;
public class Test {
public static void main(String[] args) {
BufferedReader br = null;
try {
InputStreamReader isr = new InputStreamReader(System.in);
br = new BufferedReader(isr);
while(true){
System.out.println("请输入字符串:");
String data = br.readLine();
if("e".equalsIgnoreCase(data)||"exit".equalsIgnoreCase(data)){
System.out.println("程序结束");
break;
}
String upperCase = data.toUpperCase();
System.out.println(upperCase);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(br!=null)
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
9.打印流的使用
- System.setOut(PrintSteam ps)
package com.rufy.java;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class Test {
public static void main(String[] args) {
PrintStream ps = null;
try {
FileOutputStream fos = new FileOutputStream(new File("day01\\打印流.txt"));
ps = new PrintStream(fos, true);
if(ps!=null){
System.setOut(ps);
}
for(int i=0;i<255;i++){
System.out.print((char) i);
if(i%50==0){
System.out.println();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if(ps!=null)
ps.close();
}
}
}