Java 对象序列化和操作文件 正则表达式

来源:互联网 发布:荧光文字配图美图软件 编辑:程序博客网 时间:2024/04/30 05:54

Java支持对象序列化得机制,可以将任何对象写出到流中,并在之后将其读回。

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("1.txt");Employee harry = new Employee("harry",5000,1989,10,1)out.writeObject(harry);Employee e1 = (Employee)in.readObject();
要使用这种机制,所有对象类必须实现Serializable接口。

class Employee implements Serializable {...}

算法原理

1.每一个对象都关联一个序列号(serial number)2.每个对象第一次遇到时,保存到流中3.如果对象已经保存,写出“与序号x的对象相同”4.对于流中对象,第一次遇到其序列号时,构建它,并使用流中的数据初始化,然后记录顺序号和你对象之间的关联5.当遇到“与序列号x对象相同”时,获取这个顺序号相关联的对象引用。

Path类--java.nio.file.Paths

File f = new File("/Users/aron/Desktop/1.txt");Path path = f.toPath();//Flie to PathSystem.out.println(path.getParent());Path p = Paths.get("/usr", "absolute");  //拼接路径File file = p.toFile();//Path to FileSystem.out.println(p);  // 结果为/usr/absoluteSystem.out.println(p.resolve("abc"));  //如果是绝对路径,直接输出。如果是相对路径,返回拼接后路径

文件复制、移动

Files.copy(fromPath, toPath)Files.move(fromPath, toPath)//如果想覆盖同名文件,增加如下参数Files.copy(fromPath, toPath, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE)Files.delete(path)

目录迭代

FIles类可以产生一个Iterable对象,打印给定目录下所有文件示例:

Path visitPath = Paths.get("/Users/aron", "Documents", "Tencent Files");Files.walkFileTree(visitPath, new SimpleFileVisitor<Path>() {public FileVisitResult visitFile(Path path,BasicFileAttributes attrs) throws IOException {if (attrs.isRegularFile()) {System.out.println(path);}return FileVisitResult.CONTINUE;}public FileVisitResult visitFileFailed(Path path, IOException exc)throws IOException {return FileVisitResult.CONTINUE;}});

ZIP文件系统

FileSystem fs = FileSystems.newFileSystem(Paths.get("abc.zip"), null);Files.copy(fs.getPath("/usr/local"), visitPath); // 文件复制/** * 列出zip文档中的所有文件 */Files.walkFileTree(fs.getPath("/"), new SimpleFileVisitor<Path>() {public FileVisitResult visitFile(Path file,BasicFileAttributes attrs) throws IOException {System.out.println(file);return FileVisitResult.CONTINUE;}});

内存映射文件

step1 从文件获取一个通道

FileChannel channel = FileChannel.open(path,options)

step2 调用map方法,获取bytebuffer

三种模式:FIleChannel.MapMode.READ_ONLY, READ_WRITE, PRIVATE

step3 有了缓冲区,可以使用byteBuffer类进行读写数据

public static void main(String[] args) throws IOException {Path path = Paths.get("/Users/aron/Desktop/", "1.txt");System.out.println("Mapped File");long start = System.currentTimeMillis();long crcValue = checksumMappedFile(path);long end = System.currentTimeMillis();System.out.println(Long.toHexString(crcValue));System.out.println((end - start) + " milliseconds");}private static long checksumMappedFile(Path path) throws IOException {try (FileChannel channel = FileChannel.open(path)) {CRC32 crc = new CRC32();int length = (int) channel.size();MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);for (int i = 0; i < length; i++) {int c = buffer.get(i);crc.update(c);}return crc.getValue();}}

加锁机制:

FileLock lock = channel.lock();

锁定文件的一部分

FileLock lock(long start, long size, boolean shared)   //shared 为false,锁定文件读写,shared为true,允许多个进程读文件。

如果锁定了文件尾,而文件长度随后超出了锁定部分,那么新区域是未锁定的,要想全部锁定,可以使用Long.MAX_VALUE来表示尺寸

文件加锁机制是依赖于操作系统的,尽量在网络文件上使用。

正则表达式

Scanner in = new Scanner(System.in); System.out.println("Enter pattern : ");String input = in.nextLine();if (input == null || input.equals("")) {return;}Pattern pattern = Pattern.compile(input);while (true) {System.out.print("Enter String to match : ");String str = in.nextLine();Matcher matcher = pattern.matcher(str);if (matcher.matches()) {System.out.println("Match");int g = matcher.groupCount();if (g > 0) {for (int i = 0; i < str.length(); i++) {for (int j = 1; j <= g; j++) {if (i == matcher.start(j) && i == matcher.end(j)) {System.out.println("()");}}for (int j = 1; j <= g; j++) {if (i == matcher.start(j) && i != matcher.end(j)) {System.out.println("(");}}System.out.println(str.charAt(i));for (int j = 1; j <= g; j++) {if (i + 1 != matcher.start(j)&& i + 1 == matcher.end(j)) {System.out.println(")");}}}System.out.println();}} else {System.out.println("Not Match");}}


0 0