StandardOpenOptions - Enum constants. Note, before was StandardCopyOptions.
1. WRITE - to write data
2. APPEND - append the new data to the end of the file. Used together with WRITE and CREATE.
3. TRUNCATE_EXISTING - truncate the file to zero bytes. Used together with WRITE.
4. CREATE_NEW - creates a new file and throws exception if it does exist.
5. CREATE - opens a file if it does exist or create a new one if does not.
6. DELETE_ON_CLOSE - deletes the file with the stream is close.
7. SPARSE - hints that the newly created file will be scattered to consume the empty gaps in the storage.
8. SYNC - keep the file content and metadata synchronized with the storage.
9. DSYNC - keep the file content synchronized with the storage.
For Small files only
1. File.readAllBytes(Path)
2. File.readAllLines(Path, Charset)
3. write(Path, byte[], OpenOption)
4. write(Path, Iterable<extends CharSequence>, Charset, OpenOption)
Add this to the previous code
=================
private static void readingSmallFiles(Path thePath) throws IOException{
if(Files.exist(thePath)){
List<String> message = Files.readAllLines(thePath);
System.out.println(message);
}
}
private static void writingToSmallFiles(Path thePath) throws IOException{
try(Scanner scanner = new Scanner(System.in)){
List<String> stringList = new ArrayList<>();
while(scanner.hasNext()){
String message = scanner.nextLine();
if(message.equals("999")){
break;
}else {
stringList.add(message);
}
}
if(Files.exist(thePath)){
Files.write(thePath, stringList, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE);
}else{
Files.write(thePath, stringList, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
}
}
private static void appendingToAFile(Path thePath) throws IOException{
try(Scanner scanner = new Scanner(System.in)){
List<String> stringList = new ArrayList<>();
while(scanner.hasNext()){
String message = scanner.nextLine();
if(message.equals("999")){
break;
}else{
stringList.add(message);
}
}
if(Files.exist(thePath)){
Files.write(thePath, stringList, StandardOpenOption.APPEND);
}else {
Files.write(thePath, stringList, StandardOpenOption.CREATE, StandardOpenOption.WRITE);
}
}
}
=================
YOU ARE READING
Java Programming
Acakhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...