什麼是輸出流?
程序能向其中寫入數據的流
程序 -> 文件
稱爲輸出(output 寫文件)
舉個例子,就像學生寫作業 交給老師
什麼叫輸入流?
程序可以從中讀取數據的流
文件 -> 程序
稱爲輸入(input 讀文件)
一樣的例子 就像老師改完的作業 拿去給學生閱讀
字節輸出流
OutputStream
是一個抽象類,是所有輸出字節流的父類
因爲是抽象類,所以不能直接實例化對象
需要子類來重寫
這裏我們用FileOutputStream這個子類來操作文件
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/su.txt");
FileOutputStream oStream = new FileOutputStream(file);
oStream.write(49);
oStream.write(48);
byte[] b = {65,66,67};
oStream.write(b);
oStream.write(b, 0, 3);
oStream.write("hello".getBytes());
oStream.write("world".getBytes());
oStream.close();
}
}
文件的續寫
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/sudie.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("hello\n".getBytes());
oStream.close();
}
}
異常處理
public class Demo {
public static void main(String[] args) {
File file = new File("/Users/s/Desktop/Test/sudie.txt");
FileOutputStream oStream = null;
try{
oStream = new FileOutputStream(file);
o.Stream.write("hello".getBytes());
} catch (FileNotFoundException e){
throw new RuntimeException("文件找不到");
} catch(IOException e){
throw new RuntimeException("文件編寫失敗");
} finally{
try{
if(oStream != null) {
oStream.close();
}
} catch (IOException e){
throw new RuntimeException("關閉失敗")
}
}
}
}
字節輸入流
InputStream 是所有輸入流的父類
注意: 字節流寫入的時候 是一個字節一個字節的寫
: 讀取也是一個字節一個字節的讀
讀取文件流程
1.綁定數據源文件(要讀哪個文件)
2.使用read方法讀文件
3.關閉資源
在你想要讀的路徑下創建一個txt文件 這裏我取名爲 su.txt
在裏面輸入 abcde保存一下
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/su.txt");
FileInputStream iStream = new FileInputStream(file);
int i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println(i1);
iStream.close();
}
}
如果要讀取的文件中元素有很多,像上面一樣打,需要大量時間,所以可以使用循環
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/su.txt");
FileInputStream iStream = new FileInputStream(file);
int num = 0;
while ((num = iStream.read()) != -1) {
System.out.println(num);
}
}
文件的複製
public class Demo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("/Users/lanou/Desktop/Test/taytay.jpg");
fos = new FileOutputStream("/Users/lanou/Desktop/Test/Taylor.jpg");
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件未找到");
} catch (IOException e) {
throw new RuntimeException("文件複製失敗");
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
} finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
throw new RuntimeException("關閉資源失敗");
}
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
將一個文件夾 複製 另一個文件夾下
public class Demo02 {
public static void main(String[] args) throws IOException {
File oldFile = new File("/Users/lanou/Desktop/Test");
File newFile = new File("/Users/lanou/Desktop/XTest");
copyFile(oldFile, newFile);
}
public static void copyFile(File oldFile,File newFile) throws IOException {
File newDir = new File(newFile, oldFile.getName());
newDir.mkdir();
File[] listFiles = oldFile.listFiles();
int len = 0;
for (File subFile : listFiles) {
if (subFile.isFile()) {
FileInputStream fis = new FileInputStream(subFile);
File tempFile = new File(newDir, subFile.getName());
FileOutputStream fos = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
}else {
copyFile(subFile, newDir);
}
}
}
}
將一個文件夾的所有txt文件 複製 到另一個文件夾下
public class Demo03 {
public static void main(String[] args) throws IOException {
File oldFile = new File("/Users/lanou/Desktop/Test");
File newFile = new File("/Users/lanou/Desktop/XTest");
getOldFlieTxtToNewFile(oldFile, newFile);
}
public static void getOldFlieTxtToNewFile(File oldFile, File newFile) throws IOException {
File[] listFiles = oldFile.listFiles(new TXTFiles());
for (File subFile : listFiles) {
if (subFile.isFile()) {
FileInputStream fis = new FileInputStream(subFile);
File tempFile = new File(newFile, subFile.getName());
FileOutputStream fos = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int len = 0;
if ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
}else {
getOldFlieTxtToNewFile(subFile, newFile);
}
}
}
}
class TXTFiles implements FileFilter{
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
return pathname.getName().endsWith("txt");
}
}
Day.24
http://blog.csdn.net/ssssssue