控制台输入输出
@Test
public void reader() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.err.println("输入字符, 按下 'q' 键退出。");
char a;
do {
a = (char) reader.read();
System.err.println(a);
} while (a != 'q');
}
@Test
public void readerLine() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.err.println("输入字符, 按下 'end' 键退出。");
String str;
do {
str = reader.readLine();
System.err.println(str);
} while (!str.equals("end"));
int b = 'A';
System.err.write(b);
System.err.write('\n');
}
读写文件
一个流被定义为一个数据序列。输入流用于从源读取数据,输出流用于向目标写数据。
下图是一个描述输入流和输出流的类层次图
@Test
public void outPutStreamTest() {
byte[] bytes = { 11, 21, 3, 40, 5 };
try {
OutputStream output = new FileOutputStream("D:\\File\\text.txt");
for (int i = 0; i < bytes.length; i++) {
output.write(bytes[i]);
}
output.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void inputStream() {
try {
InputStream inputStream = new FileInputStream("D:\\File\\text.txt");
int available = inputStream.available();
for (int i = 0; i < available; i++) {
System.err.println(inputStream.read());
}
inputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void outputStreamWriterTest() {
File file = new File("D:\\File\\text.txt");
try {
// 构建FileOutputStream对象,文件不存在会自动新建
OutputStream outputStream = new FileOutputStream(file);
// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
OutputStreamWriter writer = new OutputStreamWriter(outputStream, "UTF-8");
writer.append("中文输入");// 写入到缓冲区
writer.append("\r\n"); // 换行
// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
writer.append("English");
// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
writer.close();
// 关闭输出流,释放系统资源
outputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void inputStreamReaderTest() {
File file = new File("D:\\File\\text.txt");
try {
InputStream inputStream = new FileInputStream(file);
InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
StringBuffer sb = new StringBuffer();
while (reader.ready()) {
sb.append((char) reader.read());
}
// 关闭读取流
reader.close();
// 关闭输入流,释放系统资源
inputStream.close();
System.err.println(sb.toString());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
目录
创建目录
/**
* 创建文件
*/
@Test
public void fileTest() {
String pathname = "D:\\File\\test\\text";
File file = new File(pathname);
file.mkdirs();
}
读取目录
/**
* 判断是文件还是目录
*/
@Test
public void dirList() {
String pathname = "D:\\File";
File file = new File(pathname);
if (file.isDirectory()) {//判断是文件还是目录
String[] strings = file.list();
for (String string : strings) {
File f = new File(pathname + "\\" + string);
if (f.isDirectory()) {
System.err.println(string + " 是一个目录");
} else {
System.err.println(string + " 是一个文件");
}
}
} else {
System.err.println(pathname + " 是一个文件");
}
}
删除目录或文件
/**
* 递归删除目录
*/
@Test
public void deleteFile() {
String pathname = "D:\\File";
File file = new File(pathname);
deleteFolder(file);
}
public void deleteFolder(File folder) {
List<File> fileList = Arrays.asList(folder.listFiles());
Optional.ofNullable(fileList).ifPresent(list -> {
list.forEach(f -> {
if (f.isDirectory()) {
deleteFolder(f);
} else {
f.delete();
}
});
});
// if (fileList != null) {
// for (File file : fileList) {
// if (file.isDirectory()) {
// deleteFolder(file);
// } else {
// file.delete();
// }
// }
// }
folder.delete();
}