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); 

相关文章
相关标签/搜索