-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMain.java
More file actions
196 lines (180 loc) · 6.29 KB
/
Copy pathTestMain.java
File metadata and controls
196 lines (180 loc) · 6.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
*
*/
package com.javaIO.test;
import java.io.*;
/**
* @author zhangbo
* 2017年2月15日 下午12:57:26
*
*/
public class TestMain {
/*
*
* File类共提供了三个不同的构造函数,以不同的参数形式灵活地接收文件和目录名信息。构造函数:
1)File (String pathname)
例:File f1=new File("FileTest1.txt"); //创建文件对象f1,f1所指的文件是在当前目录下创建的FileTest1.txt
2)File (String parent , String child)
例:File f2=new File(“D:\\dir1","FileTest2.txt") ;// 注意:D:\\dir1目录事先必须存在,否则异常
3)File (File parent , String child)
例:File f4=new File("\\dir3");
File f5=new File(f4,"FileTest5.txt"); //在如果 \\dir3目录不存在使用f4.mkdir()先创建
一个对应于某磁盘文件或目录的File对象一经创建, 就可以通过调用它的方法来获得文件或目录的属性。
1)public boolean exists( ) 判断文件或目录是否存在
2)public boolean isFile( ) 判断是文件还是目录
3)public boolean isDirectory( ) 判断是文件还是目录
4)public String getName( ) 返回文件名或目录名
5)public String getPath( ) 返回文件或目录的路径。
6)public long length( ) 获取文件的长度
7)public String[ ] list ( ) 将目录中所有文件名保存在字符串数组中返回。
File类中还定义了一些对文件或目录进行管理、操作的方法,常用的方法有:
1) public boolean renameTo( File newFile ); 重命名文件
2) public void delete( ); 删除文件
3) public boolean mkdir( ); 创建目录
*/
private static void fileTest() { //在指定的路径下创建文件,统计文件文件夹下的所有文件名称
File dir = new File("D:\\github\\javaIOExample\\files");
File f1 = new File(dir, "fileOne.txt");
File f2 = new File(dir, "fileTwo.java");
try {
if (!dir.exists())
dir.mkdir();
if (!f1.exists())
f1.createNewFile();
if (!f2.exists())
f2.createNewFile();
System.out.println("f1's AbsolutePath= " + f1.getAbsolutePath());
System.out.println("f1 Canread=" + f1.canRead());
System.out.println("f1's len= " + f1.length());
String[] FL;
int count = 0;
FL = dir.list();
for (int i = 0; i < FL.length; i++) {
count++;
System.out.println(FL[i] + "is in /rootpath");
}
System.out.println("there are" + count + " file in /rootpath ");
} catch (Exception e) {
e.printStackTrace();
}
/*
* 说明:File类的方法:
(1) exists()测试磁盘中指定的文件或目录是否存在
(2) mkdir()创建文件对象指定的目录(单层目录)
(3) createNewFile()创建文件对象指定的文件
(4) list()返回目录中所有文件名字符串
*
*/
}
/*
* 以文件作为数据输入源的数据流。
* 以数据流的方式读取文件
*/
private static void fileInputStreamTest() {
try {
File f = new File("src/com/javaIO/test/TestMain.java"); //相对路径
FileInputStream rf = new FileInputStream(f);
int n = 512;
byte buffer[] = new byte[n];
while ((rf.read(buffer, 0, n) != -1) && (n > 0)) {
System.out.println(new String(buffer));
}
System.out.println();
rf.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//标准输出流
//把数据写到 write.txt 文件中
private static void fileOutputStreamTest(){
try {
System.out.println("please Input from Keyboard");
int count, n = 512;
// byte buffer[] = new byte[n];
// count = System.in.read(buffer);
FileInputStream rf = new FileInputStream(new File("src/com/javaIO/test/TestMain.java"));
byte buffer[] = new byte[n];
FileOutputStream wf = new FileOutputStream("D:\\github\\javaIOExample\\files\\write.txt");
while ((rf.read(buffer, 0, n) != -1) && (n > 0)) {
// System.out.println(new String(buffer));
wf.write(buffer, 0, n);
}
wf.close(); // 当流写操作结束时,调用close方法关闭流。
System.out.println("Save to the write.txt");
} catch (IOException IOe) {
System.out.println("File Write Error!");
}
}
// FileInputStream流和FileOutputStream的应用
private static void fileinoutputTest(){
try {
File inFile = new File("src/com/javaIO/test/TestMain.java");
File outFile = new File("D:\\github\\javaIOExample\\files\\TestMaincopy2.java");
FileInputStream finS = new FileInputStream(inFile);
FileOutputStream foutS = new FileOutputStream(outFile);
int c;
while ((c = finS.read()) != -1) {
foutS.write(c);
}
finS.close();
foutS.close();
} catch (IOException e) {
System.err.println("FileStreamsTest: " + e);
}
}
//将BufferedReader与标准的数据流相接
private static void readWriteToFile() throws IOException{
InputStreamReader sin = new InputStreamReader(System.in);
BufferedReader bin = new BufferedReader(sin);
FileWriter out = new FileWriter("myfile.txt");
BufferedWriter bout = new BufferedWriter(out);
String s;
while ((s = bin.readLine()).length() > 0) {
bout.write(s, 0, s.length());
}
}
/*
* InputStream和OutputStream类处理的是字节流,数据流中的最小单位是字节(8个bit)
Reader与Writer处理的是字符流,在处理字符流时涉及了字符编码的转换问题
*/
private static void readBuff(byte[] buff) throws IOException {
ByteArrayInputStream in = new ByteArrayInputStream(buff);
int data;
while ((data = in.read()) != -1)
System.out.print(data + " ");
System.out.println();
in.close();
}
private static void encodeTest() {
try {
System.out.println("内存中采用unicode字符编码:");
char c = '好';
int lowBit = c & 0xFF;
int highBit = (c & 0xFF00) >> 8;
System.out.println("" + lowBit + " " + highBit);
String s = "好";
System.out.println("本地操作系统默认字符编码:");
readBuff(s.getBytes());
System.out.println("采用GBK字符编码:");
readBuff(s.getBytes("GBK"));
System.out.println("采用UTF-8字符编码:");
readBuff(s.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* @author zhangbo
* 2017年2月15日 下午12:57:28
* @throws IOException
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// fileInputStreamTest() ;
// fileOutputStreamTest();
//fileinoutputTest();
encodeTest();
System.out.println("Hello World !!!");
}
}