001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.io; 018 019import java.io.File; 020import java.io.FileNotFoundException; 021import java.io.RandomAccessFile; 022import java.nio.file.Path; 023 024/** 025 * Access modes and factory methods for {@link RandomAccessFile}. 026 * 027 * @since 2.12.0 028 */ 029public enum RandomAccessFileMode { 030 031 /** 032 * Mode {@code "r"} opens for reading only. 033 */ 034 READ_ONLY("r"), 035 036 /** 037 * Mode {@code "rw"} opens for reading and writing. 038 */ 039 READ_WRITE("rw"), 040 041 /** 042 * Mode {@code "rws"} opens for reading and writing, as with {@code "rw"}, and also require that every update to the file's content or metadata be written 043 * synchronously to the underlying storage device. 044 */ 045 READ_WRITE_SYNC_ALL("rws"), 046 047 /** 048 * Mode {@code "rwd"} open for reading and writing, as with {@code "rw"}, and also require that every update to the file's content be written synchronously 049 * to the underlying storage device. 050 */ 051 READ_WRITE_SYNC_CONTENT("rwd"); 052 053 private final String mode; 054 055 RandomAccessFileMode(final String mode) { 056 this.mode = mode; 057 } 058 059 /** 060 * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File} 061 * argument. 062 * 063 * @param file the file object 064 * @return a random access file stream 065 * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}. 066 */ 067 public RandomAccessFile create(final File file) throws FileNotFoundException { 068 return new RandomAccessFile(file, mode); 069 } 070 071 /** 072 * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File} 073 * argument. 074 * 075 * @param file the file object 076 * @return a random access file stream 077 * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}. 078 */ 079 public RandomAccessFile create(final Path file) throws FileNotFoundException { 080 return create(file.toFile()); 081 } 082 083 /** 084 * Constructs a random access file stream to read from, and optionally to write to, the file specified by the {@link File} 085 * argument. 086 * 087 * @param file the file object 088 * @return a random access file stream 089 * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}. 090 */ 091 public RandomAccessFile create(final String file) throws FileNotFoundException { 092 return new RandomAccessFile(file, mode); 093 } 094 095 @Override 096 public String toString() { 097 return mode; 098 } 099 100}