Skip to content

Commit 91c413a

Browse files
author
turingfly
committed
IO
1 parent ae6c7e0 commit 91c413a

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

Java-8/src/io/FileClass.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package io;
2+
3+
import java.io.File;
4+
5+
/**
6+
*
7+
* @author chengfeili
8+
* Jun 13, 2017 10:33:04 PM
9+
*
10+
*/
11+
public class FileClass {
12+
13+
public void creat() {
14+
System.out.println(java.io.File.separator); // /
15+
File parent = new File("/home/smith");
16+
File child = new File(parent, "data/zoo.txt");
17+
}
18+
19+
public void fileObject() {
20+
File file = new File("/usr/local");
21+
System.out.println("File Exists: " + file.exists());
22+
if (file.exists()) {
23+
System.out.println("Absolute Path: " + file.getAbsolutePath());
24+
System.out.println("Is Directory: " + file.isDirectory());
25+
System.out.println("Parent Path: " + file.getParent());
26+
if (file.isFile()) {
27+
System.out.println("File size: " + file.length());
28+
System.out.println("File LastModified: " + file.lastModified());
29+
} else {
30+
for (File subfile : file.listFiles()) {
31+
System.out.println("\t" + subfile.getName());
32+
}
33+
}
34+
}
35+
}
36+
37+
public static void main(String[] args) {
38+
FileClass fc = new FileClass();
39+
fc.creat();
40+
fc.fileObject();
41+
}
42+
43+
}

Java-8/src/io/Streams.java

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package io;
2+
3+
/**
4+
*
5+
* @author chengfeili
6+
* Jun 13, 2017 10:41:57 PM
7+
*
8+
* The java.io API defines two sets of classes for reading and writing
9+
* streams: those with Stream in their name and those with Reader /
10+
* Writer in their name.
11+
*
12+
* Differences between Streams and Readers/Writers
13+
* 1. The stream classes are used for inputting and outputting all
14+
* types of binary or byte data.
15+
* 2. The reader and writer classes are used for inputting and
16+
* outputting only character and String data.
17+
*/
18+
public class Streams {
19+
20+
/**
21+
*
22+
* A low-level stream connects directly with the source of the data, such as
23+
* a file, an array, or a String
24+
*/
25+
public void lowLevelStreams() {
26+
27+
}
28+
29+
/**
30+
* A high-level stream is built on top of another stream using wrapping.
31+
* Wrapping is the process by which an instance is passed to the constructor
32+
* of another class and operations on the resulting instance are filtered
33+
* and applied to the original instance.
34+
*/
35+
public void highLevelStreasm() {
36+
37+
}
38+
39+
/**
40+
* The java.io library defines four abstract classes that are the parents of
41+
* all stream classes defined within the API: InputStream , OutputStream ,
42+
* Reader , and Writer .
43+
*
44+
* The constructors of high-level streams often take a reference to the
45+
* abstract class. For example, BufferedWriter takes a Writer object as
46+
* input, which allows it to take any subclass of Writer .
47+
*/
48+
public void streamBaseClasses() {
49+
50+
}
51+
52+
public static void main(String[] args) {
53+
Streams stream = new Streams();
54+
55+
}
56+
}

Java-8/src/io/StreamsContinue.java

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package io;
2+
3+
import java.io.*;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
/**
8+
*
9+
* @author chengfeili
10+
* Jun 13, 2017 11:03:58 PM
11+
*
12+
*/
13+
public class StreamsContinue {
14+
// FileInputStream
15+
// FileOutputStream
16+
public void fileInputOutputStream() throws IOException {
17+
File source = new File("Zoo.class");
18+
File destination = new File("ZooCopy.class");
19+
try (InputStream in = new FileInputStream(source);
20+
OutputStream out = new FileOutputStream(destination)) {
21+
int b;
22+
while ((b = in.read()) != -1) {
23+
out.write(b);
24+
}
25+
}
26+
}
27+
28+
/**
29+
* Instead of reading the data one byte at a time, we use the underlying
30+
* read(byte[]) method of BufferedInputStream , which returns the number of
31+
* bytes read into the provided byte array.
32+
*
33+
* Why use the buffered classes?
34+
*
35+
* The BufferedInputStream class is capable of retrieving and storing in
36+
* memory more data than you might request with a single read() call. For
37+
* successive calls to the read() method with small byte arrays, this would
38+
* be faster in a wide variety of situations, since the data can be returned
39+
* directly from memory without going to the file system.
40+
*/
41+
public void bufferedInputOutputStream() throws IOException {
42+
File source = new File("Zoo.class");
43+
File destination = new File("ZooCopy.class");
44+
try (InputStream in = new BufferedInputStream(new FileInputStream(source));
45+
OutputStream out = new BufferedOutputStream(new FileOutputStream(destination))) {
46+
byte[] buffer = new byte[1024];
47+
int lengthRead;
48+
while ((lengthRead = in.read(buffer)) > 0) {
49+
out.write(buffer, 0, lengthRead);
50+
// Ensure that the written data actually makes it to disk before
51+
// the next buffer of data is read.
52+
out.flush();
53+
}
54+
}
55+
}
56+
57+
public void fileReaderWiter() throws IOException {
58+
59+
}
60+
61+
// BufferedReader
62+
public static List<String> readFile(File source) throws IOException {
63+
List<String> data = new ArrayList<String>();
64+
try (BufferedReader reader = new BufferedReader(new FileReader(source))) {
65+
String s;
66+
while ((s = reader.readLine()) != null) {
67+
data.add(s);
68+
}
69+
}
70+
return data;
71+
}
72+
73+
// BufferedWriter
74+
public static void writeFile(List<String> data, File destination) throws IOException {
75+
try (BufferedWriter writer = new BufferedWriter(new FileWriter(destination))) {
76+
for (String s : data) {
77+
writer.write(s);
78+
}
79+
}
80+
}
81+
82+
public static void main(String[] args) throws IOException {
83+
84+
}
85+
}

0 commit comments

Comments
 (0)