首先我们先看看整个IO流的体系图
由图中我们可以看出,Java的IO主要分为字节流和字符流两部分。每一部分都有相应的读和写操作,再往下就是各种操作的细分,比如BufferReader,InputerStreamReader之类,就是缓存读取,和字节流通向字符流的读取方式。
1. 先来看下简单的文件创建:
/**
* Created by Administrator on 2016/2/28.
*/
public class CreateFile {
public static void main(String[] args){
File file = new File("E:"+ File.separator+"newFile.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.创建目录:
/**
* Created by Administrator on 2016/2/28.
*/
public class CreateFile {
public static void main(String[] args){
File file = new File("E:/newFile.txt");
file.mkdir(); //创建一个文件夹
}
}
3.显示目录下面所有的文件:
/**
* Created by Administrator on 2016/2/28.
*/
public class listFile {
@Test
public void showAllFiles(){
String mkdirName = "E:"+ File.separator;
File file = new File(mkdirName);
String[] str = file.list();
// File[] fileArray=file.listFiles();
for(String names :str){
System.out.println(names);
}
}
}
1.FileReader
首先我们来看下最简单的,从文件中读取出文本信息
package com.joker.FileReader;
import org.junit.Test;
import java.io.*;
/**
* Created by jaycekon on 2016/3/10.
*/
public class ReadFile {
@Test
public void readFile(){
File file = new File("E:"+ File.separator+"hello.txt");
char[] buffer=new char[512];
int num = 0;
FileReader reader =null;
try {
reader = new FileReader(file);
reader.read(buffer);
System.out.println(buffer);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果:
hello
world!
这种方法适用于文件内容较少的情况
2.FileWriter
package com.Joker.Char;
import org.junit.Test;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* Created by Administrator on 2016/3/11.
*/
public class Write {
@Test
public void TestOutWriter(){
try {
FileWriter writer =new FileWriter("hello.txt");
writer.write("写入文件!\n");
char[] cs = {'a','b','c'};
writer.write(cs);
writer.write("覆盖原文!");
writer.close();
FileReader reader = new FileReader("hello.txt");
char[] ch = new char[100];
reader.read(ch);
System.out.println(ch);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
写入文件!
覆盖原文!
3.StringReader
package com.Joker.Char;
import org.junit.Test;
import java.io.IOException;
import java.io.StringReader;
/**
* Created by Administrator on 2016/3/11.
*/
public class StringRead {
@Test
public void TestString(){
StringReader reader = new StringReader("hello");
char[] ch = new char[100];
try {
reader.read(ch);
System.out.println(ch[1]);
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4.PrintWriter
package com.Joker.Char;
import org.junit.Test;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
/**
* Created by Administrator on 2016/3/11.
*/
public class printWrite {
@Test
public void testPrint(){
try {
Writer write= new PrintWriter(System.out);
write.write("hello world!");
write.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
Process finished with exit code 0
hello world!
5.BufferedReader
package com.Joker.Char;
import org.junit.Test;
import java.io.*;
/**
* Created by Administrator on 2016/3/11.
*/
public class BufferRead {
@Test
public void TestBufferRead(){
File file = new File("hello.txt");
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String str;
while((str=reader.readLine())!=null){
System.out.println(str);
}
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
hello world!==============>1
hello world!==============>2
hello world!==============>3
hello world!==============>4
hello world!==============>5
hello world!==============>6
hello world!==============>7
Process finished with exit code 0
6.BufferWriter
package com.Joker.Char;
import org.junit.Test;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* Created by Administrator on 2016/3/11.
*/
public class BufferedWrite {
@Test
public void testBufferWrite(){
try {
BufferedWriter writer = new BufferedWriter(new FileWriter("hello.txt"));
for(int i=0;i<10;i++){
writer.write("hello world! ++++++++"+i+"\n");
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
hello world! ++++++++0
hello world! ++++++++1
hello world! ++++++++2
hello world! ++++++++3
hello world! ++++++++4
hello world! ++++++++5
hello world! ++++++++6
hello world! ++++++++7
hello world! ++++++++8
hello world! ++++++++9
package com.Joker.chars;
import org.junit.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* Created by Administrator on 2016/3/11.
*/
public class FileInputStreams {
@Test
public void testFileInputStreams(){
File file = new File("hello.txt");
try {
FileInputStream inputStream = new FileInputStream(file);
byte[] by = new byte[100];
inputStream.read(by);
String str = new String(by);
System.out.println(str);
for(byte b:by){
System.out.print(b+" ");
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
运行结果:
hello world!
104 101 108 108 111 32 119 111 114 108 100 33
2.FileOutPutSream
package com.Joker.chars;
import org.junit.Test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Created by Administrator on 2016/3/11.
*/
public class FileOutSreams {
@Test
public void testFileOutSreams(){
File file = new File("hello.txt");
try {
FileOutputStream fileOutputStream = new FileOutputStream(file);
String str ="Hello Wrold!";
byte[] bytes =str.getBytes();
fileOutputStream.write(bytes);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
输出结果:
Hello Wrold!
参考文章:
http://blog.csdn.net/yczz/article/details/38761237
http://www.importnew.com/