4. 1. Path Class and File Class
4
β’ Path Class ααΉααΆ class αα½αααααααα»ααα ααα method αααααΆααααααΎααΆα ααΆαα½α information αααα Path αα ααα»α File
System α Path object αΆα file name αα·α directory list αααααΆαα ααΆααααααΎα Path, ααΆααααααΈααΆαα file αα·α ααααα½ααα·αα·
αα file/directoryα
Method and Description
get(String first, String... more) Converts a path string, or a sequence of strings that
when joined form a path string, to a Path.
get(URI uri) Converts the given URI to a Path object.
5. 5
β’ Create Path
import java.io.file.Path;
import java.io.file.Paths;
public class CreatePath {
private Path path; // instance variable
public static void main (String [] args) {
CreatePath example = new CreatePath();
example.createPath();
}
private void createPath() {
path = Paths.get("D:JCGarticlesPathfile1.txt");
System.out.println("Path created: " + path.toString());
}
}
1. Path Class and File Class
6. 1. Path Class and File Class
6
β’ Java File Class ααααααΎαααΎαααΈααααΎααΆαααΆαα½α file αα·α directory pathnamesα α αΎαααΆαααααααΆααΉαααααΆαα αααααΎα files αα·α
directories, file searching, file deletion .
S.N. Constructor & Description
1 File(File parent, String child)
This method creates a new File instance from a parent abstract pathname and a child pathname string.
2 File(String pathname)
This method creates a new File instance by converting the given pathname string into an abstract
pathname.
3 File(String parent, String child)
This method creates a new File instance from a parent pathname string and a child pathname string.
4 File(URI uri)
This method Creates a new File instance by converting the given file : URI into an abstract pathname.
7. 1. Path Class and File Class
7
β’ Create New file βtest.txtβ
import java.io.File;
public class CreateFile {
public static void main(String[] args) {
try {
File new_file = new File("test.txt");
} catch (Exception e) {
e.printStackTrace();
}
}
}
15. 5. InputSream / OutputStream
15
Java Application ααααΎα αα input stream αααΎαααΈααααΎααΆα read data ααΈ source αα·α ααααΎα αα output stream
αααΎαααΈααααΎααΆα write data αα ααΆαα destinationα
InputStream Class and OutputStream Class:ααΊααΆ abstract class αααααΆ subclass αααα classes ααΆαα αα
αααααΆαααΆααα’α input stream αααα bytes
αααΆααααΎ ααΆ ααα inputstream αα·α outputstream
Method of InputStream Method of OutputStream
1) public abstract int read()throws
IOException:
1) public void write(int)throws IOException:
2) public int available()throws IOException: 2) public void write(byte[])throws IOException:
3) public void close()throws IOException: 3) public void close()throws IOException:
16. 5. InputSream / OutputStream
16
Hairachy of InputStream/OutputStream
17. 6. InputSreamReader/OutputStreamWriter
17
Java.io.InputStreamReader class ααΊααΆααααΆ αα αααααΈ byte streams αα ααΆαα character streams
ααΆααααΎααΆα read ααΈ byte α αΎα decode ααΆ character αααααααΎα αα charset
α αΆα αααJava.io.OutputStreamWriter ααΆααααΎααΆα write α αΎα encode αα ααΆ byte βα α αααααααΎα αα charsetα
S.N. InputStreamReader: Constructor & Description OutputStreamWriter: Constructor & Description
1 InputStreamReader(InputStream in)
This creates an InputStreamReader that uses the default charset.
OutputStreamWriter(OutputStream out)
This creates an OutputStreamWriter that uses the default character
encoding.
2 InputStreamReader(InputStream in, Charset cs)
This creates an InputStreamReader that uses the given charset.
OutputStreamWriter(OutputStream out, Charset cs)
This creates an OutputStreamWriter that uses the given charset.
3 InputStreamReader(InputStream in, CharsetDecoder dec)
This creates an InputStreamReader that uses the given charset
decoder.
OutputStreamWriter(OutputStream out, CharsetEncoder enc)
This creates an OutputStreamWriter that uses the given charset
encoder.
4 InputStreamReader(InputStream in, String charsetName)
This creates an InputStreamReader that uses the named charset.
OutputStreamWriter(OutputStream out, String charsetName)
This creates an OutputStreamWriter that uses the named charset.
21. 8. RandomacessFile
21
Java.io.RandomAccessfile class ααΆααΆαααΈααΌα ααΆ Array αααΆ αααΆααα½αααα αα α»αααΆ bytes αα αα α»α file
ααα ααΆα’α αααΌα αααΆαα reaing αα·α writing αα ααΆ random access file.
Class constructor:Constructor Descriptio
RandomAccessFileFilefile, Stringmode This creates a random access file stream to read from, and optionally to
to, the file
specified by the File argument.
RandomAccessFileFilefile, Stringmode This creates a random access file stream to read from, and optionally to
to, a file
with the specified name.
22. 8. RandomacessFile
22
Methods Descriptio
void close This method Closes this random access file stream and releases any system
associated with the stream.
FileChannel getChannel This method returns the unique FileChannel object associated with this file.
FileDescriptor getFD This method returns the opaque file descriptor object associated with this stream.
long getFilePointer This method returns the current offset in this file.
long length This method returns the length of this file.
int read This method reads a byte of data from this file.
int readbyte[]b This method reads up to b.length bytes of data from this file into an array of bytes.
int readbyte[]b, intoff, intlen This method reads up to len bytes of data from this file into an array of bytes.
boolean readBoolean This method reads a boolean from this file.
byte readByte This method reads a signed eight-bit value from this file.
24. 8. RandomAcessFile
24
S.N. InputStreamReader: Method & Description OutputStreamWriter: Method & Description
1 void close()
This method closes the stream and releases any system resources
associated with it.
void close()
This method closes the stream, flushing it first.
2 String getEncoding()
This method returns the name of the character encoding being used by
this stream
String getEncoding()
This method returns the name of the character encoding being used by
this stream.
3 int read()
This method reads a single character.
void write(char[] cbuf, int off, int len)
This method writes a portion of an array of characters.
4 int read(char[] cbuf, int offset, int length)
This method reads characters into a portion of an array.
void write(int c)
This method writes a single character.
5 boolean ready()
This method tells whether this stream is ready to be read.
void write(String str, int off, int len)
This method writes a portion of a string.