io

IO

1、标准输入:FileInputStream
File file = new File("abc.txt");
InputStream in = null;
try {
in = new FileInputStream(file);
byte[] bytes = new byte[2];
int len = -1;
while ((len = in.read(bytes)) != -1) {
String str = new String(bytes, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、标准输出:FileOutputStream
File file = new File("soutn.txt");
OutputStream out = null;
try {
out = new FileOutputStream(file, true);
String mes = "asdubfw";
byte[] bytes = mes.getBytes();
out.write(bytes, 0, bytes.length);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
3、标准copy
File file = new File("abc.txt");
File file2 = new File("soutn.txt");
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(file);
out = new FileOutputStream(file2, true);
byte[] bytes = new byte[2];
int len = -1;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
4、字符输入流FileReader
File file = new File("abc.txt");
Reader in = null;
try {
in = new FileReader(file);
char[] chars = new char[1024];
int len = -1;
while ((len = in.read(chars)) != -1) {
String str = new String(chars, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
5、字符输出流:FileWriter
File file = new File("soutn.txt");
Writer out = null;
try {
out = new FileWriter(file, true);
String mes = "asdubfw我得哎";
char[] bytes = mes.toCharArray();
out.write(bytes, 0, bytes.length);
// out.write(bytes);
//out.append()
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
6、字节数组输入流:ByteArrayInputStream 不需要关闭资源
byte[] bytes = "i love".getBytes();
InputStream in = null;
try {
in = new ByteArrayInputStream(bytes);
byte[] chars = new byte[1024];
int len = -1;
while ((len = in.read(chars)) != -1) {
String str = new String(chars, 0, len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
7、字节数组输出流:ByteArrayOutputStream
byte[] bytes = null;
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
String mes = "show me";
// 写到字节数组中
byte[] dates = mes.getBytes();
out.write(dates, 0, bytes.length);
out.flush();

        // 获取数据
        bytes = out.toByteArray();
        System.out.println(bytes.length);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

8、装饰流:字节缓冲输入输出流
File file = new File("abc.txt");
InputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(file));
byte[] bytes = new byte[2];
int len = -1;
while ((len = in.read(bytes)) != -1) {
String str = new String(bytes, 0, len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
10 字符缓冲输入流
File file = new File("abc.txt");
BufferedReader in = null;
try {
in = new BufferedReader(new FileReader(file));
String line = null;
int len = -1;
// 逐行读取
while ((line = in.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

File file = new File("soutn.txt");
BufferedWriter out = null;
try {
out = new BufferedWriter(new FileWriter(file, true));
String mes = "asdubfw我得哎";
char[] bytes = mes.toCharArray();
out.write(bytes, 0, bytes.length);
// out.write(bytes);
//out.append() out.newLine
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Commons-io
// 文件大小
long length = FileUtils.sizeOf(new File("D:/sd.tet"));
// 目录大小
length = FileUtils.sizeOf(new File("D:/sd"));
// 列出子孙集listFiles 文件 过滤文件 过滤目录

    // 读取文件 readFileToString readFileToByteArray

    // 逐行读取 readLines FileUtils.lineIterator()

    // write writeStringToFile

    // writeLines
posted @ 2020-11-26 01:20  杨大德  阅读(55)  评论(0)    收藏  举报