View on GitHub

CC

CC practice and cheat sheets

Files

tldr - writers write text files, streams write binary files

  File(String Path);
  File (Stirng Parent, String Child); // new File('path', 'filename')
  File(File Parent, String Child);// new File(new File('some path to folder'), 'fileName.txt')
  
  File obj = new File("/home/david/hello.txt");
  System.out.println(obj.getName());
  System.out.println(obj.separator);
  System.out.println(obj.getAbsolutePath());
  
  System.out.println(obj.getPath());
  System.out.println(obj.exists());
  System.out.println(obj.isDirectory());
  System.out.println(obj.isAbsolute());
  System.out.println(obj.canRead());
  System.out.println(obj.canWrite());
  
  
  String arr[] = obj.listFiles();
  
  //if file does not exist
  if(obj.createNewFile()){
      // do something
  }

Types of streams


Types of Output Streams

Method Description
int available() It is used to return the estimated number of bytes that can be read from the input stream.
int read() It is used to read the byte of data from the input stream.
int read(byte[] b) It is used to read up to b.length bytes of data from the input stream.
int read(byte[] b, int off, int len) It is used to read up to len bytes of data from the input stream.
long skip(long x) It is used to skip over and discards x bytes of data from the input stream.
FileChannel getChannel() It is used to return the unique FileChannel object associated with the file input stream.
FileDescriptor getFD() It is used to return the FileDescriptor object.
protected void finalize() It is used to ensure that the close method is call when there is no more reference to the file input stream.
void close() It is used to closes the stream.