IO输入输出流

  1. 1. File
    1. 1.1. 含义
    2. 1.2. 常用方法
    3. 1.3. 案例一
    4. 1.4. 案例二
    5. 1.5. 案例三
    6. 1.6. 案例四
    7. 1.7. 案例五
  2. 2. IO
    1. 2.1. 含义
    2. 2.2. 分类
    3. 2.3. 使用场景
  3. 3. 字节流
    1. 3.1. 含义
    2. 3.2. 使用场景
    3. 3.3. 分类
  4. 4. 字节流输入流
    1. 4.1. 体系结构
    2. 4.2. 常用方法
    3. 4.3. 案例一
    4. 4.4. 案例二
  5. 5. 字节输入缓冲流
    1. 5.1. 含义
    2. 5.2. 常用方法
    3. 5.3. 示意图
    4. 5.4. 案例
  6. 6. 字节输出流
    1. 6.1. 含义
    2. 6.2. 体系结构
    3. 6.3. 常用方法
    4. 6.4. 案例
  7. 7. 字节输出缓冲流
    1. 7.1. 含义
    2. 7.2. 常用方法
    3. 7.3. 案例
  8. 8. 字节流文件复制
    1. 8.1. 思路
    2. 8.2. 代码
  9. 9. 字符流
    1. 9.1. 含义
    2. 9.2. 使用场景
    3. 9.3. 分类
  10. 10. 字符输入流
    1. 10.1. 含义
    2. 10.2. 体系结构
    3. 10.3. 案例
  11. 11. 字符输出流
    1. 11.1. 含义
    2. 11.2. 体系结构
    3. 11.3. 案例
  12. 12. 字符流文件复制
  13. 13. 代码行数统计
  14. 14. 文件下载
  15. 15. Properties
    1. 15.1. 含义
    2. 15.2. 体系结构
    3. 15.3. 作用
    4. 15.4. 使用步骤
    5. 15.5. 案例

File

含义

File对象表示一个文件或文件夹

常用方法

  • pathSeparator

  • pathSeparatorChar

  • separator

  • separatorChar

  • new File(String pathName)

  • new File(String parentName,String childName)

  • new File(File parentFile,String childName)

  • mkdir

  • mkdirs

  • createNewFile

  • delete

    删除普通文件或目录

  • renameTo

  • exists

  • isFile

  • isDirectory

  • getParent

  • getParentFile

  • getAbsolutePath

  • list

  • listFiles

案例一

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
public class _01FileDemo {

//File对象
//含义:文件或文件夹


public static void main(String[] args) {

//;
System.out.println(File.pathSeparator);
System.out.println(File.pathSeparatorChar);

//C:\\abc\\efg
System.out.println(File.separator);
System.out.println(File.separatorChar);

//构造器
File file1 = new File("C:\\abc.txt");

System.out.println(file1.isFile());


File file2 = new File("C:", "abc.txt");
System.out.println(file2.isDirectory());


File file3 = new File(new File("C:"), "abc");
System.out.println(file3.exists());
}
}

案例二

创建、删除

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
public class _02FileDemo {

//File对象
//含义:文件或文件夹
//常用
//mkdir
//mkdirs
//delete
//...

public static void main(String[] args) throws IOException {

File file = new File("C:\\efg");
System.out.println(file.exists());

boolean sign = false;
if(!file.exists()){
sign = file.mkdir();
System.out.println(sign);
}


File otherFile = new File("C:\\11\\22\\33");

sign = otherFile.mkdirs();

System.out.println(sign);

//删除
new File("C:\\abc.txt").delete();

new File("C:\\efg").delete();
new File("C:\\11").delete();


file = new File("C:\\11.txt");
if(!file.exists()){
file.createNewFile();
}
file.renameTo(new File("C:\\22.txt"));
}
}

案例三

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
public class _03FileDemo {

//File对象
//含义:文件或文件夹

public static void main(String[] args) throws IOException {

File file = new File("C:\\22.txt");

System.out.println(file.isFile());
System.out.println(file.isDirectory());
System.out.println(file.exists());


//System.out.println(file.canRead());
//System.out.println(file.canWrite());
//System.out.println(file.canExecute());


//System.out.println(file.lastModified());

//System.out.println(file.length());

//System.out.println(new File("C:\\Windows").length());

System.out.println(file.getParent());
System.out.println(file.getParentFile());

System.out.println(file.isAbsolute());
System.out.println(file.getAbsolutePath());
}
}

案例四

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
public class _04FileDemo {

//File对象
//含义:文件或文件夹

public static void main(String[] args) throws IOException {

File file = new File("D:\\java6\\02javase\\Day10");
System.out.println(Arrays.toString(file.list()));

File [] children = file.listFiles();
for(File child:children){
System.out.println(child.getAbsolutePath());
}

System.out.println("过滤.........");
children = file.listFiles(new FileFilter() {
@Override
public boolean accept(File pathname) {
return pathname.getName().endsWith(".txt");
}
});

//System.out.println(children.length);
for(File child:children){
System.out.println(child.getAbsolutePath());
}
}
}

案例五

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
public class _05FileDemo {

public static void main(String[] args) throws IOException {
listFileName("D:\\java6\\02javase\\Day09","|");
}

public static void listFileName(String path,String prefix){

File file = new File(path);

if(file.isFile()){
System.out.println(file.getName());
}else{
//逻辑孩子
File childrent [] = file.listFiles();
for(File child:childrent){
if(child.isFile()){
System.out.println(prefix+child.getName());
}else{
System.err.println(prefix+child.getName());
//又是一个文件夹
listFileName(child.getAbsolutePath(),prefix+"-");
}
}
}

}
}

IO

含义

输入输出流

分类

  • 流向
    • 输入
    • 输出
  • 传输单位
    • 字节
    • 字符

使用场景

  • 读取配置文件
  • 等等

字节流

含义

传输单位一个一个字节

使用场景

操作非文本文件

mp3、mp4、压缩文件等等

分类

  • 字节输出流
  • 字节输入流

字节流输入流

体系结构

  • InputStream
    • FileInputStream

常用方法

  • new FileInputStream(String path)

  • new FileInputStream(File file)

  • read()

    读取一个字节,-1代表读取到了末尾

  • read(byte [] cache)

    读取到的字节放入到cache中

  • close

    关闭管道

案例一

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
public class FileInputStreamDemo {

//字节流
//相关的基础类:InputStream\OutputStream

//输入流
//read

//输出流
//write

//字节输入流
public static void main(String[] args) {
readFile("C:\\abc.txt");
}

public static void readFile(String path){

InputStream inputStream=null;
try {
//新建管道
inputStream = new FileInputStream(path);

//每次只读一个字节
//int result = inputStream.read();
//System.out.println(result);

//循环读取
StringBuilder SB = new StringBuilder();
int result = -1;
while((result=inputStream.read())!=-1){
//System.out.println(result);
//System.out.println((char)result);
SB.append((char)result);
}
System.out.println(SB);

} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

}

案例二

使用缓冲

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
public class FileInputStreamDemoOther {

public static void main(String[] args) {

String result = readFile("C:\\abc.txt");

System.out.println(result);
}

public static String readFile(String path){
StringBuilder SB = null;
InputStream inputStream=null;
try {
inputStream = new FileInputStream("C:\\abc.txt"); //abcdefg

SB = new StringBuilder();
byte [] buffer = new byte[3];
//将读取到的字节,先放入到buffer中,返回的是读取到的总数量,-1代表末尾

int len = -1;
while((len = inputStream.read(buffer))!=-1){
SB.append(new String(buffer,0,len));
}
return SB.toString();
} catch (IOException e) {
e.printStackTrace();
return null;
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

字节输入缓冲流

含义

字节缓冲输入流

常用方法

  • read
  • close

示意图

案例

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
public class BufferedInputSteamDemo {
public static void main(String[] args) {
String path = "C:\\abc.txt";
readFile(path);
}

public static void readFile(String path){
InputStream inputStream = null;
BufferedInputStream bufferedInputStream =null;
try {
inputStream = new FileInputStream(path);
bufferedInputStream = new BufferedInputStream(inputStream);

//一个字节一个字节读取
//int result = bufferedInputStream.read();
//System.out.println((char)result);

byte [] cache = new byte[1024];

int len = -1;
while((len=bufferedInputStream.read(cache))!=-1){
System.out.println(new String(cache,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedInputStream!=null){
try {
bufferedInputStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}

字节输出流

含义

字节输出流

体系结构

  • OutputStream
    • FileOutputStream

常用方法

  • new FileOutputStream(Strint fileName)
  • new FileOutputStream(File file)
  • write
  • close

案例

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
public class FileOutputStreamDemo {

//FileOuputStream
//含义:字节输出流
//体系结构
//OutputStream
//FileOutputStream

//常用方法
//write
//close

public static void main(String[] args) {
String des = "C:\\xx\\33.txt";

writeToFile("中国加油",des);
}

public static void writeToFile(String content,String dest){
File target = new File(dest);
if(!target.getParentFile().exists()){
target.getParentFile().mkdirs();
}

OutputStream outputStream=null;
try {
outputStream = new FileOutputStream(target);

//outputStream.write(97);

//建议使用该方法
outputStream.write(content.getBytes());
} catch (IOException e) {
e.printStackTrace();
}finally {
if(outputStream!=null){
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

字节输出缓冲流

含义

对字节输出流的包装,内置有缓冲区。

常用方法

  • write

  • flush

    将缓冲区的数据输出到目标中

  • close

案例

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
public class BufferedOutputSteamDemo {

//含义:字节缓冲输入流

public static void main(String[] args) {
writeFile("JAVA","c:\\yy\\aa.txt");
}

public static void writeFile(String content,String target){

File targetFile = new File(target);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}

BufferedOutputStream bufferedOutputStream=null;
try {
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(target));

bufferedOutputStream.write(content.getBytes());
bufferedOutputStream.flush(); //刷新缓冲区的[缓冲区中数据输出到目标]
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedOutputStream!=null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

字节流文件复制

思路

一边读取一边输出

代码

StringUtil.java

1
2
3
4
5
6
public class StringUtil {

public static boolean isEmpty(String param){
return param == null || "".equals(param.trim());
}
}

ParamException.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class ParamException extends RuntimeException {
public ParamException() {
super();
}

public ParamException(String message) {
super(message);
}

public ParamException(String message, Throwable cause) {
super(message, cause);
}

public ParamException(Throwable cause) {
super(cause);
}
}

FileUtil.java

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
/**
* 文件工具类
* @author Administrator
*
*/
public class FileUtil {

public static void main(String[] args) {
boolean result = copyFile("C:\\abc.txt","D:\\xx\\yy\\22.txt");
System.out.println(result);
}


/**
* 文件的复制
* @param source 源文件
* @param target 目标文件
* @return false:复制失败 true:复制成功
* @throws IOException
*/
public static boolean copyFile(String source,String target){

//参数的判断
if(StringUtil.isEmpty(source) || StringUtil.isEmpty(target)){
throw new ParamException("参数都不能为空");
}

if(!new File(source).exists()){
throw new ParamException("参数错误");
}

File targetFile = new File(target);
if(!targetFile.getParentFile().exists()){
targetFile.getParentFile().mkdirs();
}

BufferedInputStream bufferedInputStream=null;
BufferedOutputStream bufferedOutputStream=null;

try {
bufferedInputStream = new BufferedInputStream(new FileInputStream(source));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(target));

byte [] cache = new byte[1024];

int len = -1;
while((len=bufferedInputStream.read(cache))!=-1){
bufferedOutputStream.write(cache, 0, len);
bufferedOutputStream.flush();
}
return true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedInputStream!=null){
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedOutputStream!=null){
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}

字符流

含义

传输单位一个一个字符

使用场景

操作文本文件

txt\xml\html\properties等等

分类

  • 字符输出流
  • 字符输入流

字符输入流

含义

字符输入流

体系结构

  • Reader
    • FileReader

案例

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
public class _01FileReadDemo {

//含义:字符输入流
//体系结构
//Reader
//FileReader

public static void main(String[] args) {

String source = "C:\\aa.txt";
String result = readFile(source);
System.out.println(result);
}

public static String readFile(String source) {

Reader reader=null;
try {
reader = new FileReader(source);


//读取到一个字符
//int result = reader.read();
//System.out.println((char)result);

StringBuilder SB = new StringBuilder();

char [] buffer = new char[1024];
int len = -1;

while((len=reader.read(buffer))!=-1){
SB.append(new String(buffer,0,len));
}
return SB.toString();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}

字符输出流

含义

字符输出流

体系结构

  • Writer
    • FileWriter

案例

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
public class _02FileWriterDemo {

//含义: 字符输出流
//体系结构:
//Writer
//FileWriter
//常用方法
//write
//close

public static void main(String[] args) {
boolean sign = writeFile("广州你好\r\n深圳你好","C:\\result.txt");
System.out.println(sign);
}

public static boolean writeFile(String content,String target) {

Writer writer=null;
try {
writer = new FileWriter(target);

writer.write(content);
return true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;

}
}

字符流文件复制

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
public class _03FileCopy {

public static void main(String[] args) {
copyFile("C:\\aa.txt","D:\\result.txt");
}

private static boolean copyFile(String source,String target) {

//BufferedInputStream
BufferedReader bufferedReader=null;
BufferedWriter bufferedWriter=null;
try {
bufferedReader = new BufferedReader(new FileReader(source));
bufferedWriter = new BufferedWriter(new FileWriter(target));


//从缓冲区中读取一个字符,-1代表末尾
//bufferedReader.read();

//从缓冲区中读取字符到字符数组中,-1代表末尾
//bufferedReader.read(cbuf)

//一行一行读,返回null代表末尾

String content = null;
while((content = bufferedReader.readLine())!=null){
//System.out.println(content);

//bufferedWriter.write(content+"\r\n");

bufferedWriter.write(content);
bufferedWriter.newLine();

//bufferedWriter.flush();
}
return true;
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedReader!=null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedWriter!=null){
try {
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}

代码行数统计

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
public class Test {

// 文件的递推+文件的io

private static int count;

public static void main(String[] args) {

listJava("C:\\Users\\Administrator\\workspace");
System.out.println(count);
}

private static void listJava(String path) {

File file = new File(path);

// 文件夹
File[] children = file.listFiles(new FileFilter() {

@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
// 普通文件,判断后缀是否为java
return file.getName().endsWith(".java");
}
}
});
for (File child : children) {
if (child.isFile()) {
//System.out.println(child.getName()); //java文件
count +=lineCount(child.getAbsolutePath());
} else {
listJava(child.getAbsolutePath());
}
}

}

private static int lineCount(String path){
int count=0;
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(path));

String content = null;

while((content = reader.readLine())!=null){
if(!"".equals(content.trim())){
count++;
}
}

} catch (IOException e) {
e.printStackTrace();
}finally {
if(reader!=null){
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return count;
}
}

文件下载

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
package com.neu.day10._04download;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class DownloadDemo {
//下载文件
//步骤
//使用网络的知识
//URL
//输入流

public static void main(String[] args) {
try {
boolean sign = download("https://www.jxycu.edu.cn/","C:\\index.html");
System.out.println(sign);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}

public static boolean download(String source,String target) throws MalformedURLException{

URL url = new URL(source);
HttpURLConnection connection =null;
OutputStream outputStream=null;
//新建连接
try {
connection = (HttpURLConnection) url.openConnection(); //管道

InputStream inputStream = connection.getInputStream();

outputStream = new FileOutputStream(target);

int len = -1;
byte [] cache = new byte[1024];
while((len=inputStream.read(cache))!=-1){
outputStream.write(cache, 0, len);
}

return true;
} catch (IOException e) {
e.printStackTrace();
}finally {

connection.disconnect();
//TODO
}
return false;
}
}

Properties

含义

加载配置文件的

体系结构

  • Map
    • Hashtable
      • Properties

作用

  • 读取配置文件其实就是properties文件

  • 避免硬编码

    硬编码意思是将软件的配置信息写死在java代码中,修改麻烦。

使用步骤

  • 在资源文件夹下新建xx.properties文件

    建议:新建名字为config的资源文件夹

  • 往里面添加数据

    key1=value2

    keyN=valueN

  • 通过IO读取该配置文件中的内容

案例

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
public class JDBCUtil {

private static String name;
private static String password;

static{

Properties properties = new Properties();

//会默认从bin目录下面加载内容
ClassLoader classLoader = JDBCUtil.class.getClassLoader();

InputStream inputStream = null;
try {
inputStream = classLoader.getResourceAsStream("jdbc.properties");
properties.load(inputStream);

name = properties.getProperty("name");
password = properties.getProperty("password");

} catch (IOException e) {
e.printStackTrace();
}finally {
if(inputStream!=null){
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static String getName() {
return name;
}

public static String getPassword() {
return password;
}
}