一、Java IO流按流向可分为输入流(读数据)和输出流(写数据),按操作对象分可分为字符流和字节流。下面是IO流相关类的关系图:
二、文件的复制:
</pre><p></p><p></p><pre code_snippet_id="1628667" snippet_file_name="blog_20160330_1_7791617" name="code" class="java"><pre name="code" class="java">/**
* @param fileSource 要复制的文件
* @param fileCopy 复制后的文件
*/
private static void copyFile(File fileSource, File fileCopy) {
InputStream is = null;
OutputStream os = null;
byte[] buffer = new byte[512];//每次读取512字节大小的数据
int totalBytes = 0;
try {
is = new FileInputStream(fileSource);
os = new FileOutputStream(fileCopy);
while ((totalBytes = is.read(buffer)) != -1) {
os.write(buffer, 0, totalBytes);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (is != null && os != null) {
try {
is.close();
os.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
/**用字符流读取文件txt格式的文件*/
private static void readFileByChar() {
Reader reader = null;
try {
File file=new File("d:/测试.txt");
//设置读取的编码格式为“gbk”,如果读取的编码格式和该文件的编码格式不一致,则读出来的会是乱码
reader = new InputStreamReader(new FileInputStream(file),"gbk");
int tempchar;
while ((tempchar = reader.read()) != -1) {
// 对于windows下,\r\n这两个字符在一起时,表示一个换行。
// 但如果这两个字符分开显示时,会换两次行。
// 因此,屏蔽掉\r,或者屏蔽\n。否则,将会多出很多空行。
if (((char) tempchar) != '\r') {
System.out.print((char) tempchar);
}
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/** 每次读一个字节数组大小的数据 ***/
private static void readByByteArray() {
File file = new File("d:/测试.txt");
InputStream is = null;
try {
StringBuilder sb = new StringBuilder();
is = new FileInputStream(file);
byte[] bytes = new byte[1024];
String str = "";
int length=0;
while ((length=is.read(bytes)) != -1) {
str = new String(bytes,0,length,"gbk");
sb.append(str);
}
System.out.println("-------sb---=" + sb.toString());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/* 用缓冲流读取文件 **/
private static void bufferReadFile() {
File file = new File("d:/测试.txt");
StringBuffer sb = new StringBuffer();
String s = "";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
while (((s = reader.readLine()) != null)) {
sb.append(s + "\n");
}
System.out.println("----readResult--=" + sb.toString());
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** 用StringReader读字符数据 **/
private static void stringReadTest() {
StringReader sr = new StringReader("马克思主义");
int totalChar;
char[] chars = new char[10];
StringBuilder sb = new StringBuilder();
String s = "";
try {
while ((totalChar = sr.read(chars)) != -1) {
s = new String(chars, 0, totalChar);
sb.append(s);
}
System.out.println("----result--=" + sb.toString());
sr.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**用PrintWriter写数据**/
private static void printWriterTest() {
File file = new File("d:/测试.txt");
PrintWriter writer = null;
OutputStream os = null;
try {
os = new FileOutputStream(file,true);//“true” 写操作是追加 在文件末尾的
writer = new PrintWriter(os);
writer.write("----哈哈,马克思");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
writer.close();
}
}