什么是输出流?
程序能向其中写入数据的流
程序 -> 文件
称为输出(output 写文件)
举个例子,就像学生写作业 交给老师
什么叫输入流?
程序可以从中读取数据的流
文件 -> 程序
称为输入(input 读文件)
一样的例子 就像老师改完的作业 拿去给学生阅读
字节输出流
OutputStream
是一个抽象类,是所有输出字节流的父类
因为是抽象类,所以不能直接实例化对象
需要子类来重写
这里我们用FileOutputStream这个子类来操作文件
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/su.txt");
FileOutputStream oStream = new FileOutputStream(file);
oStream.write(49);
oStream.write(48);
byte[] b = {65,66,67};
oStream.write(b);
oStream.write(b, 0, 3);
oStream.write("hello".getBytes());
oStream.write("world".getBytes());
oStream.close();
}
}
文件的续写
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/sudie.txt");
FileOutputStream oStream = new FileOutputStream(file, true);
oStream.write("hello\n".getBytes());
oStream.close();
}
}
异常处理
public class Demo {
public static void main(String[] args) {
File file = new File("/Users/s/Desktop/Test/sudie.txt");
FileOutputStream oStream = null;
try{
oStream = new FileOutputStream(file);
o.Stream.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("关闭失败")
}
}
}
}
字节输入流
InputStream 是所有输入流的父类
注意: 字节流写入的时候 是一个字节一个字节的写
: 读取也是一个字节一个字节的读
读取文件流程
1.绑定数据源文件(要读哪个文件)
2.使用read方法读文件
3.关闭资源
在你想要读的路径下创建一个txt文件 这里我取名为 su.txt
在里面输入 abcde保存一下
public class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/su.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 class Demo {
public static void main(String[] args) throws IOException {
File file = new File("/Users/s/Desktop/Test/su.txt");
FileInputStream iStream = new FileInputStream(file);
int num = 0;
while ((num = iStream.read()) != -1) {
System.out.println(num);
}
}
文件的复制
public class Demo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream("/Users/lanou/Desktop/Test/taytay.jpg");
fos = new FileOutputStream("/Users/lanou/Desktop/Test/Taylor.jpg");
byte[] b = new byte[1024];
int len = 0;
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件未找到");
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
throw new RuntimeException("关闭资源失败");
} finally {
try {
if (fos != null) {
fos.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 oldFile = new File("/Users/lanou/Desktop/Test");
File newFile = new File("/Users/lanou/Desktop/XTest");
copyFile(oldFile, newFile);
}
public static void copyFile(File oldFile,File newFile) throws IOException {
File newDir = new File(newFile, oldFile.getName());
newDir.mkdir();
File[] listFiles = oldFile.listFiles();
int len = 0;
for (File subFile : listFiles) {
if (subFile.isFile()) {
FileInputStream fis = new FileInputStream(subFile);
File tempFile = new File(newDir, subFile.getName());
FileOutputStream fos = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
}else {
copyFile(subFile, newDir);
}
}
}
}
将一个文件夹的所有txt文件 复制 到另一个文件夹下
public class Demo03 {
public static void main(String[] args) throws IOException {
File oldFile = new File("/Users/lanou/Desktop/Test");
File newFile = new File("/Users/lanou/Desktop/XTest");
getOldFlieTxtToNewFile(oldFile, newFile);
}
public static void getOldFlieTxtToNewFile(File oldFile, File newFile) throws IOException {
File[] listFiles = oldFile.listFiles(new TXTFiles());
for (File subFile : listFiles) {
if (subFile.isFile()) {
FileInputStream fis = new FileInputStream(subFile);
File tempFile = new File(newFile, subFile.getName());
FileOutputStream fos = new FileOutputStream(tempFile);
byte[] b = new byte[1024];
int len = 0;
if ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
fis.close();
fos.close();
}else {
getOldFlieTxtToNewFile(subFile, newFile);
}
}
}
}
class TXTFiles implements FileFilter{
@Override
public boolean accept(File pathname) {
if (pathname.isDirectory()) {
return true;
}
return pathname.getName().endsWith("txt");
}
}
Day.24
http://blog.csdn.net/ssssssue