属性信息导入/导出
SaveProperties
package org.wp.io; import java.io.FileWriter; import java.io.IOException; import java.util.Properties; /** * 属性信息的导出 * * @author wp * */ public class SaveProperties { public static void main(String args[]) { try { // 当前系统属性集合作为 Properties对象返回 // Properties ps = System.getProperties(); Properties ps = new Properties(); ps.setProperty("name", "Scott"); ps.setProperty("password", "Tiger"); FileWriter fw = new FileWriter("F:\\temp\\props.txt"); /* * 以适合使用 load(Reader) 方法的格式,将此 Properties 表中的属性列表(键和元素对)写入输出字符 * writer - 输出字符流writer * comments - 属性列表的描述 */ ps.store(fw, "loginfo"); fw.close(); } catch (IOException e) { e.printStackTrace(); } } }
LoadProperties
package org.wp.io; import java.io.FileReader; import java.io.IOException; import java.util.Properties; /** * 属性信息的导入 * * @author wp * */ public class LoadProperties { public static void main(String args[]) { try { Properties ps = new Properties(); FileReader fr = new FileReader("F:/temp/props.txt"); // 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对) ps.load(fr); fr.close(); // 将属性列表输出到指定的输出流 ps.list(System.out); } catch (IOException e) { e.printStackTrace(); } } }
随机存取文件
RandomAccessFileTest
package org.wp.io; import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; /** * 随机访问文件的读取和写入 * * @author wp * */ public class RandomAccessFileTest { private File file; public static void main(String args[]) { RandomAccessFileTest raft = new RandomAccessFileTest(); raft.init(); raft.record("张三", 21); raft.record("李四", 33); raft.record("王五", 18); raft.record("张三", 36); raft.listAllRecords(); } public void init() { if (file == null) { file = new File("F:/temp/record.txt"); try { // 检查文件是否存在,若不存在则创建该文件 file.createNewFile(); } catch (IOException e) { e.printStackTrace(); } } } public void record(String record_breaker, int times) { try { RandomAccessFile raf = new RandomAccessFile(file, "rw"); boolean flag = false; // getFilePointer // 返回此文件中的当前偏移量 while (raf.getFilePointer() < raf.length()) { String name = raf.readUTF(); if (record_breaker.equals(name)) { raf.writeInt(times); flag = true; break; } else { // 尝试跳过输入的 n个字节以丢弃跳过的字节 raf.skipBytes(4); } } if (!flag) { raf.writeUTF(record_breaker); raf.writeInt(times); } raf.close(); } catch (IOException e) { e.printStackTrace(); } } public void listAllRecords() { try { RandomAccessFile raf = new RandomAccessFile(file, "r"); while (raf.getFilePointer() < raf.length()) { String name = raf.readUTF(); int times = raf.readInt(); System.out.println("name:" + name + "\trecord:" + times); } raf.close(); } catch (IOException e) { e.printStackTrace(); } } }
临时文件
TempFileTest
package org.wp.io; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import javax.swing.JButton; import javax.swing.JFrame; public class TempFileTest implements ActionListener { private File tempPath; public static void main(String args[]) { TempFileTest tft = new TempFileTest(); tft.init(); tft.createUI(); } public void init() { tempPath = new File("F:/temp/temp"); // exists() // 测试此抽象路径名表示的文件或目录是否存在 // isDirectory() // 测试此抽象路径名表示的文件是否是一个目录 if (!tempPath.exists() || !tempPath.isDirectory()) { // mkdir() // 创建此抽象路径名指定的目录 tempPath.mkdir(); } } public void createUI() { JFrame frame = new JFrame(); JButton jb = new JButton("创建临时文件"); jb.addActionListener(this); frame.add(jb, "North"); frame.setSize(350, 150); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } @Override public void actionPerformed(ActionEvent e) { try { // 在tempPath路径下创建临时文件"mytempfileXXXX.txt" // XXXX 是系统自动产生的随机数, tempPath对应的路径应事先存在 File tempFile = File.createTempFile("mytempfile", ".txt", tempPath); System.out.println(tempFile.getAbsolutePath()); FileWriter fw = new FileWriter(tempFile); PrintWriter pw = new PrintWriter(fw); pw.println("some info!"); pw.close(); // delete() // 删除此抽象路径名表示的文件或目录 // tempFile.delete(); // deleteOnExit() // 在虚拟机终止时,请求删除此抽象路径名表示的文件或目录 tempFile.deleteOnExit(); } catch (IOException e1) { e1.printStackTrace(); } } }