Skip to content

Commit 5966cb0

Browse files
Chapter 18 IO/NIO btp170802
1 parent 0194f40 commit 5966cb0

41 files changed

Lines changed: 1363 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
eclipse.preferences.version=1
2+
encoding//src/btp/oneP/BufferToText.java=UTF-8

src/Data/IO.jpg

50.1 KB
Loading
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package btp.oneP;
2+
3+
import java.nio.charset.Charset;
4+
import java.util.Iterator;
5+
import java.util.SortedMap;
6+
7+
public class AvailableCharSets {
8+
9+
public static void main(String[] args) {
10+
SortedMap<String,Charset> charSets = Charset.availableCharsets();
11+
Iterator<String> it = charSets.keySet().iterator();
12+
while(it.hasNext()){
13+
String csName = it.next();
14+
System.out.print(csName);
15+
Iterator aliases = charSets.get(csName).aliases().iterator();
16+
if(aliases.hasNext()){
17+
System.out.print(": ");
18+
}
19+
while(aliases.hasNext()){
20+
System.out.print(aliases.next());
21+
if(aliases.hasNext()){
22+
System.out.print(", ");
23+
}
24+
}
25+
System.out.println();
26+
}
27+
}
28+
29+
}

src/btp/oneP/BasicFileOutput.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package btp.oneP;
2+
3+
import java.io.BufferedReader;
4+
import java.io.BufferedWriter;
5+
import java.io.FileWriter;
6+
import java.io.IOException;
7+
import java.io.PrintWriter;
8+
import java.io.StringReader;
9+
10+
public class BasicFileOutput {
11+
static String file = "C:\\Users\\dell\\Desktop\\IO\\io.txt";
12+
public static void main(String[] args) throws IOException {
13+
BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read("E:\\workspaces\\NewWork\\Study\\src\\btp\\oneP\\BasicFileOutput.java")));
14+
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));
15+
int lineCount = 1;
16+
String s;
17+
while((s = in.readLine()) != null){
18+
out.println(lineCount++ + " : " + s);
19+
}
20+
out.close();
21+
System.out.println(BufferedInputFile.read(file));
22+
}
23+
24+
}

src/btp/oneP/BinaryFile.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package btp.oneP;
2+
3+
import java.io.BufferedInputStream;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.IOException;
7+
import java.util.HashMap;
8+
import java.util.Map;
9+
10+
public class BinaryFile {
11+
public static byte[] read(File bFile) throws IOException{
12+
BufferedInputStream bf = new BufferedInputStream(new FileInputStream(bFile));
13+
try{
14+
byte[] data = new byte[bf.available()];
15+
bf.read(data);
16+
return data;
17+
}finally{
18+
bf.close();
19+
}
20+
}
21+
22+
public static byte[] read(String bFile) throws IOException{
23+
return read(new File(bFile).getAbsoluteFile());
24+
}
25+
public static void main(String[] args) throws IOException {
26+
byte[] bytes = read("E:\\workspaces\\NewWork\\Study\\src\\btp\\oneP\\BinaryFile.java");
27+
Map<Byte,Integer> map = new HashMap<Byte,Integer>();
28+
for(byte b : bytes){
29+
if(null == map.get(b)){
30+
map.put(b, 1);
31+
}else{
32+
map.put(b, map.get(b)+1);
33+
}
34+
}
35+
System.out.println(map);
36+
}
37+
38+
}

src/btp/oneP/BufferToText.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package btp.oneP;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileOutputStream;
5+
import java.io.IOException;
6+
import java.nio.ByteBuffer;
7+
import java.nio.channels.FileChannel;
8+
import java.nio.charset.Charset;
9+
10+
public class BufferToText {
11+
private static final int BSIZE = 1024;
12+
public static void main(String[] args) throws IOException {
13+
FileChannel fc = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
14+
fc.write(ByteBuffer.wrap("some text".getBytes()));
15+
fc.close();
16+
17+
fc = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
18+
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
19+
fc.read(buff);
20+
buff.flip();
21+
System.out.println(buff.asCharBuffer());
22+
buff.rewind();
23+
24+
String encoding = System.getProperty("file.encoding");
25+
System.out.println("Decoded using "+encoding+":"+Charset.forName(encoding).decode(buff));
26+
27+
fc = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
28+
fc.write(ByteBuffer.wrap("Some text".getBytes("GBK")));
29+
fc.close();
30+
31+
fc = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
32+
buff.clear();
33+
fc.read(buff);
34+
buff.flip();
35+
System.out.println(buff.asCharBuffer());
36+
System.out.println("Decoded using "+"GBK"+":"+Charset.forName("GBK").decode(buff));
37+
38+
fc = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
39+
buff = ByteBuffer.allocate(24);
40+
buff.asCharBuffer().put("Some text!");
41+
fc.write(buff);
42+
fc.close();
43+
44+
fc = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\data2.txt").getChannel();
45+
buff.clear();
46+
fc.read(buff);
47+
buff.flip();
48+
System.out.println(buff.asCharBuffer());
49+
}
50+
51+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package btp.oneP;
2+
3+
import java.io.BufferedReader;
4+
import java.io.FileReader;
5+
import java.io.IOException;
6+
7+
public class BufferedInputFile {
8+
9+
public static String read(String filename) throws IOException{
10+
BufferedReader im = new BufferedReader(new FileReader(filename));
11+
String s;
12+
StringBuilder sb = new StringBuilder();
13+
while((s = im.readLine()) != null){
14+
sb.append(s+"\n");
15+
}
16+
im.close();
17+
return sb.toString();
18+
}
19+
public static void main(String[] args) throws IOException {
20+
System.out.println(read("E:\\workspaces\\NewWork\\Study\\src\\btp\\oneP\\BufferedInputFile.java"));
21+
}
22+
23+
}

src/btp/oneP/ChangeSystemOut.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package btp.oneP;
2+
3+
import java.io.PrintWriter;
4+
5+
public class ChangeSystemOut {
6+
7+
public static void main(String[] args) {
8+
// TODO Auto-generated method stub
9+
//PrintWriter有接受OutputStream的构造器,将其用OutputStreamWriter转换为Writer
10+
PrintWriter out = new PrintWriter(System.out,true);
11+
out.println("Hello,world");
12+
}
13+
14+
}

src/btp/oneP/ChannelCopy.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package btp.oneP;
2+
3+
import java.io.FileInputStream;
4+
import java.io.FileNotFoundException;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.nio.ByteBuffer;
8+
import java.nio.channels.FileChannel;
9+
10+
public class ChannelCopy {
11+
private static final int BSIZE = 1024;
12+
public static void main(String[] args) throws IOException {
13+
FileChannel in = new FileInputStream("C:\\Users\\dell\\Desktop\\IO\\channelIn.txt").getChannel(),
14+
out = new FileOutputStream("C:\\Users\\dell\\Desktop\\IO\\channelOut.txt").getChannel();
15+
ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
16+
while(in.read(buffer) != -1){
17+
//写完之后:limit = capacity;position = 实际数据大小
18+
buffer.flip();//limit->position;position->0
19+
out.write(buffer);
20+
buffer.clear();//limit->capacity;position->0;准备下一路的读
21+
}
22+
}
23+
24+
}

src/btp/oneP/DirList.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package btp.oneP;
2+
3+
import java.io.File;
4+
import java.io.FilenameFilter;
5+
import java.util.Arrays;
6+
import java.util.regex.Pattern;
7+
8+
public class DirList {
9+
10+
public static void main(String[] args) {
11+
File path = new File("E:\\workspaces\\NewWork\\Study\\src\\btp\\oneP");
12+
String[] list;
13+
if(args.length == 0){
14+
list = path.list();
15+
}else{
16+
list = path.list(new DirFilter(args[0]));
17+
}
18+
Arrays.sort(list, String.CASE_INSENSITIVE_ORDER);
19+
for(String dirItem : list){
20+
System.out.println(dirItem);
21+
}
22+
}
23+
24+
}
25+
26+
class DirFilter implements FilenameFilter{
27+
private Pattern pattern;
28+
29+
public DirFilter(String regex){
30+
pattern = Pattern.compile(regex);
31+
}
32+
@Override
33+
public boolean accept(File dir, String name) {
34+
// TODO Auto-generated method stub
35+
return false;
36+
}
37+
38+
}

0 commit comments

Comments
 (0)