java.io.file
- path interface
- get(String, String)
- getFileName()
- getName(int)
- getNameCount()
- getParent(), getRoot()
- toAbsolutePath()
- toFile()
- paths class
- exists(path)
- notExists(path)
- isReadable(path)
- isWritable(path)
- isDirectory(path)
- isRegularFile(path)
- size(path)
- newDirectoryStream(path)
- createFile(path)
- createDirectory(path)
- createDirectories(path)
- files class
- Exceptions
- IOException
- EOFException
- FileNotFoundException
- FileAlreadyExistsException
- DirectoryNotEmptyException
import java.io.*;
import java.nio.file.*;
Path myp = Paths.get("./mydir");
String dir = "./src/db/files";
String filename = "db.txt";
Path dirPath = Paths.get(dir);
Path fPath = Paths.get(dir, filename);
File fName = fPath.toFile();
if (Files.notExists(fPath)) {
Files.createDirectories(fPath);
}
// fPath.getFileName() -> return String
// fPath.toAbsolutePath() -> return String
// Files.isWritable(fPath) -> return boolean
// Files.exists(dirPath) -> return boolean
// Files.isDirectory (dirPath) -> return boolean
DirectoryStream dirs = Files.newDirectoryStream(dirPath);
for (Path p: dirs) {
if (Files.isRegularFile (p) {
System.out.println (" " +
p.getFileName());
}
}
// Write data to file
// layered approach to get an object using constructor
try (PrintWriter out = new PrintWriter (
new BufferedWriter (
new FileWriter (productsFile))))
{
out.println ("printout line");
}
catch (IOException e)
{
System.out.println(e);
}
//Read data from the file
try (BufferedReader in = new BufferedReader(
new FileReader(myFile)))
{
String line = in.readLine();
while (line != null)
{
System.out.println(line);
line = in.readLine();
String[] columns = line.split("\t");
String name = columns[0];
String value = columns[1];
int type = Integer.parseInteger(columns[2]);
System.out.println(type);
}
}
catch (IOException e)
{
System.out.println(e);
}
// flush the buffer and close the input stream
in.close();
// Code that handles I/O exceptions
Path productsPath = Paths.get("products.txt");
if (Files.exists(productsPath)){ //defensive prog.
//prevent the FileNotFoundeException
File productsFile = productsPath.toFile();
try (BufferedReader in = new BufferedReader(
new FileReader(productsFile)))
{
String line = in.readLine();
//prevent EOFException
while(line != null){
System.out.println(line);
line = in.readLine();
}
}
catch (IOException e){
System.out.println(e);
}
} else{
System.out.println(
productsPath.toAbsolutePath() + " doesn't exist");
}
// ProductDAO interface
public interface ProductDAO
extends ProductReader, ProductWriter,
ProductConstants {}
import java.util.ArrayList;
public interface ProductReader
{
Product getProduct(String code);
ArrayList getProducts();
}
//The ProductWriter interface
public interface ProductWriter
{
boolean addProduct(Product p);
boolean updateProduct(Product p);
boolean deleteProduct(Product p);
}
//The ProductConstants interface
public interface ProductConstants
{
int CODE_SIZE = 4;
int DESCRIPTION_SIZE = 40;
}
java.nio.file (JDK 1.7+)
No comments:
Post a Comment