1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.io; 18 19 import java.io.File; 20 import java.io.FileNotFoundException; 21 import java.io.RandomAccessFile; 22 import java.nio.file.Path; 23 24 /** 25 * Access modes and factory methods for {@link RandomAccessFile}. 26 * 27 * @since 2.12.0 28 */ 29 public enum RandomAccessFileMode { 30 31 /** 32 * Mode "r" opens for reading only. 33 */ 34 READ_ONLY("r"), 35 36 /** 37 * Mode "rw" opens for reading and writing. 38 */ 39 READ_WRITE("rw"), 40 41 /** 42 * Mode "rws" opens for reading and writing, as with "rw", and also require that every update to the file's content or 43 * metadata be written synchronously to the underlying storage device. 44 */ 45 READ_WRITE_SYNC_ALL("rws"), 46 47 /** 48 * Mode "rwd" open for reading and writing, as with "rw", and also require that every update to the file's content be 49 * written synchronously to the underlying storage device. 50 */ 51 READ_WRITE_SYNC_CONTENT("rwd"); 52 53 private final String mode; 54 RandomAccessFileMode(final String mode)55 RandomAccessFileMode(final String mode) { 56 this.mode = mode; 57 } 58 59 /** 60 * Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File} 61 * argument. 62 * 63 * @param file the file object 64 * @return a random access file stream 65 * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}. 66 */ create(final File file)67 public RandomAccessFile create(final File file) throws FileNotFoundException { 68 return new RandomAccessFile(file, mode); 69 } 70 71 /** 72 * Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File} 73 * argument. 74 * 75 * @param file the file object 76 * @return a random access file stream 77 * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}. 78 */ create(final Path file)79 public RandomAccessFile create(final Path file) throws FileNotFoundException { 80 return create(file.toFile()); 81 } 82 83 /** 84 * Creates a random access file stream to read from, and optionally to write to, the file specified by the {@link File} 85 * argument. 86 * 87 * @param file the file object 88 * @return a random access file stream 89 * @throws FileNotFoundException See {@link RandomAccessFile#RandomAccessFile(File, String)}. 90 */ create(final String file)91 public RandomAccessFile create(final String file) throws FileNotFoundException { 92 return new RandomAccessFile(file, mode); 93 } 94 95 @Override toString()96 public String toString() { 97 return mode; 98 } 99 100 } 101