## IO流
### 文件--基本概念
文件是數據源(保存數據的地方)的一種,比如大word文檔、jpg文件、MP4文件...都是文件。文件最主要的作用就是保存數據,它既可以保存一張圖片,也可以保存視頻、聲音...等
?
### 文件流--基本概念
文件在程序中是以流的形式來操作的。

流:數據在數據源(文件)和程序(內存)之間經歷的路徑
輸入流:數據從數據源(文件)到程序(內存)的路徑
輸出流:數據從程序(內存)到數據源(文件)的路徑
### 如何判斷是輸入流、輸出流?
以內存為參照,如果數據流向內存流動,則是輸入流;反之,則是輸出流。
### 文件流--分類

### java流分為兩種流
1、字節流:可以用于讀寫二進制文件及任何類型文件
2、字符流:可以用于讀寫文本文件,不能操作二進制文件
### 實例1.File類的使用
~~~
/**
* File類的基本用法
*/
package com.io;
import java.io.*;
public class IO1 {
public static void main(String[] args) {
//創建一個文件對象
File f1 = new File("C:\\in.txt");
//得到文件路徑
System.out.println("文件路徑:" + f1.getAbsolutePath());
//得到文件的大小,字節數
System.out.println("文件大小:" + f1.length());
//是否可讀
System.out.println("可讀" + f1.canRead());
//創建文件和創建文件夾
File f2 = new File("C:\\in2.txt");
//判斷文件是否存在
if(!f2.exists()){
//創建一個文件
try {
f2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("文件創建成功");
} else {
System.out.println("文件已存在,無法創建");
}
File f3 = new File("C:\\file1");
//判斷文件是否為文件夾
if(f3.isDirectory()){
System.out.println("文件夾已存在");
} else {
//創建文件夾
f3.mkdir();
System.out.println("文件夾已創建");
}
//列出一個文件夾下面的所有文件
File f4 = new File("C:\\");
if(f4.isDirectory()){
//獲取文件數組
File[] lists = f4.listFiles();
for(int i = 0; i < lists.length; i++){
System.out.println("文件名:" + lists[i].getName());
}
}
}
}
~~~
### 實例2.文件字節流的使用
~~~
/**
* FileInputStream類的使用
*/
package com.io;
import java.io.*;
public class IO2 {
public static void main(String[] args) {
//創建一個文件對象
File f = new File("C:\\in.txt");
FileInputStream fis = null;
//File無讀寫能力,所以需要使用InputStream進行讀入
try {
fis = new FileInputStream(f);
//定義一個字節數組,相當于緩存
byte[] bytes = new byte[1024];
//得到實際讀取到的字節數
int n = 0;
//循環讀取
while((n = fis.read(bytes)) != -1){
//把字節轉換成String
String s = new String(bytes, 0, n);
System.out.println(s);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關閉文件流--必須放這里
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~
~~~
/**
* FileOutputStream類的使用
*/
package com.io;
import java.io.*;
public class IO3 {
public static void main(String[] args) {
//創建文件對象
File f = new File("C:\\out2.txt");
//字節輸出流
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
String s = "hello,world\r\n";
String s2 = "hello,java\r\n";
//定義字節數組
//byte[] bytes = new byte[1024];
fos.write(s.getBytes());
fos.write(s2.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~
~~~
/**
* 圖片拷貝
*/
package com.io;
import java.io.*;
public class IO4 {
public static void main(String[] args) {
//先把圖片讀入到內存 -> 寫入到某個文件
//因為是二進制文件,因此只能用字節流完成
//輸入流
FileInputStream fis = null;
//輸出流
FileOutputStream fos = null;
try {
fis = new FileInputStream(new File("C:\\image01.jpg"));
fos = new FileOutputStream(new File("C:\\image01_copy.jpg"));
byte[] buf = new byte[1024];
//記錄實際讀取到的字節
int n = 0;
//循環讀取
while((n = fis.read(buf)) != -1){
//輸出到指定文件
fos.write(buf);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關閉打開的文件流
try {
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~
### 實例3.文件字節流
~~~
/**
* 字符流
*/
package com.io;
import java.io.*;
public class IO5 {
public static void main(String[] args) {
//文件讀入字符流
FileReader fr = null;
//文件寫出字符流
FileWriter fw = null;
try {
//創建文件讀入字符流對象
fr = new FileReader(new File("C:\\test.txt"));
//創建文件寫出字符流對象
fw = new FileWriter(new File("C:\\test_copy.txt"));
//讀入到內存
//緩存char數組
char[] c = new char[1024];
//讀入實際大小
int n = 0;
while((n = fr.read(c)) != -1){
fw.write(c, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關閉文件流
try {
fr.close();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~
### 實例4.緩存字節流
~~~
/**
* 緩沖字符流
*
*/
package com.io;
import java.io.*;
public class IO6 {
public static void main(String[] args) {
//緩沖字符流定義
BufferedReader br = null;
BufferedWriter bw = null;
try {
//創建FileReader對象
FileReader fr = new FileReader(new File("C:\\test.txt"));
//創建FileWriter對象
FileWriter fw = new FileWriter(new File("C:\\test_copy2.txt"));
//創建緩沖字符流
br = new BufferedReader(fr);
bw = new BufferedWriter(fw);
//循環讀文件
//臨時字符串
String s = "";
while((s = br.readLine()) != null){
//輸出到文件
bw.write(s + "\r\n");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//關閉緩沖字符流
try {
br.close();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
~~~
### 實例5.記事本
~~~
/**
* 記事本(界面+功能)
*/
package com.notepad;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class NotePad extends JFrame implements ActionListener{
//定義組件
//文本域
JTextArea jta = null;
//滾動條
JScrollPane jsp = null;
//菜單條
JMenuBar jmb =null;
//菜單欄目
JMenu jm = null;
//菜單項
JMenuItem jmi1 = null;
JMenuItem jmi2 = null;
//構造方法
public NotePad(){
//創建組件
jta = new JTextArea();
jsp = new JScrollPane(jta);
jmb = new JMenuBar();
jm = new JMenu("文件(F)");
jmi1 = new JMenuItem("打開(O)");
jmi2 = new JMenuItem("保存(S)");
//設置助記符
jm.setMnemonic('F');
jmi1.setMnemonic('O');
jmi2.setMnemonic('S');
//設置監聽器
jmi1.addActionListener(this);
jmi2.addActionListener(this);
//設置動作監聽器反應命令
jmi1.setActionCommand("open");
jmi2.setActionCommand("save");
//設置菜單條
setJMenuBar(jmb);
//把菜單欄目放入菜單條
jmb.add(jm);
//菜單項放入菜單欄
jm.add(jmi1);
jm.add(jmi2);
//加入到JFrame
add(jsp);
//設置窗體
setTitle("我的記事本");
setSize(400,300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args) {
NotePad np = new NotePad();
}
//動作監聽器實現
@Override
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("open")){
//文件選擇框
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("打開文件");
jfc.showOpenDialog(null);
jfc.setVisible(true);
String file = jfc.getSelectedFile().getAbsolutePath();
//設置緩沖讀入流
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(new File(file)));
//臨時字符串
String s = "";
String all = "";
while((s = br.readLine()) != null){
//因為readLine方法會去掉回車換行
all += s + "\r\n";
}
jta.setText(all);
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} else if(e.getActionCommand().equals("save")){
//文件選擇框
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("保存文件");
jfc.showSaveDialog(null);
jfc.setVisible(true);
String file = jfc.getSelectedFile().getAbsolutePath();
//設置緩沖寫出流
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(new File(file)));
//臨時存放JTextArea中的字符串
String s = jta.getText();
//將字符串按一行分割成字符串數組
String[] ss = s.split("\r\n");
//循環寫入寫出流
for(int i = 0; i < ss.length; i++){
bw.write(ss[i] + "\r\n");
}
} catch (Exception e2) {
e2.printStackTrace();
} finally {
try {
bw.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
} else {
System.out.println("無效動作");
}
}
}
~~~
----------參考《韓順平.循序漸進學.java.從入門到精通》
----------參考《JDK_API_1_6_zh_CN》
Java學習筆記--導航[http://blog.csdn.net/q547550831/article/details/49819641](http://blog.csdn.net/q547550831/article/details/49819641)