標籤:== lin 圖片 try catch post port leo sts
字元流
package jd_1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class jd_1 {
/**
* 字元流
*
* @param args
*/
public static void main(String[] args) {
// 建立BufferedReader用於讀取檔案
BufferedReader reader = null;
// 建立BufferedWriter用於寫入檔案
BufferedWriter writer = null;
// 建立FileReader用於儲存讀入的路徑
FileReader fis = null;
try {
fis = new FileReader("F:\\java IO\\簡答copy\\source.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 建立FileWriter用於儲存寫入的路徑
FileWriter fw = null;
try {
fw = new FileWriter("F:\\java IO\\簡答copy\\targetcopy.txt");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
reader = new BufferedReader(fis);
writer = new BufferedWriter(fw);
String line = null;
// 讀取的是字串
try {
// while ((line = reader.readLine()) != null) {
// try {
// Thread.sleep(500);
// } catch (InterruptedException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
// writer.write(line);
// }
// 通過數組作為中轉站
char[] c = new char[1024];
StringBuffer buffer = new StringBuffer();
int legin = fis.read(c);
while (legin != -1) {
buffer.append(c);
legin = fis.read();
fw.write(c);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.err.println("copy成功");
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
位元組流
public class copy {
public static void main(String[] args) {
// 複製前
FileInputStream file = null;
// 複製後
FileOutputStream file1 = null;
try {
// 複製前的路徑
file = new FileInputStream("F:\\java IO\\建立文字文件.txt");
// 複製後的路徑
file1 = new FileOutputStream("F:\\java IO\\copy.txt");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 建立一個字元數組作為中轉站
byte[] Words = new byte[1024];
// 記錄數組的長度
int len;
try {
while ((len = file.read(Words)) != -1) {
file1.write(Words, 0, Words.length);
System.out.println("copy成功");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (file != null) {
try {
file.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (file1 != null) {
try {
file1.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
二進位流
package bdqn4;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 二進位讀取
*
* @author Administrator
*
*/
public class Text {
public static void main(String[] args) {
// 建立流
// 實現讀寫操作
// 關閉流
DataInputStream dis = null;
DataOutputStream dos = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("F:\\java IO\\原圖片.jpg");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
FileOutputStream fos = null;
try {
fos = new FileOutputStream("F:\\java IO\\copy圖片\\copy.jpg");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dis = new DataInputStream(fis);
dos = new DataOutputStream(fos);
int temp;
try {
while ((temp = dis.read()) != -1) {
dos.write(temp);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
System.err.println("轉移成功");
if (dis != null) {
try {
dis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (dos != null) {
try {
dos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
判斷IO的的各種屬性及其方法
package bdqn1;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class text {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// 執行個體化對象指點判斷路徑
File file = new File("F:\\java IO\\建立文字文件.txt");
// 判斷.tet是否存在
if (file.exists()) {
System.out.println("當前檔案存在");
System.out.println("檔案的完整路徑" + file.getAbsolutePath());
System.out.println("檔案名稱" + file.getName());
System.out.println("檔案的相對路徑" + file.getPath());
System.out.println("檔案的上一級目錄" + file.getParent());
System.out.println("檔案的長度" + file.length());
if (file.isDirectory()) {
System.out.println("當前是檔案夾");
} else {
System.out.println("當前是不是檔案夾");
System.err.println("請輸入1刪除");
int number = input.nextInt();
if (number == 1) {
boolean bool = file.delete();
if (bool) {
System.out.println("刪除成功");
}
}
}
} else {
System.out.println("當前檔案不存在");
try {
boolean bool = file.createNewFile();
if (bool) {
System.out.println("建立成功");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
java中的IO 的樣本