Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit06bd86e

Browse files
IO example
1 parent247fc64 commit06bd86e

File tree

2 files changed

+212
-0
lines changed

2 files changed

+212
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
packagesporadic.IO_example;
2+
3+
importjava.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+
publicclassJavaFileExample {
23+
24+
privatestaticfinalStringDIRECTORY_TO_CREATE ="/tmp/fileCreatedFromRunning-JavaFileExample";
25+
privatestaticfinalStringDIRECTORY_TO_LIST ="/Users/SteveSun/Downloads";
26+
27+
publicstaticvoidmain(String[]args) {
28+
mkDirectory(DIRECTORY_TO_CREATE);
29+
30+
listDirectories(DIRECTORY_TO_LIST);
31+
}
32+
33+
/**
34+
* @param dir TODO
35+
*
36+
*/
37+
staticvoidmkDirectory(StringDIRECTORY_TO_CREATE) {
38+
Filed =newFile(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+
staticvoidlistDirectories(StringDIRECTORY_TO_LIST) {
49+
Filefile =null;
50+
String[]paths;
51+
52+
try {
53+
// create new file object
54+
file =newFile(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 (Stringpath :paths) {
61+
// prints filename and directory name
62+
System.out.println(path);
63+
}
64+
}catch (Exceptione) {
65+
e.printStackTrace();
66+
}
67+
}
68+
69+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
packagesporadic.IO_example;
2+
3+
importjava.io.FileInputStream;
4+
importjava.io.FileNotFoundException;
5+
importjava.io.FileOutputStream;
6+
importjava.io.FileReader;
7+
importjava.io.FileWriter;
8+
importjava.io.IOException;
9+
importjava.io.InputStreamReader;
10+
11+
publicclassJavaIOExample {
12+
13+
privatestaticfinalStringINPUT ="/Users/SteveSun/Downloads/bash.sh";
14+
// "/tmp/input.txt";
15+
16+
privatestaticfinalStringOUTPUT ="/tmp/output.txt";
17+
18+
publicstaticvoidmain(String[]args)throwsIOException {
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+
staticvoidbyteStreamsIO()throwsIOException {// This type is
37+
// package-level access,
38+
// without public, protected
39+
// and private keywords,
40+
// this is package-level
41+
// method
42+
FileInputStreamfis =null;
43+
FileOutputStreamfos =null;
44+
45+
try {
46+
fis =newFileInputStream(INPUT);
47+
fos =newFileOutputStream(OUTPUT);
48+
49+
intc;
50+
while ((c =fis.read()) != -1) {
51+
fos.write(c);
52+
}
53+
}catch (FileNotFoundExceptione) {
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+
staticvoidcharacterStreamsIO()throwsFileNotFoundException,IOException {
79+
FileReaderin =null;
80+
FileWriterout =null;
81+
82+
try {
83+
in =newFileReader(INPUT);
84+
out =newFileWriter(OUTPUT);
85+
86+
intc;
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+
staticvoidstandardStreamsIO()throwsIOException {
125+
InputStreamReaderisr =null;
126+
127+
try {
128+
isr =newInputStreamReader(System.in);
129+
System.out.println("Enter characters, 'q' to quit.");
130+
charc;
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)

[8]ページ先頭

©2009-2025 Movatter.jp