java io經典代碼

package IO; css

import java.io.*; java

public class FileDirectoryDemo { css3

public static void main(String[] args) { app

// 若是沒有指定參數,則缺省爲當前目錄。 dom

if (args.length == 0) { ide

args = new String[] { "." }; 函數

try { ui

// 新建指定目錄的File對象。 spa

File currentPath = new File(args[0]); code

// 在指定目錄新建temp目錄的File對象。 

File tempPath = new File(currentPath, "temp"); 

// 用「tempPath」對象在指定目錄下建立temp目錄。 

tempPath.mkdir(); 

// 在temp目錄下建立兩個文件。 

File temp1 = new File(tempPath, "temp1.txt"); 

temp1.createNewFile(); 

File temp2 = new File(tempPath, "temp2.txt"); 

temp2.createNewFile(); 

// 遞歸顯示指定目錄的內容。 

System.out.println("顯示指定目錄的內容"); 

listSubDir(currentPath); 

// 更改文件名「temp1.txt」爲「temp.txt」。 

File temp1new = new File(tempPath, "temp.txt"); 

temp1.renameTo(temp1new); 

// 遞歸顯示temp子目錄的內容。 

System.out.println("更改文件名後,顯示temp子目錄的內容"); 

listSubDir(tempPath); 

// 刪除文件「temp2.txt」。 

temp2.delete(); 

// 遞歸顯示temp子目錄的內容。 

System.out.println("刪除文件後,顯示temp子目錄的內容"); 

listSubDir(tempPath); 

} catch (IOException e) { 

System.err.println("IOException"); 

// 遞歸顯示指定目錄的內容。 

static void listSubDir(File currentPath) { 

// 取得指定目錄的內容列表。 

String[] fileNames = currentPath.list(); 

try { http://www.huiyi8.com/css3/css3特效

for (int i = 0; i < fileNames.length; i++) { 

File f = new File(currentPath.getPath(), fileNames[i]); 

// 若是是目錄,則顯示目錄名後,遞歸調用,顯示子目錄的內容。 

if (f.isDirectory()) { 

// 以規範的路徑格式顯示目錄。 

System.out.println(f.getCanonicalPath()); 

// 遞歸調用,顯示子目錄。 

listSubDir(f); 

// 若是是文件,則顯示文件名,不包含路徑信息。 

else { 

System.out.println(f.getName()); 

} catch (IOException e) { 

System.err.println("IOException"); 

package IO; 

import java.io.*; 

public class FileExample { 

public FileExample() { 

super();// 調用父類的構造函數 

public static void main(String[] args) { 

try { 

String outfile = "demoout.xml"; 

// 定義了一個變量, 用於標識輸出文件 

String infile = "demoin.xml"; 

// 定義了一個變量, 用於標識輸入文件 

DataOutputStream dt = new DataOutputStream( 

new BufferedOutputStream(new FileOutputStream(outfile))); 

/** 

* 用FileOutputStream定義一個輸入流文件, 

* 而後用BuferedOutputStream調用FileOutputStream對象生成一個緩衝輸出流 

* 而後用DataOutputStream調用BuferedOutputStream對象生成數據格式化輸出流 

*/

BufferedWriter NewFile = new BufferedWriter(new OutputStreamWriter( 

dt, "gbk"));// 對中文的處理 

DataInputStream rafFile1 = new DataInputStream( 

new BufferedInputStream(new FileInputStream(infile))); 

/** 

*用FileInputStream定義一個輸入流文件, 

* 而後用BuferedInputStream調用FileInputStream對象生成一個緩衝輸出流 

* ,其後用DataInputStream中調用BuferedInputStream對象生成數據格式化輸出流 

*/

BufferedReader rafFile = new BufferedReader(new InputStreamReader( 

rafFile1, "gbk"));// 對中文的處理 

String xmlcontent = ""; 

char tag = 0;// 文件用字符零結束 

while (tag != (char) (-1)) { 

xmlcontent = xmlcontent + tag + rafFile.readLine() + '

NewFile.write(xmlcontent); 

NewFile.flush();// 清空緩衝區 

NewFile.close(); 

rafFile.close(); 

System.gc();// 強制當即回收垃圾,即釋放內存。 

} catch (NullPointerException exc) { 

exc.printStackTrace(); 

} catch (java.lang.IndexOutOfBoundsException outb) { 

System.out.println(outb.getMessage()); 

outb.printStackTrace(); 

} catch (FileNotFoundException fex) { 

System.out.println("fex" + fex.getMessage()); 

} catch (IOException iex) { 

System.out.println("iex" + iex.getMessage()); 

package IO; 

import java.io.*; 

public class FileRandomRW { 

// 須要輸入的person數目。 

public static int NUMBER = 3; 

public static void main(String[] args) { 

Persons[] people = new Persons[NUMBER]; 

people[0] = new Persons("張峯", 26, 2000, "N"); 

people[1] = new Persons("豔娜", 25, 50000, "Y"); 

people[2] = new Persons("李朋", 50, 7000, "F"); 

try { 

DataOutputStream out = new DataOutputStream(new FileOutputStream( 

"peoplerandom.dat")); 

// 將人員數據保存至「peoplerandom.dat」二進制文件中。 

writeData(people, out); 

// 關閉流。 

out.close(); 

// 從二進制文件「peoplerandom.dat」中逆序讀取數據。 

RandomAccessFile inOut = new RandomAccessFile("peoplerandom.dat", 

"rw"); 

Persons[] inPeople = readDataReverse(inOut); 

// 輸出讀入的數據。 

System.out.println("原始數據:"); 

for (int i = 0; i < inPeople.length; i++) { 

System.out.println(inPeople[i]); 

// 修改文件的第三條記錄。 

inPeople[2].setSalary(4500); 

// 將修改結果寫入文件。 

inPeople[2].writeData(inOut, 3); 

// 關閉流。 

inOut.close(); 

// 從文件中讀入的第三條記錄,並輸出,以驗證修改結果。 

RandomAccessFile in = new RandomAccessFile("peoplerandom.dat", "r"); 

Persons in3People = new Persons(); 

// 隨機讀第三條記錄。 

in3People.readData(in, 3); 

// 關閉流。 

in.close(); 

System.out.println("修改後的記錄"); 

System.out.println(in3People); 

} catch (IOException exception) { 

System.err.println("IOException"); 

// 將數據寫入輸出流。 

static void writeData(Persons[] p, DataOutputStream out) throws IOException { 

for (int i = 0; i < p.length; i++) { 

p[i].writeData(out); 

// 將數據從輸入流中逆序讀出。 

static Persons[] readDataReverse(RandomAccessFile in) throws IOException { 

// 得到記錄數目。 

int record_num = (int) (in.length() / Persons.RECORD_LENGTH); 

Persons[] p = new Persons[record_num]; 

// 逆序讀取。 

for (int i = record_num - 1; i >= 0; i--) { 

p[i] = new Persons(); 

// 文件定位。 

in.seek(i * Persons.RECORD_LENGTH); 

p[i].readData(in, i + 1); 

return p; 

class Persons { 

private String name; 

private int age; // 4個字節 

private double salary; // 8個字節 

private String married; 

public static final int NAME_LENGTH = 20; // 姓名長度 

public static final int MARRIED_LENGTH = 2; // 婚否長度 

public static final int RECORD_LENGTH = NAME_LENGTH * 2 + 4 + 8

+ MARRIED_LENGTH * 2; 

public Persons() { 

public Persons(String n, int a, double s) { 

name = n; 

age = a; 

salary = s; 

married = "F"; 

public Persons(String n, int a, double s, String m) { 

name = n; 

age = a; 

salary = s; 

married = m; 

public String getName() { 

return name;  

public int getAge() { 

return age; 

public double getSalary() { 

return salary; 

public String getMarried() { 

return married; 

public String setName(String n) { 

name = n; 

public int setAge(int a) { 

age = a; 

return age; 

public double setSalary(double s) { 

salary = s; 

return salary; 

public String setMarried(String m) { 

married = m; 

return married; 

// 設置輸出格式。 

public String toString() { 

return getClass().getName() + "[name=" + name + ",age=" + age 

+ ",salary=" + salary + ",married=" + married + "]"; 

// 寫入一條固定長度的記錄,即一我的的數據到輸出流。 

public void writeData(DataOutput out) throws IOException { 

FixStringIO.writeFixString(name, NAME_LENGTH, out); 

out.writeInt(age); 

out.writeDouble(salary); 

FixStringIO.writeFixString(married, MARRIED_LENGTH, out); 

// 寫入一條固定長度的記錄到隨機讀取文件中。 

private void writeData(RandomAccessFile out) throws IOException { 

FixStringIO.writeFixString(name, NAME_LENGTH, out); 

out.writeInt(age); 

out.writeDouble(salary); 

FixStringIO.writeFixString(married, MARRIED_LENGTH, out); 

 

// 隨機寫入一條固定長度的記錄到輸出流的指定位置。 

public void writeData(RandomAccessFile out, int n) throws IOException { 

out.seek((n - 1) * RECORD_LENGTH); 

writeData(out); 

// 從輸入流隨機讀入一條記錄,即一我的的數據。 

private void readData(RandomAccessFile in) throws IOException { 

name = FixStringIO.readFixString(NAME_LENGTH, in); 

age = in.readInt(); 

salary = in.readDouble(); 

married = FixStringIO.readFixString(MARRIED_LENGTH, in); 

// 從輸入流隨機讀入指定位置的記錄。 

public void readData(RandomAccessFile in, int n) throws IOException { 

in.seek((n - 1) * RECORD_LENGTH); 

readData(in); 

// 對固定長度字符串從文件讀出、寫入文件 

class FixStringIO { 

// 讀取固定長度的Unicode字符串。 

public static String readFixString(int size, DataInput in) 

throws IOException { 

StringBuffer b = new StringBuffer(size); 

int i = 0; 

boolean more = true; 

while (more && i < size) { 

char ch = in.readChar(); 

i++; 

if (ch == 0) { 

more = false; 

} else { 

b.append(ch); 

// 跳過剩餘的字節。 

in.skipBytes(2 * (size - i)); 

return b.toString(); 

// 寫入固定長度的Unicode字符串。 

public static void writeFixString(String s, int size, DataOutput out) 

throws IOException { 

int i; 

for (i = 0; i < size; i++) { 

char ch = 0; 

if (i < s.length()) { 

ch = s.charAt(i); 

out.writeChar(ch); 

相關文章
相關標籤/搜索