java中八种IO操作
package com.mengya.TestIO;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.BufferedOutputStream;
import java.io.FileWriter;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.InputStreamReader;
public class TestIO {
/**
* Java中的8种IO操作
*/
/*
* 创建文件
*/
public void CreateFile(){
File f=new File("e:\\io.txt");
if(!f.exists()){
try {
f.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* FileOutputStream字节流式的写
*/
public void set1(){
try {
FileOutputStream f=new FileOutputStream("e:\\io.txt");
String str="我的未来不是梦";
byte[] b=str.getBytes();
try {
f.write(b);
f.flush();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
* FileInputStream字节流式的读
*/
public void get1(){
try {
FileInputStream f=new FileInputStream("e:\\io.txt");
byte[] b=new byte[200];
try {
int n=f.read(b);
String str=new String(b,0,n);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
*
*/
public void set2(){
try {
FileOutputStream f=new FileOutputStream("e:\\io.txt",true);//如果没true则复盖原来的文件,加了true则添加原文件后面
BufferedOutputStream ff=new BufferedOutputStream(f);
String str=new String("我的心跟着希望在动");
try {
ff.write(str.getBytes());
ff.flush();
ff.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void get2(){
try {
FileInputStream f=new FileInputStream("e:\\io.txt");
BufferedInputStream ff=new BufferedInputStream(f);
byte[] b=new byte[200];
try {
int n = ff.read(b);
String str=new String(b,0,n);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
* 字符流
*/
public void set3(){
try {
FileWriter f=new FileWriter("e:\\io.txt",true);
f.write("\r\n我在佛前苦苦求了几千前");//\r\n表示换行
f.flush();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void get3(){
try {
FileReader f=new FileReader("e:\\io.txt");
char[] c=new char[200];
try {
int n = f.read(c);
String str=new String(c,0,n);
System.out.println(str);
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void set4(){
try {
FileWriter f=new FileWriter("e:\\io.txt",true);
BufferedWriter ff=new BufferedWriter(f);
ff.write("希望能感动上天");
ff.flush();
ff.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void get4(){
try {
FileReader f=new FileReader("e:\\io.txt");
BufferedReader ff=new BufferedReader(f);
char[] c=new char[200];
int n;
try {
n = ff.read(c);
String str=new String(c,0,n);
System.out.println(str);
ff.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
* 将字节流转化为字符流
*/
public void h1(){
try {
FileOutputStream f=new FileOutputStream("e:\\io.txt",true);
OutputStreamWriter ff=new OutputStreamWriter(f);
try {
ff.write("\r\n希望你能够出现在我面前");
ff.flush();
ff.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void h2(){
try {
FileInputStream f=new FileInputStream("e:\\io.txt");
InputStreamReader ff=new InputStreamReader(f);
char[] c=new char[200];
int n;
try {
n = ff.read(c);
String str=new String(c,0,n);
System.out.println(str);
ff.close();
f.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/*
* 比较好的读的方式
*/
public void hh(){
try {
FileReader f=new FileReader("e:\\io.txt");
BufferedReader ff=new BufferedReader(f);
while(ff.ready()){
System.out.println(ff.readLine());
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
TestIO test=new TestIO();
test.CreateFile();
//test.set1();
//test.get1();
//test.set2();
//test.get2();
//test.set3();
//test.get3();
//test.set4();
//test.get4();
//test.h1();
//test.h2();
test.hh();
}
}