Stanislav Zorjan - Stasha - Full Stack Software Engineer and Game Development Hobbyist - Prague


1. Java - Primitives
2. Java - Strings
3. Java - Arrays
4. Java - Date
5. Java - Generics
6. Java - Collections
7. Java - Localization
8. Java - Formatting
9. Java - Regular Expressions
10. Java - System
11. Java - Serialization
12. Java - Multithreading
13. Java - IO/File
14. Java - Networking
15. Java - ORM - JPA
      a) Simple User entity class

 

 

package basics.basicsExamples;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;

/** ****************************************************************************
 * IO FILE EXAMPLE CLASS
 * @author stanislav.zorjan
 */
public class IOFileExample {
    
    // Byte Streams
    // http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html
    // http://docs.oracle.com/javase/7/docs/api/java/io/OutputStream.html
    private FileInputStream fis = null;
    private FileOutputStream fos = null;
    
    // Character streams
    // http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html
    // http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html
    private BufferedReader br = null;
    private BufferedWriter bw = null;
    
    Path path = null;
    File file = new File("D:\\txt_test.txt");
    
    /** ************************************************************************
     * Default constructor
     */
    public IOFileExample() {
	createPath();
	createTempFile();
	pathOperations();
	fileOperations();
	try{
	    nioFilesOperations();
	}catch(IOException ex){}
	
	//readFile();
	//writeFile();
    }
    
    /** ************************************************************************
     * Method for creating path
     */
    private void createPath(){
	path = new File("D:\\").toPath();
    }
    
    /** ************************************************************************
     * Path operations
     */
    private void pathOperations(){
	System.out.println(path.compareTo(path));
	System.out.println(path.endsWith(file.toPath()));
	System.out.println(path.endsWith(".txt"));
	System.out.println(path.equals(path));
	System.out.println(path.getFileName());
	System.out.println(path.getFileSystem());
	//System.out.println(path.getName(0));
	System.out.println(path.getNameCount());
	System.out.println(path.getParent());
	System.out.println(path.getRoot());
	System.out.println(path.hashCode());
	System.out.println(path.isAbsolute());
	System.out.println(path.iterator());
	System.out.println(path.normalize());
	System.out.println(path.relativize(path));
	System.out.println(path.resolve(path));
	System.out.println(path.resolve("D:"));
	System.out.println(path.resolveSibling(path));
	System.out.println(path.resolveSibling("D:"));
	System.out.println(path.startsWith(path));
	System.out.println(path.startsWith("txt"));
	//System.out.println(path.subpath(0, 1));
	System.out.println(path.toAbsolutePath());
	System.out.println(path.toFile());
	//System.out.println(path.toRealPath(los));
	System.out.println(path.toString());
	System.out.println(path.toUri());
    }
    
    /** ************************************************************************
     * File operations
     */
    private void fileOperations(){
	System.out.println(file.canExecute());
	System.out.println(file.canRead());
	System.out.println(file.canWrite());
	System.out.println(file.compareTo(path.toFile()));
	//System.out.println(file.createNewFile());
	//System.out.println(file.delete());
	System.out.println(file.equals(path.toFile()));
	System.out.println(file.exists());
	System.out.println(file.getAbsoluteFile());
	System.out.println(file.getAbsolutePath());
	//System.out.println(file.getCanonicalFile());
	//System.out.println(file.getCanonicalPath());
	System.out.println(file.getFreeSpace());
	System.out.println(file.getName());
	System.out.println(file.getParent());
	System.out.println(file.getParentFile().getAbsolutePath());
	System.out.println(file.getPath());
	System.out.println(file.getTotalSpace());
	System.out.println(file.getUsableSpace());
	System.out.println(file.hashCode());
	System.out.println(file.isAbsolute());
	System.out.println(file.isDirectory());
	System.out.println(file.isFile());
	System.out.println(file.isHidden());
	System.out.println(file.lastModified());
	System.out.println(file.length());
	System.out.println(Arrays.toString(file.list()));
	
	FilenameFilter ff = new FilenameFilter() {
	    @Override
	    public boolean accept(File file, String string) {
		if(file.getName().endsWith(".txt")){
		   return true; 		
		}
		
		return false;
	    }
	};
	
	System.out.println(file.list(ff));
	System.out.println(file.listFiles());
	System.out.println(file.listFiles(ff));
	//System.out.println(file.mkdir());
	//System.out.println(file.mkdirs());
	System.out.println(file.renameTo(file));
	System.out.println(file.setExecutable(true));
	System.out.println(file.setLastModified(10L));
	System.out.println(file.setReadOnly());
	System.out.println(file.setReadable(true));
	System.out.println(file.setWritable(true));
	System.out.println(file.toPath().toUri());
	System.out.println(file.toString());
	System.out.println(file.toURI());
    }
    
    /** ************************************************************************
     * NIO File operations
     */
    private void nioFilesOperations() throws IOException{
	//TODO NIO Files operations
	
	//Files.copy(path, fos);
	//Files.copy(fis, path, StandardCopyOption.REPLACE_EXISTING);
	//Files.copy(path, path, StandardCopyOption.ATOMIC_MOVE);
	
    }
    
   
    /** ************************************************************************
     * Method for creating temp file
     */
    private void createTempFile(){
	try{
	    
	    Path p = Files.createTempFile("txt_test", "txt");
	    //Path p = Files.createTempFile(path, "txt_test", "txt");
	    file = new File(p.toUri());
	    writeFile();
	}catch(IOException ex){
	    ex.printStackTrace();
	}
    }
    
    
    
    
    /** ************************************************************************
     * Reading file
     * @return
     */
    private StringBuilder readFile(){ 
	
	StringBuilder sb = new StringBuilder();
	
	
	try{
	    //br = new BufferedReader(new FileReader(file));
	    
	    fis = new FileInputStream(file);
	    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
	    br = new BufferedReader(isr);
	    
	    String str = null;
	    
	    while((str = br.readLine()) != null){
		System.out.println(str);
		sb.append(str+"\n");
	    }
	    
	}catch(IOException ex){
	    ex.printStackTrace();
	}finally{
	    try {
		br.close();
	    } catch (IOException ex) {
		ex.printStackTrace();
	    }
	}
	
	return sb;
    }
    
    
    
    /** ************************************************************************
     * Writing text to file
     */
    private void writeFile(){
	try{
	    StringBuilder sb = readFile();
	    
	    //bw = new BufferedWriter(new FileWriter(file));
	    
	    fos = new FileOutputStream(file);
	    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
	    bw = new BufferedWriter(osw);
	    
	    bw.write(sb.toString());
	    bw.append("фдсаф фдсафдсф4324 фдсЧЧ ЧЧѕџџ џџџЏЏЏЉЉЊЕ ЉЕРРЉОПЋЖ");
	    bw.flush();
	    
	}catch(IOException ex){
	    ex.printStackTrace();
	}finally{
	    try {
		br.close();
	    } catch (IOException ex) {
		ex.printStackTrace();
	    }
	}
    }
    
}