Skip to content

Commit 06bd86e

Browse files
IO example
1 parent 247fc64 commit 06bd86e

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package sporadic.IO_example;
2+
3+
import java.io.File;
4+
5+
/**
6+
* This is a class to demo File class in java.
7+
*
8+
* A directory is a File
9+
*
10+
* which can contain a list of other files and directories. You use File object
11+
* to create directories, to list down files available in a directory. For
12+
* complete detail check a list of all the methods which you can call on File
13+
* object and what are related to directories.
14+
*
15+
* Java automatically takes care of path separators on UNIX and Windows as per
16+
* conventions. If you use a forward slash (/) on a Windows version of Java, the
17+
* path will still resolve correctly.
18+
*
19+
* @author jiahuan
20+
*
21+
*/
22+
public class JavaFileExample {
23+
24+
private static final String DIRECTORY_TO_CREATE = "/tmp/fileCreatedFromRunning-JavaFileExample";
25+
private static final String DIRECTORY_TO_LIST = "/Users/SteveSun/Downloads";
26+
27+
public static void main(String[] args) {
28+
mkDirectory(DIRECTORY_TO_CREATE);
29+
30+
listDirectories(DIRECTORY_TO_LIST);
31+
}
32+
33+
/**
34+
* @param dir TODO
35+
*
36+
*/
37+
static void mkDirectory(String DIRECTORY_TO_CREATE) {
38+
File d = new File(DIRECTORY_TO_CREATE);
39+
// Create directory now.
40+
d.mkdirs();
41+
}
42+
43+
/**
44+
* You can use list() method provided by File object to list down all the
45+
* files and directories available in a directory as follows:
46+
* @param dir TODO
47+
*/
48+
static void listDirectories(String DIRECTORY_TO_LIST) {
49+
File file = null;
50+
String[] paths;
51+
52+
try {
53+
// create new file object
54+
file = new File(DIRECTORY_TO_LIST);
55+
56+
// array of files and directory
57+
paths = file.list();
58+
59+
// for each name in the path array
60+
for (String path : paths) {
61+
// prints filename and directory name
62+
System.out.println(path);
63+
}
64+
} catch (Exception e) {
65+
e.printStackTrace();
66+
}
67+
}
68+
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package sporadic.IO_example;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.FileReader;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.io.InputStreamReader;
10+
11+
public class JavaIOExample {
12+
13+
private static final String INPUT = "/Users/SteveSun/Downloads/bash.sh";
14+
// "/tmp/input.txt";
15+
16+
private static final String OUTPUT = "/tmp/output.txt";
17+
18+
public static void main(String[] args) throws IOException {
19+
20+
byteStreamsIO();
21+
22+
// characterStreamsIO();
23+
24+
// standardStreamsIO();
25+
}
26+
27+
/**
28+
* Java byte streams are used to perform input and output of 8-bit bytes.
29+
* Though there are many classes related to byte streams but the most
30+
* frequently used classes are: FileInputStream and FileOutputStream.
31+
* Following is an example which makes use of these two classes to copy an
32+
* input file into an output file:
33+
*
34+
* @throws IOException
35+
*/
36+
static void byteStreamsIO() throws IOException {// This type is
37+
// package-level access,
38+
// without public, protected
39+
// and private keywords,
40+
// this is package-level
41+
// method
42+
FileInputStream fis = null;
43+
FileOutputStream fos = null;
44+
45+
try {
46+
fis = new FileInputStream(INPUT);
47+
fos = new FileOutputStream(OUTPUT);
48+
49+
int c;
50+
while ((c = fis.read()) != -1) {
51+
fos.write(c);
52+
}
53+
} catch (FileNotFoundException e) {
54+
e.printStackTrace();
55+
} finally {
56+
if (fis != null) {
57+
fis.close();
58+
}
59+
if (fos != null) {
60+
fos.close();
61+
}
62+
}
63+
}
64+
65+
/**
66+
* Java Byte streams are used to perform input and output of 8-bit bytes,
67+
* where as Java Character streams are used to perform input and output for
68+
* 16-bit unicode. Though there are many classes related to character
69+
* streams but the most frequently used classes are: FileReader and
70+
* FileWriter Though internally FileReader uses FileInputStream and
71+
* FileWriter uses FileOutputStream but here major difference is that
72+
* FileReader reads two bytes at a time and FileWriter writes two bytes at a
73+
* time.
74+
*
75+
* @throws FileNotFoundException
76+
* @throws IOException
77+
*/
78+
static void characterStreamsIO() throws FileNotFoundException, IOException {
79+
FileReader in = null;
80+
FileWriter out = null;
81+
82+
try {
83+
in = new FileReader(INPUT);
84+
out = new FileWriter(OUTPUT);
85+
86+
int c;
87+
while ((c = in.read()) != -1) {
88+
out.write(c);
89+
}
90+
} finally {
91+
if (in != null) {
92+
in.close();
93+
}
94+
if (out != null) {
95+
out.close();
96+
}
97+
}
98+
}
99+
100+
/**
101+
* All the programming languages provide support for standard I/O where
102+
* user's program can take input from a keyboard and then produce output on
103+
* the computer screen. If you are aware if C or C++ programming languages,
104+
* then you must be aware of three standard devices STDIN, STDOUT and
105+
* STDERR. Similar way Java provides following three standard streams
106+
*
107+
* Standard Input: This is used to feed the data to user's program and
108+
* usually a keyboard is used as standard input stream and represented as
109+
* System.in.
110+
*
111+
* Standard Output: This is used to output the data produced by the user's
112+
* program and usually a computer screen is used to standard output stream
113+
* and represented as System.out.
114+
*
115+
* Standard Error: This is used to output the error data produced by the
116+
* user's program and usually a computer screen is used to standard error
117+
* stream and represented as System.err.
118+
*
119+
* Following is a simple program which creates InputStreamReader to read
120+
* standard input stream until the user types a "q":
121+
*
122+
* @throws IOException
123+
*/
124+
static void standardStreamsIO() throws IOException {
125+
InputStreamReader isr = null;
126+
127+
try {
128+
isr = new InputStreamReader(System.in);
129+
System.out.println("Enter characters, 'q' to quit.");
130+
char c;
131+
do {
132+
c = (char) isr.read();
133+
System.out.print(c);
134+
} while (c != 'q');
135+
} finally {
136+
if (isr != null) {
137+
isr.close();
138+
}
139+
System.out.println("\nProgram stopped.");
140+
}
141+
}
142+
143+
}

0 commit comments

Comments
 (0)