字节流
-->
-->
字节输出流
--
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/yin.txt");
FileOutputStream oStream = new FileOutputStream(file);
oStream.write(48);
byte[] b = {65,66,67,68};
oStream.write(b);
oStream.write(b, 0, 3);
oStream.write("hello".getBytes());
oStream.write("world".getBytes());
oStream.close();
}
文件的续写和换行:
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/gs.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("hello\n".getBytes());
oStream.close();
}
public static void main(String[] args) throws IOException {
File file = new File("/Users/lanou/Desktop/level/gs.txt");
FileOutputStream oStream = null;
try {
oStream = new FileOutputStream(file);
oStream.write("hello".getBytes());
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到");
} catch (IOException e) {
throw new RuntimeException("文件编写失败");
} finally {
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭失败");
}
}
}
字节输入流
字节输入流(Input) -- 读文件
InputStream是抽象类不能实例化对象并且是所有输入流的父类
注意:读取是一个字节一个字节的读
读取文件流程
1.绑定数据源文件
2.使用read方法读
3.关闭资源
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
int i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println((char)i1);
i1 = iStream.read();
System.out.println(i1);
iStream.close();
}
循环读取:
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
int num = 0;
while ((num=iStream.read()) != -1) {
System.out.println((char)num);
}
iStream.close();
}
注意:read方法每调用一次就读取一个字节,循环的时候read方法只能出现一次
使用字节数组读取文件:
public static void main(String[] args) throws IOException{
File file = new File("/Users/lanou/Desktop/level/znb.txt");
FileInputStream iStream = new FileInputStream(file);
byte[] b = new byte[2];
int i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
i1 = iStream.read(b);
System.out.println(i1);
System.out.println(new String(b));
iStream.close();
}
字节的输入输出流练习
使用字节的输入输出流进行文件的复制:
public class Demo01 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
FileInputStream iStream = null;
FileOutputStream oStream = null;
try {
iStream = new FileInputStream("/Users/lanou/Desktop/level/9.jpg");
oStream = new FileOutputStream("/Users/lanou/Desktop/level/10.jpg");
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件找不到");
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (iStream != null) {
iStream.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (oStream != null) {
oStream.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
}
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}
将一个文件夹复制到另一个文件夹:
public class Demo02 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/level");
File file2 = new File("/Users/lanou/Desktop/XTest");
copyFile(file1, file2);
}
public static void copyFile(File file1,File file2) throws IOException {
File newDir = new File(file2, file1.getName());
newDir.mkdir();
File[] listFiles = file1.listFiles();
for (File file3 : listFiles) {
if (file3.isDirectory()) {
copyFile(file3, newDir);
} else {
FileInputStream iStream = new FileInputStream(file3);
File tempFile = new File(newDir, file3.getName());
FileOutputStream oStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
iStream.close();
oStream.close();
}
}
}
}
将一个文件夹下的txt文件复制到另一个文件夹:
public class Demo03 {
public static void main(String[] args) throws IOException {
File file1 = new File("/Users/lanou/Desktop/level");
File file2 = new File("/Users/lanou/Desktop/XTest");
getTXTFile(file1, file2);
}
public static void getTXTFile(File file1,File file2) throws IOException {
File[] listFiles = file1.listFiles(new FilterTXT());
for (File file : listFiles) {
if (file.isFile()) {
FileInputStream iStream = new FileInputStream(file);
File tempFile = new File(file2, file.getName());
FileOutputStream oStream = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int num = 0;
while ((num = iStream.read(b)) != -1) {
oStream.write(b, 0, num);
}
iStream.close();
oStream.close();
} else {
getTXTFile(file, file2);
}
}
}
}
class FilterTXT implements FileFilter {
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
return pathname.getName().endsWith("txt");
}
}